我有以下代码
import subprocess
import re
from itertools import *
command = ['ffprobe', '-i', '/media/some_file.mp4']
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
text = p.stderr.read()
retcode = p.wait()
text = text.decode('utf-8')
p = re.compile("Duration(.*)")
num = 0 #for debugging
for line in iter(text.splitlines()):
print(str(num) + line) #for debugging
m = p.match(str(line))
if m != None:
print(m.group(1))
当我查看输出时,有一行显示"持续时间"在它上面,但是没有捕获它,从未到达print(m.group(1))。如果我将文本变量更改为"持续时间blahblah"的硬编码字符串。我得到" blahblah",这是我所期待的。似乎正则表达式不能识别从stderr返回的文本。如何将文本转换为正则表达式识别和匹配的格式?
我已经提出了以下解决方案,它是否应该帮助其他人尝试使用python从ffmpeg捕获持续时间
import subprocess
import re
command = ['ffprobe', '-i', '/media/some_file.mp4']
p = subprocess.Popen(command, stderr=subprocess.PIPE)
text = p.stderr.read()
retcode = p.wait()
text = text.decode('utf-8')
p = re.compile(".*Duration:\s([0-9:\.]*),", re.MULTILINE|re.DOTALL)
m = p.match(text)
print(m.group(1))
答案 0 :(得分:1)
p = re.compile(r".*?Duration(.*)")
试试这个。match
从开头开始,而duration
之前可能会有某些内容。