如何使用子过程捕获FFMPEG例外?
我在字幕上做一些工作,有些视频有1个字幕曲目,有些视频有2个字幕轨道。对于具有2个的人,我使用第二个(索引= 1)。我正在尝试使用Python自动化它。
对于带有2个字幕轨道的文件,我使用:
-vf“ subtitles ='file.mkv':si = 1
,对于具有1个字幕轨道的人,我使用:
-vf“ subtitles ='file.mkv':si = 0
我正在使用此代码:
for mkv in all_mkvs:
try:
subprocess.call(f'ffmpeg -i ... -vf "subtitles='file.mkv':si=1 ...')
except:
subprocess.call(f'ffmpeg -i ... -vf "subtitles='file.mkv':si=0 ...')
但是它似乎不在乎例外,只要符合1个字幕的文件,就只会结束循环,并给我带来了错误 。
Press [q] to stop, [?] for help
[Parsed_subtitles_0 @ 0000011af4912880] Shaper: FriBidi 1.0.10 (SIMPLE) HarfBuzz-ng 2.7.2 (COMPLEX)
[Parsed_subtitles_0 @ 0000011af4912880] Unable to locate subtitle stream in ./test/349.mkv
[AVFilterGraph @ 0000011af623d880] Error initializing filter 'subtitles' with args './test/349.mkv:si=1'
Error reinitializing filters!
Failed to inject frame into filter network: Operation not permitted
Error while processing the decoded data for stream #0:3
Conversion failed!
无论如何 似乎不在乎。
但这 net/xmooi.png“ alt =”在此处输入图像描述>
所以我试图捕获该错误,然后说“好的,在这种情况下,让我们做si = 0代替”。
I'm doing some work on subtitles and some videos have 1 subtitle track, others have 2 subtitle tracks. For those that have 2, I use the 2nd one (index = 1). I'm trying to automate it with python.
For files with with 2 subtitle tracks, I use:
-vf "subtitles='file.mkv':si=1
and for those with 1 subtitle track, I use:
-vf "subtitles='file.mkv':si=0
I'm using this code:
for mkv in all_mkvs:
try:
subprocess.call(f'ffmpeg -i ... -vf "subtitles='file.mkv':si=1 ...')
except:
subprocess.call(f'ffmpeg -i ... -vf "subtitles='file.mkv':si=0 ...')
But it doesn't seem to care about the exception and just ends the loop whenever it meets a file with 1 subtitle and gives me the error anyway.
Press [q] to stop, [?] for help
[Parsed_subtitles_0 @ 0000011af4912880] Shaper: FriBidi 1.0.10 (SIMPLE) HarfBuzz-ng 2.7.2 (COMPLEX)
[Parsed_subtitles_0 @ 0000011af4912880] Unable to locate subtitle stream in ./test/349.mkv
[AVFilterGraph @ 0000011af623d880] Error initializing filter 'subtitles' with args './test/349.mkv:si=1'
Error reinitializing filters!
Failed to inject frame into filter network: Operation not permitted
Error while processing the decoded data for stream #0:3
Conversion failed!
As you can see in the error message above, it says: "Error initializing filter .... si=1" because it should be si=0 in the case of that specific file, which is why I added the exception, but it doesn't seem to care about it.
So I'm trying to catch that error and say "ok, in that case, let's do si=0 instead".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
subprocess.call
返回返回代码,它永远不会提出。您可能想要
check_call
,它将在非零returncodes上提出subprocess.calledprocesserror
。https://docs.python.org/3/library/library/library/subprocess.html
subprocess.call
returns the returncode, it never raises.You probably want
check_call
, which will raise asubprocess.CalledProcessError
on non-zero returncodes.https://docs.python.org/3/library/subprocess.html