python - 在python中使用ffmpeg获取视频时长

标签 python video ffmpeg

我在我的电脑上使用 pip ffprobe 命令安装了 ffprobe,并从 here 安装了 ffmpeg。 .

但是,我仍然无法运行列出的代码 here .

我尝试使用以下代码失败。

SyntaxError: Non-ASCII character '\xe2' in file GetVideoDurations.py
on line 12, but no encoding declared; see
http://python.org/dev/peps/pep-0263/ for details

有人知道怎么回事吗?我没有正确引用目录吗?我是否需要确保 .py 和视频文件位于特定位置?

import subprocess

def getLength(filename):
    result = subprocess.Popen(["ffprobe", "filename"],
    stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
    return [x for x in result.stdout.readlines() if "Duration" in x]

fileToWorkWith = ‪'C:\Users\PC\Desktop\Video.mkv'

getLength(fileToWorkWith)

如果问题有点基础,我们深表歉意。我所需要的只是能够遍历一组视频文件并获取它们的开始时间和结束时间。

谢谢!

最佳答案

无需遍历 FFprobe 的输出。有 one simple command它只返回输入文件的持续时间:

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 <input_video>

您可以使用以下方法来获取持续时间:

def get_length(input_video):
    result = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', input_video], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    return float(result.stdout)

关于python - 在python中使用ffmpeg获取视频时长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31024968/

相关文章:

python - 加速这个 Python 脚本的想法

jquery - 如何让视频显示在特定div内?

Html5 视频在 Chrome 中无法正常工作

shell - ffmpeg:如何为所有视频输出添加水印?

audio - 将 png 与 mp4 ffmpeg 合并缺少音频

android - 无法使用 ffmpeg 读取音频流

python - 如何在fabric中并行执行多个任务

python - MySQL cur.execute with 格式

Python 3 print() 到变量

batch-file - 尝试使用 ffmpeg 将 wav 与视频文件合并