Python - 使用 ffprobe 获取视频的持续时间 [英] Python - getting duration of a video with ffprobe

查看:62
本文介绍了Python - 使用 ffprobe 获取视频的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,我正在尝试使用 ffprobe 获取文件视频的持续时间(以秒为单位).调用以下指令

I am new in Python and I am trying to get the duration (in seconds) of a file video by using ffprobe. Calling the following instruction

ffprobe -i video.mp4 -show_entries format=duration -v quiet -of csv="p=0"

在 CMD 上,我会在几秒钟内得到正确的结果,但是如果我在 python 中使用以下命令调用相同的指令:

on the CMD, I get the right result in seconds but if I call the same instruction in python by using:

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

它返回1.有没有办法通过 Python 获得正确的结果?提前致谢.

it returns 1. Is there a way to get the right result also through Python? Thank you in advance.

推荐答案

问题在于双引号参数 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

这篇关于Python - 使用 ffprobe 获取视频的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆