ffmpeg generate thumbnail with aspect ratio, but X seconds into video

ffmpeg, php

Solution

You can do that with a command line similar to:

ffmpeg -i inputVideo -vf scale='min(300,iw)':-1 -ss 00:00:05 -f image2 -vframes 1 thumbnail.jpg

So in your script, add `-vf` (video filter) before scale and reorder input and output parameters like below:

$cmd = "/usr/local/bin/ffmpeg -i ".$this->getUploadRootDir()."/".$fname." -vf scale='min(300\, iw):-1' -ss 00:00:5 -f image2 -vframes 1 ".$this->getUploadRootDir()."/thumb_".$output;

Problem

So this command works for generating a thumbnail 5 seconds into the video, with a size of 300x300: ``` $cmd = '/usr/local/bin/ffmpeg -i '.$this->getUploadRootDir().'/'.$fname.' -ss 00:00:05 -f image2 -vframes 1 -s 300x300 '.$this->getUploadRootDir().'/thumb_'.$output; ``` However, I want to keep the aspect ratio so I changed my code to this: ``` $cmd = "/usr/local/bin/ffmpeg -i ".$this->getUploadRootDir()."/".$fname." -ss 00:00:5 -f image2 scale='min(300\, iw):-1' ".$this->getUploadRootDir()."/thumb_".$output; ``` The above code correctly sizes the image, however it's of the first frame in the video.. I need the thumbnail to be 5 seconds in. Any help would be much appreciated.

Original source