我在我的电脑上使用 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/