如何在 Python 中使用 ffprobe 获取视频文件的长度/持续时间
在我们之前的文章如何使用 ffmpeg/ffprobe 将视频元数据获取为 JSON中,我们展示了如何使用随 ffmpeg 一起提供的 ffprobe 生成 json 格式的输出。
假设已安装 ffprobe,你可以轻松地使用 Python 获取视频剪辑(例如 input.mp4)的持续时间:
ffprobe_duration.py
import subprocess
import json
input_filename = "input.mp4"
out = subprocess.check_output(["ffprobe", "-v", "quiet", "-show_format", "-print_format", "json", input_filename])
ffprobe_data = json.loads(out)
duration_seconds = float(ffprobe_data["format"]["duration"])
# 示例:duration_seconds = 11.6685编写此类代码时,注意如果你不正确使用 subprocess 会有 shell 代码注入的风险!
Check out similar posts by category:
Audio/Video, Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow