如何使用FFMPEG创建一个动画GIF与间隔?

时间:2014-02-19 05:04:20

标签: ffmpeg gif animated-gif

你好伙伴们,

简要概述我正在努力完成的事情;我有一个网站,它将接受视频上传,上传转换为mp4格式,以便在网上使用众多可用播放器之一进行制作。那部分都很好,花花公子。

现在的问题是我想在用户点击播放之前向用户显示视频的缩放预览(动画gif)。我现在正在使用的代码是

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

适用于以1帧/秒的速率创建固定宽度为150像素的缩放动画gif,但它只是视频前10秒的动画。我正在尝试做一些扩展帧间隙的内容,以覆盖整个视频长度,但创建一个不超过10秒的动画礼物。

例如说我有一个30秒的视频,我希望gif长10秒但覆盖整个30秒的帧,所以它可能从第3帧或第3帧开始并在gif中创建一个帧,然后在视频中6秒创建另一帧,然后在另一帧中创建9秒,等等,最终结果为

    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

你最终会得到一个长达10秒的GIF动画,展示整个视频的预览,无论它的长度如何。这基本上归结为 video length / 10 (length of desired animated gif) = interval to use between frames但我不知道如何使用这些数据来解决我的问题...

所有人都有一个关于如何相对轻松地完成这项工作的想法或建议吗?我可以通过代码计算长度并运行命令从视频中提取每个单独的帧然后从图像生成gif,但我希望能够只用一个命令完成所有操作。感谢。

1 个答案:

答案 0 :(得分:0)

所以我最后只是通过找到这个脚本(http://www.alberton.info/video_preview_as_animated_gif_with_ffmpeg_and_spl.html#.UxnU_IXYNyI)的代码来做到这一点,这使得你可以简单地指定视频中的位置按帧的百分比来提取以及帧之间的间隔我可以使用以下内容完成上述问题。

// 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');

以上将占视频长度的10%,抓取动画GIF的第一张图像的帧,创建60ms / 1秒延迟,在视频长度上移动20%并为每个视频重复该过程指定的百分比,导致任意长度的视频具有10秒长的动画GIF,在视频中以10%的间隔显示10帧,每个帧显示1秒钟。

相关问题