如何有效提取视频的特定部分(Python/FFmpeg)?
我有一堆视频,我想提取特定部分(作为视频或帧)。我从 .json 文件中获取特定部分,其中开始帧和结束帧根据标签存储,例如“视频中的猫”、“视频中的狗”。我在Python中有一个使用opencv的现有方法,使用提到的方法 here 但我发现了一个使用 ffmpeg 的单行代码,它比我的 Python 脚本更快、更高效,只是我必须手动填写开始和结束帧在这个命令中。
ffmpeg -i in.mp4 -vf select='between(n\,x\,y)' -vsync 0 frames%d.png
我读了一些关于 在 shell 脚本中处理 .json 文件 或 将参数传递给批处理脚本,这看起来相当复杂,可能会破坏我的系统。由于我不熟悉在 shell/批处理脚本中使用 .json 文件,因此我不知道如何开始。任何人都可以为我指明如何制作可以从 .json 文件读取变量并将其输入到我的 ffmpeg 命令中的批处理脚本的正确方向吗?
I have a bunch of videos for which I want to extract specific sections (either as videos or as frames). I get the specific sections from a .json file where the start and end frames are stored according to labels, like 'cat in video', 'dog in video'. I have an existing method in Python using opencv using the method mentioned here but I found a one-liner using ffmpeg which is a lot more faster and efficient than my Python script, except that I have to manually fill in the start and end frames in this command.
ffmpeg -i in.mp4 -vf select='between(n\,x\,y)' -vsync 0 frames%d.png
I read a few questions about working with .json files in a shell script or passing arguments to a batch script which looks quite complicated and might spoil my system. Since I'm not familar working with .json files in a shell/batch script, I'm not sure how to start. Can anyone point me in the right direction on how to make a batch script that can read variables from a .json file and input it into my ffmpeg command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于你已经熟悉Python,我建议你使用它来解析JSON文件,然后你可以使用 ffmpeg-python 库,它是 Python 的 ffmpeg 绑定。它还具有
crop
函数,该函数我想这就是你所需要的。另一种方法是使用 Python 脚本中的 os.system('ffmpeg') 调用,这样您就可以从脚本运行外部工具。
Since you're already familiar with Python, I suggest you to use it to parse JSON files, then you can use ffmpeg-python library, which is a ffmpeg binding for Python. It also has a
crop
function, which I assume is what you need.An alternative would be to use the
os.system('ffmpeg <arguments>')
calls from a Python script, which allows you to run external tools from the script.Python 通过其内置
json
包原生支持 JSON至于在 python 中执行此操作,这里有一种替代方法,您可以尝试我的 ffmpegio-core 包:
如果视频是恒定帧速率,则将开始和结束时间戳指定为输入可能会更快选项:
如果您不知道框架率,您为每个文件调用 ffprobe 和 ffmpeg,因此需要权衡。但如果您的输入视频很长,那么这可能是值得的。
但如果速度是您的首要目标,直接调用 FFmpeg 总是最快的。
ffmpegio
GitHub 存储库Python natively supports JSON with its builtin
json
packageAs for doing this in python, here is an alternative approach that you can try my
ffmpegio-core
package:If the videos are constant frame rate, it could be faster to specify the start and end timestamps as input options:
If you don't know the frame rate, you are calling ffprobe and ffmpeg for each file, so there is a tradeoff. But if your input video is long, it could be worthwhile.
But if speed is your primary goal, calling FFmpeg directly always is the fastest.
ffmpegio
GitHub repo