我正在尝试使用 FFMPEG 为上传的视频生成 8 个屏幕截图。我目前有:
ffmpeg -i Trailer-720p.mov -r .2 -vcodec png Preview-%d.png
每 5 秒生成一个屏幕截图。如何添加为按总时间百分比分布的帧生成屏幕截图的功能。谢谢。此外,是否可以生成 50% 的屏幕截图?谢谢。
最佳答案
如果您仅使用 -i 参数运行 ffmpeg,它将为您提供 stderr 上的视频长度(以及许多其他内容)。您可以围绕它写一些东西,将持续时间和预期的帧数转换为正确的 -r 参数。
这是 python 中的一个简单示例,它基本上完成了我所描述的操作。由于某种原因,我的 ffmpeg 版本生成的前两个静止图像都显示第 0 帧,但 Preview-3 到 Preview-n 的间隔是正确的。将第二个参数设置为“1”运行它,它将生成中间帧为 Preview-3.png。
#!/usr/bin/env python
import sys,os,re
from subprocess import *
if len(sys.argv)<=1:
print("usage: python oneinn.py filename frames")
sys.exit(0)
try:
fvideo = sys.argv[1]
frames = float(sys.argv[2])
except:
sys.stderr.write("Failed to parse parameters.\n")
sys.exit(1)
output = Popen(["ffmpeg", "-i", fvideo], stderr=PIPE).communicate()
# searching and parsing "Duration: 00:05:24.13," from ffmpeg stderr, ignoring the centiseconds
re_duration = re.compile("Duration: (.*?)\.")
duration = re_duration.search(output[1]).groups()[0]
seconds = reduce(lambda x,y:x*60+y,map(int,duration.split(":")))
rate = frames/seconds
print("Duration = %s (%i seconds)" % (duration, seconds))
print("Capturing one frame every %.1f seconds" % (1/rate))
output = Popen(["ffmpeg", "-i", fvideo, "-r", str(rate), "-vcodec", "png", 'Preview-%d.png']).communicate()
关于FFMPEG 生成 N 个等距的 PNG 截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3306888/