如何使用子过程捕获FFMPEG例外?

发布于 2025-01-29 07:05:28 字数 1198 浏览 5 评论 0原文

我在字幕上做一些工作,有些视频有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.

enter image description here

So I'm trying to catch that error and say "ok, in that case, let's do si=0 instead".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄司 2025-02-05 07:05:28

subprocess.call返回返回代码,它永远不会提出。

您可能想要check_call,它将在非零returncodes上提出subprocess.calledprocesserror

https://docs.python.org/3/library/library/library/subprocess.html

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

运行由ARGS描述的命令。等待命令完成,然后返回return Code属性。

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

带有参数的运行命令。等待命令完成。如果返回代码为零,则返回,否则会提高致电ProcoSessError。 naterProcessError对象将在返回代码属性中具有返回代码。如果check_call()无法启动该过程,则将传播提出的异常。

subprocess.call returns the returncode, it never raises.

You probably want check_call, which will raise a subprocess.CalledProcessError on non-zero returncodes.

https://docs.python.org/3/library/subprocess.html

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Run the command described by args. Wait for command to complete, then return the returncode attribute.

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Run command with arguments. Wait for command to complete. If the return code was zero then return, otherwise raise CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute. If check_call() was unable to start the process it will propagate the exception that was raised.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文