子进程不创建 ffmpeg 命令的输出文件 [英] subprocess not creating output file of ffmpeg command

查看:36
本文介绍了子进程不创建 ffmpeg 命令的输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个 ffmpeg 命令来记录我的屏幕并在 python 中创建一个 .mp4 文件.该命令在我的 shell 中运行时有效,但当我使用 subprocess 在 Python 脚本中运行它时不起作用.

I am trying to run an ffmpeg command that records my screen and creates an .mp4 file of the recording in python. The command works when I run it in my shell, but is not working when I am running it in a Python script using subprocess.

问题是在使用子进程运行时,没有创建 output.mp4 文件.

The issue is that when running it with subprocess, the output.mp4 file is not created.

命令如下: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代码:

Here is the python code:

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')

是否需要添加额外的配置以便子进程可以写入输出文件?

Is there an additional configuration to add so that subprocess can write output files?

推荐答案

subprocess.run 返回一个 CompletedProcess 对象.您应该将其分配给一个变量,然后打印出命令的所有输出和错误(因为我认为,ffmpeg 给出了错误并且根本不尝试写入文件,但您看不到).
此外,您必须将关键字参数 shell 设置为 True,或者使用 shlex.split,否则命令的格式将不正确.shlex.split 是首选方式,因为您可以阅读 此处:

subprocess.run returns an CompletedProcess object. You should assign that to a variable, and then print out all output and errors of the command (Because i think, ffmpeg gives an error and doesn't try to write the file at all, but you do not see that).
Additionally, you have to either set the keyword argument shell to True, or use shlex.split, else the command will not be formatted right. shlex.split is the preferred way, as you can read 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).

并且您不想手动将字符串转换为参数列表!

And you do not want to manually convert the string into a list of arguments !

并且无需从外部停止 ffmpeg(您的文件可能无法写入的另一个原因).使用内置命令行 option -t

And there is no need to stop ffmpeg from the outside (another reason why your file might not get written). Use the builtin command line option -t for that.

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)

这篇关于子进程不创建 ffmpeg 命令的输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆