我是一个Python初学者,正试图编写一个脚本,在文件中查找黑视频和无声音频,并只返回它们出现的时间实例。
我有以下代码使用ffmpeg python包装器来获取stdout中的值,但我无法找到一种有效的方法来解析stdout或stderror,以仅返回black_start、black_end的实例,silence_duration。
对于那些不是专家的人来说,把ffmpeg放在一边,我如何使用re.findall或类似的东西来定义regex,只返回上面的值?
import ffmpeg
input = ffmpeg.input(source)
video = input.video.filter('blackdetect', d=0, pix_th=0.00)
audio = input.audio.filter('silencedetect', d=0.1, n='-60dB')
out = ffmpeg.output(audio, video, 'out.null', format='null')
run = out.run_async(pipe_stdout=True, pipe_stderr=True)
result = run.communicate()
print(result)
这将产生ffmpeg输出,其中包含我需要的结果。以下是输出(为简洁起见进行了编辑(:
(b'', b"ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple clang version 11.0.0 (clang-1100.0.33.17)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_3 --enable-shared --enable-pthreads --...
[silencedetect @ 0x7fdd82d011c0] silence_start: 0
frame= 112 fps=0.0 q=-0.0 size=N/A time=00:00:05.00 bitrate=N/A speed=9.96x
[blackdetect @ 0x7fdd82e06580] black_start:0 black_end:5 black_duration:5
[silencedetect @ 0x7fdd82d011c0] silence_end: 5.06285 | silence_duration: 5.06285
frame= 211 fps=210 q=-0.0 size=N/A time=00:00:09.00 bitrate=N/A speed=8.97x
frame= 319 fps=212 q=-0.0 size=N/A time=00:00:13.00 bitrate=N/A speed=8.63x
frame= 427 fps=213 q=-0.0 size=N/A time=00:00:17.08 bitrate=N/A speed=8.51x
frame= 537 fps=214 q=-0.0 size=N/A time=00:00:22.00 bitrate=N/A speed=8.77x
frame= 650 fps=216 q=-0.0 size=N/A time=00:00:26.00 bitrate=N/A speed=8.63x
frame= 761 fps=217 q=-0.0 size=N/A time=00:00:31.00 bitrate=N/A speed=8.82x
frame= 874 fps=218 q=-0.0 size=N/A time=00:00:35.00 bitrate=N/A speed=8.71x
frame= 980 fps=217 q=-0.0 size=N/A time=00:00:39.20 bitrate=N/A speed=8.67x
...
frame= 5680 fps=213 q=-0.0 size=N/A time=00:03:47.20 bitrate=N/A speed=8.53x
[silencedetect @ 0x7fdd82d011c0] silence_start: 227.733
[silencedetect @ 0x7fdd82d011c0] silence_end: 229.051 | silence_duration: 1.3184
[silencedetect @ 0x7fdd82d011c0] silence_start: 229.051
[blackdetect @ 0x7fdd82e06580] black_start:229.28 black_end:230.24 black_duration:0.96
frame= 5757 fps=214 q=-0.0 Lsize=N/A time=00:03:50.28 bitrate=N/A speed=8.54x
video:3013kB audio:43178kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[silencedetect @ 0x7fdd82d011c0] silence_end: 230.28 | silence_duration: 1.22856
n")
解析输出数据以只查找/返回这些结果值的最有效方法是什么,这样我就可以在代码中从中构建进一步的逻辑?在这种情况下,我只希望返回以下值:
silence_start:0
silence_end:506285
silence_duration:506285
black_start:0
black_end:5
黑色持续时间:5
silence_start:227.733
silence_end:229.051
silence_duration:1.3184
black_start:222.28
black_end:230.24
black_duration:0.96
silence_start:229.051
silence_end:230.28
silence_duration:1.22856
我用regex尝试了很多不同的re.findall((选项,但最接近的是只返回值的名称。例如,如果我将此添加到上面:
found = re.findall('\b' + 'silence_end' + '\b', str(result))
print(found)
我得到的只是名字:
['silence_end','silence_end','silence_end']
您可以选择两种方式来组合所有可能性,然后将1+位数字与可选点和1+位匹配:
b(?:silence|black)_(?:start|end|duration):s*d+(?:.d+)?b
图案将匹配:
b
字边界(?:silence|black)_
匹配静音或黑色和下划线(?:start|end|duration):s*
匹配开始、结束或持续时间、:
和0+空白字符d+(?:.d+)?
匹配1+数字和可选点一个数字部分b
字边界
Regex演示| Python演示
例如
import re
test_str = "your string"
regex = r"b(?:silence|black)_(?:start|end|duration):s*d+(?:.d+)?b"
print(re.findall(regex, test_str))
输出
['silence_start: 0', 'black_start:0', 'black_end:5', 'black_duration:5', 'silence_end: 5.06285', 'silence_duration: 5.06285', 'silence_start: 227.733', 'silence_end: 229.051', 'silence_duration: 1.3184', 'silence_start: 229.051', 'black_start:229.28', 'black_end:230.24', 'black_duration:0.96', 'silence_end: 230.28', 'silence_duration: 1.22856']
借用米克尔的答案
regex = re.compile(r'''
[S]+: # a key (any word followed by a colon)
(?:
s # then a space in between
(?!S+:)S+d+ # then a value (any word not followed by a colon)
) # match multiple values if present
''', re.VERBOSE)
matches = regex.findall(str)
matches
['configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_3',
'silence_end: 5.06285',
'silence_duration: 5.06285',
'silence_start: 227.733',
'silence_end: 229.051',
'silence_duration: 1.3184',
'silence_start: 229.051',
'silence_end: 230.28',
'silence_duration: 1.22856']