【问题标题】:How to create an animated GIF using FFMPEG with an interval?如何使用带间隔的 FFMPEG 创建动画 GIF?
【发布时间】:2014-03-19 06:04:33
【问题描述】:

各位大侠们好,

简要概述我要完成的工作;我有一个可以接受视频上传的网站,上传的内容会被转换为 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 来做到这一点,但我希望能够只用一个命令来完成这一切。谢谢。

【问题讨论】:

    标签: ffmpeg gif animated-gif


    【解决方案1】:

    所以我最终只是通过找到此脚本 (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 的第一张图像的帧,创建 60 毫秒/1 秒的延迟,占用视频长度的 20%,然后为每个视频重复该过程指定的百分比,这会导致任何长度的视频具有 10 秒长的动画 GIF,在视频中包含 10 帧,长度间隔为 10%,每帧显示 1 秒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 2010-11-14
      相关资源
      最近更新 更多