且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python - 使用 ffprobe 获取视频的持续时间

更新时间:2023-02-09 12:37:42

问题在于双引号参数 p=0 ,使用 % 对其进行格式化,同样我将 subprocess.call 更改为 subprocess.check_output 以将命令的输出存储在字符串中:

The problem is with the double quote argument p=0 , format it using %, also i changed subprocess.call to subprocess.check_output to store output of the command in a string :

import subprocess
duration = subprocess.check_output(['ffprobe', '-i', 'video.mp4', '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")])
print(duration)

输出:

8.824000

或者你也可以这样做:

import subprocess
result = subprocess.Popen(["ffprobe", "video.mp4"],stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
for x in result.stdout.readlines():
      if "Duration" in x:
          print(x[12:23])
          break