tags:

views:

854

answers:

3

I'm wondering how to use FFMpeg to grap the middle frame of a video. I've already written the C# to grab a frame at a certain time (ie pull one frame at second 3). But I've yet to figure out how to find the middle of video using the FFMpeg commands.

+3  A: 

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

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

Nicolas
A: 

FFmpeg certainly doens't have a single option for that. Few people need such functionality.

Also, I don't think that there's a universal way to find the middle of a video: Depending on the video codec and encoding parameters, you may not be able to find the length of the video or seek to a certain point in that video. Also, a video may even not have FPS information in it !

Serafeim
+2  A: 

This could be simplified, but here's some old code I had lying around that should to the trick. (Adjust the ffmpeg path to match the location of yours)

$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}";
Derek Gathright
Thanks, That's just what I was looking for!
Jon Dewees