Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to grab a video thumbnail in FFmpeg?

I'd like to grab the middle-most frame as the video and use that as the thumbnail. Video duration is unknown.

The ability to specify the dimensions of the thumbnail would also be helpful.

share|improve this question

First of all, always use the latest version of FFmpeg.

If you have access to PHP, your question is perfectly answered on Stack Overflow: Use FFMpeg to get middle frame of a video?

$output = shell_exec("/usr/local/bin/ffmpeg -i {$path}");
preg_match('/Duration: ([0-9]{2}):([0-9]{2}):([^ ,])+/', $output, $matches);
$time = str_replace("Duration: ", "", $matches[0]);
$time_breakdown = explode(":", $time);
$total_seconds = round(($time_breakdown[0]*60*60) + ($time_breakdown[1]*60) + $time_breakdown[2]);
shell_exec("/usr/local/bin/ffmpeg -y  -i {$path} -f mjpeg -vframes 1 -ss " . ($total_seconds / 2) . " -s {$w}x{$h} {$output_filename}"; 

What it'll do is just extract the duration from FFmpeg's output and use that to determine the timecode of the middle frame.

You can easily adapt that to other shells, and simply insert into the following command, where the middle frame is roughly at 534 seconds:

ffmpeg -y  -i input.mp4 -f mjpeg -vframes 1 -ss 534 thumbnail.jpg

You can always change the size with -s 480x320 or similar, depending on how big you want it, inserted somewhere after the -i input.mp4 option.

The above solution is a little inaccurate. It'll immediately give you a result but you can't specify which frame you want. If you already know the exact frame you want to extract, use the select filter, e.g. with the frame number 12345:

ffmpeg -i input.mp4 -filter:v select="eq(n\,12345)" -vframes 1 thumbnail.jpg

Note that this command can take a while since it needs to skip to the specified frame before it can extract it.

share|improve this answer
An alternate way to specify thumbnail dimensions - this example crops it to a 4:3 aspect ratio: ffmpeg -y -i input.mp4 -vf "crop=(ih*4/3):ih" -f mjpeg -vframes 1 -ss 534 thumbnail.jpg – rymo Jan 5 '14 at 3:47
Is there a way to skip the "waiting" part? Fast-forward to the specified frame/time ? – Samson Nov 20 '15 at 14:38
@Samson Which solution exactly are you talking about? If you specify -ss before -i, seeking should be much faster. – slhck Nov 23 '15 at 12:35
Exactly, much better thanks! – Samson Mar 14 at 23:30

In on line :

ffmpeg -i input.mp4 -vcodec mjpeg -vframes 1 -an -f rawvideo -ss `ffmpeg -i input.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , | awk -F ':' '{print $3/2}'` output.jpg

In the subcommand we get the total duration of the input video

ffmpeg -i input.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ,

And we pipe it to awk to compute duration/2 like this

echo '00:01:30.000' | awk -F ':' '{print ($3+$2*60+$1*3600)/2}'

So we give this among of seconds to -ss option and it's done.

If you would like to specify the size, add -s option like that WxH whet W and H are integer values (ex: -s 800x600 or -s svga) or read the ffmpeg man page to see other available options or format.

share|improve this answer

Here's a method using:

  • ffprobe to get the duration
  • bc to calculate half the duration
  • ffmpeg to make the thumbnail image


duration=$(ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 "$input")
ffmpeg -ss "$(echo "$duration"/2 | bc)" -i "$input" -q:v 2 -vframes 1 output.jpg
  • You can replace my lazy usage of bc with your favorite alternative, such as awk or printf, if you prefer.

  • You can scale/resize the image by adding the scale filter as an output option in your ffmpeg command, such as -vf scale=320:-1.

  • The duration supplied by ffprobe may not always be accurate depending on the input. In the unlikely case that you have such an issue you will have to fully decode the input, such as with ffmpeg -i input -f null -, and parse the output to get the correct duration value.

share|improve this answer

This bash command works like a charm:

avconv -i 'in.mpg' -vcodec mjpeg -vframes 1 -an -f rawvideo \
-s 420x300 -ss `avconv -i in.mpg 2>&1 | grep Duration \
| awk '{print $2}' | tr -d , \ 
| awk -F ':' '{print ($3+$2*60+$1*3600)/2}'` out.jpg
share|improve this answer
My god that's complicated – Sridhar-Sarnobat Mar 18 '15 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .