gpt4 book ai didi

python-3.x - 我可以从我的 Python 代码运行 ffmpeg 并在压缩完成后返回一个信号吗?

转载 作者:行者123 更新时间:2023-12-04 22:48:36 26 4
gpt4 key购买 nike

似乎我正在寻找的东西(python 的简单 ffmpeg 接口(interface))不适用于 python 3.5。我想从我的代码中运行一个命令行,我将使用以下变量填充:

video_file = "/path/to/video/video.file"
sound_file = "/path/to/sound/sound.file"
output_name= (video.file + 'mp4')
fmpeg -loop 1 -i video_file -i sound_file -c:v libx264 -tune stillimage -c:a aac -strict experimental -b:a 192k -pix_fmt yuv420p -shortest output_name

是否可以返回一个信号来触发下一个函数,该函数将使用 output_name 作为值来执行下一件事?这甚至可能吗?

最佳答案

Subprocess.call() 返回子进程的退出代码。大多数程序在成功时返回 0,在失败时返回非零,ffmpeg 也是如此。

import subprocess

return_value = subprocess.call([
'ffmpeg',
'-loop', '1',
'-i', video_file,
'-i', sound_file,
'-c:v', 'libx264',
'-tune', 'stillimage',
'-c:a', 'aac',
'-strict', 'experimental',
'-b:a', '192k',
'-pix_fmt', 'yuv420p',
'-shortest', output_name,
])

if return_value:
print("Failure")
else:
print("Sucess!")

如您所见, subprocess.call() 期望其参数为字符串列表,除非您指定 shell=True这种说法被广泛认为是一种安全风险和不良做法。

使用 str.split() 之类的在 Python 解释器中将字符串转换为所需的格式,然后删除任何变量(如 video_file 和 output_name)的引号。

关于python-3.x - 我可以从我的 Python 代码运行 ffmpeg 并在压缩完成后返回一个信号吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35151758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com