我可以使用以下命令将 gif 文件转换为 mp4:
ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4
我想要做的是循环 gif 动画 3 次并转换为 mp4。
我可以用 2 个 shell 命令来做到这一点。上面的第一个,然后将相同的视频连接 3 次。
ffmpeg -f concat -safe 0 -i <(printf "file '$PWD/video.mp4'\n%.0s" {1..3}) -c copy videoloop.mp4
我也尝试过使用 -ignore_loop 0 选项和设置时间。它确实有效,但这并不是我想要做的,因为我可以扩展视频但不能精确地制作 3 个循环。
ffmpeg -ignore_loop 0 -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -t 12 videoloop.mp4
如您所见,我已经能够实现我想要的,但是使用 2 个 shell 命令:
ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4
ffmpeg -f concat -safe 0 -i <(printf "file '$PWD/video.mp4'\n%.0s" {1..3}) -c copy videoloop.mp4
是否可以只调用一次 ffmpeg 来做到这一点?
我尝试使用输入文件的 -loop 选项。它不适用于 GIF。我也尝试使用 -stream_loop。它创造了一些被破坏的东西。
最佳答案
我用来转换 gif 至 mp4 处理 LOOPING 时分两步
例如GIF ---> 循环 ---> MP4
loop the gif itself first. In this example, the output will be added by 2 repetitions
[final file will play once, plus 2 repetitions (loops)]
ffmpeg -stream_loop 2 -i normal.gif loop.gif
convert the LOOPED GIF to mp4
ffmpeg -i loop.gif -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" loop.mp4
参数 -vf 用于避免比例划分错误并保持宽高比正常,摆脱错误消息,例如:高度不能被 2 整除 (320x569)
关闭时队列中剩余 2 帧
请注意:宽度 和 高度 被2除然后再乘以2
[相当棘手! ]\o/
关于ffmpeg - 如何在使用 ffmpeg 创建 mp4 作为输出时循环 gif 文件有限次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50910888/