子进程不创建 ffmpeg 命令的输出文件
subprocess not creating output file of ffmpeg command
我正在尝试 运行 一个 ffmpeg 命令来记录我的屏幕并在 python 中创建记录的 .mp4 文件。当我在我的 shell 中 运行 它时,该命令有效,但是当我使用子进程 运行 在 Python 脚本中将它 运行 时它不工作。
问题是当运行将其与子进程结合使用时,未创建 output.mp4 文件。
命令如下:
timeout 10 ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental output.mp4
这里是 python 代码:
os.chdir('/home/user/Desktop/myProject/')
subprocess.run('timeout 5 ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental out.mp4')
是否要添加额外的配置以便子进程可以写入输出文件?
您可以使用 -t
选项,而不是使用 timeout
here。
添加 -t 00:00:05
参数,并删除 timeout
:
subprocess.run('ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental -t 00:00:05 out.mp4')
我认为使用命令参数比使用 timeout
终止进程更优雅。
subprocess.run
returns 一个 CompletedProcess
对象。您应该将其分配给一个变量,然后打印出命令的所有输出和错误(因为我认为,ffmpeg 会给出错误并且根本不会尝试写入文件,但您看不到)。
此外,您必须将关键字参数 shell
设置为 True,或使用 shlex.split
,否则命令的格式将不正确。 shlex.split
是首选方式,您可以阅读 here:
Providing a sequence of arguments is generally preferred, as it allows
the module to take care of any required escaping and quoting of
arguments (e.g. to permit spaces in file names).
并且您不想手动将字符串转换为参数列表!
并且无需从外部停止 ffmpeg(您的文件可能无法写入的另一个原因)。为此使用内置命令行 option -t
。
import shlex
import subprocess
import os
os.chdir('/home/user/Desktop/myProject/')
p = subprocess.run(shlex.split("ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental -t 00:00:05 out.mp4"), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(p.stdout)
在 Windows 上,出于歇斯底里的原因,您可以在没有 shell=True
的情况下传入单个字符串,它会起作用。对于可移植代码,您需要指定 shell=True
,或重构代码以避免它(即 generally recommended wherever feasible)。
另请注意,subprocess.run()
具有用于设置超时和指定子进程工作目录的关键字参数。
subprocess.run(
['ffmpeg', '-video_size', '1920x1080', '-framerate', '60',
'-f', 'x11grab', '-i', ':0.0+0,0', '-f', 'alsa',
'-ac', '2', '-i', 'pulse', '-acodec', 'aac',
'-strict', 'experimental', 'out.mp4'],
cwd='/home/user/Desktop/myProject/', # current working directory
timeout=5, # timeout
check=True # check for errors
)
With check=True
如果命令失败你会得到一个异常,如果命令超时超时会引发异常,不管你是否有 check=True
.
没有关于失败原因的更多信息,很难具体说明如何解决您的问题;但有了这个,希望您至少应该在错误消息中获得足够的信息来指导您。
我正在尝试 运行 一个 ffmpeg 命令来记录我的屏幕并在 python 中创建记录的 .mp4 文件。当我在我的 shell 中 运行 它时,该命令有效,但是当我使用子进程 运行 在 Python 脚本中将它 运行 时它不工作。
问题是当运行将其与子进程结合使用时,未创建 output.mp4 文件。
命令如下:
timeout 10 ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental output.mp4
这里是 python 代码:
os.chdir('/home/user/Desktop/myProject/')
subprocess.run('timeout 5 ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental out.mp4')
是否要添加额外的配置以便子进程可以写入输出文件?
您可以使用 -t
选项,而不是使用 timeout
here。
添加 -t 00:00:05
参数,并删除 timeout
:
subprocess.run('ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental -t 00:00:05 out.mp4')
我认为使用命令参数比使用 timeout
终止进程更优雅。
subprocess.run
returns 一个 CompletedProcess
对象。您应该将其分配给一个变量,然后打印出命令的所有输出和错误(因为我认为,ffmpeg 会给出错误并且根本不会尝试写入文件,但您看不到)。
此外,您必须将关键字参数 shell
设置为 True,或使用 shlex.split
,否则命令的格式将不正确。 shlex.split
是首选方式,您可以阅读 here:
Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).
并且您不想手动将字符串转换为参数列表!
并且无需从外部停止 ffmpeg(您的文件可能无法写入的另一个原因)。为此使用内置命令行 option -t
。
import shlex
import subprocess
import os
os.chdir('/home/user/Desktop/myProject/')
p = subprocess.run(shlex.split("ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental -t 00:00:05 out.mp4"), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(p.stdout)
在 Windows 上,出于歇斯底里的原因,您可以在没有 shell=True
的情况下传入单个字符串,它会起作用。对于可移植代码,您需要指定 shell=True
,或重构代码以避免它(即 generally recommended wherever feasible)。
另请注意,subprocess.run()
具有用于设置超时和指定子进程工作目录的关键字参数。
subprocess.run(
['ffmpeg', '-video_size', '1920x1080', '-framerate', '60',
'-f', 'x11grab', '-i', ':0.0+0,0', '-f', 'alsa',
'-ac', '2', '-i', 'pulse', '-acodec', 'aac',
'-strict', 'experimental', 'out.mp4'],
cwd='/home/user/Desktop/myProject/', # current working directory
timeout=5, # timeout
check=True # check for errors
)
With check=True
如果命令失败你会得到一个异常,如果命令超时超时会引发异常,不管你是否有 check=True
.
没有关于失败原因的更多信息,很难具体说明如何解决您的问题;但有了这个,希望您至少应该在错误消息中获得足够的信息来指导您。