I have a custom post type "Videos" where multiple video files can be uploaded. I need to generate a thumbnail for each of these uploaded videos and attach these thumbnails as "featured image" for the uploaded videos. I do this so that I can easily display the images in my theme, each image linked to the video.

With the help of FFMPEG, I have tried the following code - the thumbnail is generated correctly, but I am unable to attach them to the video as a featured image. A little guidance is appreciated.

add_filter( 'wp_generate_attachment_metadata', 'ron02_create_video_thumbnail', 10, 2 );
function ron02_create_video_thumbnail( $metadata, $attachment_id ) {

    //Get the attachment/post object
    $attachment_obj = get_post( $attachment_id );
    $attachment_url = wp_get_attachment_url($attachment_id);
    $video = get_attached_file($attachment_id );

    //Thumbnail format
    $tn_format = 'jpg';
    $image = $video.'.'.$tn_format;
    $thumb_url = $attachment_url.'.'.$tn_format;

    // where ffmpeg is located
    $ffmpeg = '/usr/local/bin/ffmpeg';

    //time to take screenshot at
    $interval = 1;

    //ffmpeg command
    $cmd = "$ffmpeg -i $video -ss 00:00:01.000 -vf scale='min(160\, iw):-1' -vframes 1 $image";
    $return = `$cmd`;
    echo $return;

    require_once(ABSPATH . '/wp-admin/includes/file.php');
    require_once(ABSPATH . '/wp-admin/includes/media.php');
    require_once(ABSPATH . '/wp-admin/includes/image.php');

    $image_array = array(
        'name' => basename( $thumb_url )

    media_handle_sideload( $image_array, $attachment_id );


The image is neither showing up in the media library nor being attached as a featured image for the video but it is generated and is present in the same upload folder as the video.

  • Did you get this to work? I'm just importing all my Instagram videos to WordPress (github.com/olind/ig-to-wp) and my main issue right now is the video thumbnail. – Ola Oct 19 '19 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.