[ACCEPTED]-Use FFMpeg to get middle frame of a video?-ffmpeg

Accepted answer
Score: 11

This could be simplified, but here's some 3 old PHP code I had lying around that should 2 do the trick. (Add the location to ffmpeg 1 if it's not in your path)

$output = shell_exec("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("ffmpeg -y  -i {$path} -f mjpeg -vframes 1 -ss " . ($total_seconds / 2) . " -s {$w}x{$h} {$output_filename}");   
Score: 8

With simple shell scripting you can use 3 ffprobe to get a machine readable duration output, bc to 2 calculate the half point, and ffmpeg to make the 1 frame:

input=input.mp4; ffmpeg -ss "$(bc -l <<< "$(ffprobe -loglevel error -of csv=p=0 -show_entries format=duration "$input")*0.5")" -i "$input" -frames:v 1 half.png

This eliminates any need for PHP, echo, awk, tr, grep, sed, etc.

Score: 4

FFmpeg helps you get the framerate and the 6 length of the video, so you can multiply 5 one by the other and divide by 2 to get 4 the number of the middle frame.

ie for a 3 30 seconds video running at 15 frames per 2 second : 30 * 15 = 450 / 2 = 225, meaning 1 you need to grab the 225th frame.

More Related questions