3

Hello fellow overflowers,

A brief overview of what I'm trying to accomplish; I have a site that will accept video uploads, uploads get converted into the mp4 format to be uniformed and playable on the web using one of the many available players. That part is all fine and dandy.

The problem now is I want to show the user a short scaled preview (animated gif) of the video before they click to play it. The code I'm working with now is

ffmpeg -i test.mp4 -vf scale=150:-1 -t 10 -r 1 test.gif

Which works for creating a scaled animated gif with a fixed width of 150px at a rate of 1 frame per second but its only an animation of the first 10 seconds of the video. I'm trying to do something that spreads out the frame gap to cover the whole video length but create an animated gift that's no more then 10 seconds long.

For example say I have a video that's 30 seconds I want the gif to be 10 seconds long but cover frames of the entire 30 seconds so it might start at frame 3 or 3 seconds in and create a frame in the gif, then at 6 seconds in the video create another frame, then 9 seconds in another, and so forth where the final outcome is

    example video 30 seconds long          example video 1 minute 45 second long 

video position - gif frame/per second      video position - gif frame/per second
      00:03:00   1                               00:10:50   1
      00:06:00   2                               00:21:00   2
      00:09:00   3                               00:31:50   3
      00:12:00   4                               00:42:00   4
      00:15:00   5                               00:52:50   5
      00:18:00   6                               01:03:00   6
      00:21:00   7                               01:13:50   7
      00:24:00   8                               01:24:00   8
      00:27:00   9                               01:34:50   9
      00:30:00   10                              01:45:00   10

  3 second interval between frames         10.5 second interval between frames

Where you end up with an animated gif that's 10 seconds long showing a preview of the entire video no matter the length of it. Which basically just boils down to video length / 10 (length of desired animated gif) = interval to use between frames but I don't know how I can use that data to accomplish my problem...

So does anyone have an idea or suggestion on how this can be accomplished with relative ease? I can probably do it by calculating the length through code and running a command to extract each individual frame from the video that's needed then generate a gif from the images but I'd like to be able to do it all with just one command. Thanks.

1 Answer 1

0

So I ended up just doing this by code having found this script (http://www.alberton.info/video_preview_as_animated_gif_with_ffmpeg_and_spl.html#.UxnU_IXYNyI) which made it easy where you can simply specify the position in the video by percentage on the frame to extract along with the interval between frames I can accomplish my problem above using the following.

// where ffmpeg is located, such as /usr/sbin/ffmpeg
$ffmpeg = '/usr/bin/ffmpeg';

// the input video file
$video = 'sample.avi';

// extract one frame at 10% of the length, one at 20% and so on
$frames = array('10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%');

// set the delay between frames in the output GIF in ms (60 = 1 min)
$joiner = new Thumbnail_Joiner(60);

// loop through the extracted frames and add them to the joiner object specifying 
// the max width/height to make the thumb based on the dimensions of the video
foreach (new Thumbnail_Extractor($video, $frames, '150x150', $ffmpeg) as $key => $frame) {
    $joiner->add($frame);
}

$joiner->save('sample.gif');

The above will go 10% into the length of the video, grab the frame for the first image of the animated GIF, create a 60ms/1sec delay, go 20% into the length of the video and repeat the process for each percentage specified which results in a video of any length having an animated GIF that's 10 seconds long containing 10 frames within the video at 10% intervals in length that show for 1 second each.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.