Python/FFMPEG 命令行问题
我在从 Python 脚本中运行 FFMPEG 命令时遇到问题。当我从终端运行以下命令时,我可以从连接的网络摄像头 (Logitech C310) 流式传输视频和音频,并输出到文件“out.avi”,没有任何错误。
ffmpeg -f alsa -i default -itsoffset 00:00:00 -f video4linux2 -s 1280x720 -r 25 -i /dev/video0 out.avi
当我在下面的 Python 脚本中运行相同的命令时,
def call_command(command):
subprocess.Popen(command.split(' '))
call_command("ffmpeg -f alsa -i default -itsoffset 00:00:00 -f video4linux2 -s 1280x720 -r 25 -i /dev/video0 out.avi")
它给出了错误:
Input #0, alsa, from 'default':
Duration: N/A, start: 1317762562.695397, bitrate: N/A
Stream #0.0: Audio: pcm_s16le, 44100 Hz, 1 channels, s16, 705 kb/s
[video4linux2 @ 0x165eb10]Cannot find a proper format for codec_id 0, pix_fmt -1.
/dev/video0: Input/output error
有人能解释一下这里可能发生的情况吗?我尝试过使用 os.system() 以及 subprocess.call() ,但它给了我同样的错误。我不知道从哪里开始了解这里可能出现的问题。我尝试搜索“video4linux2 Cannot find aproper format for codec_id 0, pix_fmt -1”错误,但找不到任何一致的内容。
我还尝试将“ffmpeg -f...”命令放入 shell 脚本“test.sh”中,并赋予其可执行权限。然后我打开终端,并运行“./test.sh”,它就可以工作了。当我尝试从 Python 脚本中调用命令“./test.sh”时,我仍然遇到像以前一样的原始错误。这是我在 test.sh 脚本中尝试的 Python 命令:
subprocess.call(["./test.sh"])
I have a problem with running an FFMPEG command from within a Python script. When I run the following command from the terminal, I can stream video and audio from my attached webcam (Logitech C310) and output to file "out.avi" without any errors.
ffmpeg -f alsa -i default -itsoffset 00:00:00 -f video4linux2 -s 1280x720 -r 25 -i /dev/video0 out.avi
When I run the same command in a Python script below,
def call_command(command):
subprocess.Popen(command.split(' '))
call_command("ffmpeg -f alsa -i default -itsoffset 00:00:00 -f video4linux2 -s 1280x720 -r 25 -i /dev/video0 out.avi")
it gives me the error:
Input #0, alsa, from 'default':
Duration: N/A, start: 1317762562.695397, bitrate: N/A
Stream #0.0: Audio: pcm_s16le, 44100 Hz, 1 channels, s16, 705 kb/s
[video4linux2 @ 0x165eb10]Cannot find a proper format for codec_id 0, pix_fmt -1.
/dev/video0: Input/output error
Could anyone shed some light on what could be going on here? I've tried using os.system() as well as subprocess.call() and it gives me the same errors. I'm not sure where to start on what could be going wrong here. I tried searching for the "video4linux2 Cannot find a proper format for codec_id 0, pix_fmt -1" error, but couldn't find anything consistent.
I've also tried putting the "ffmpeg -f..." command in a shell script "test.sh", and giving it executable permissions. I then open terminal, and run "./test.sh", and it works. When I try calling the command "./test.sh" from within my Python script, I'm left with the original error as before. This is the Python command I tried with the test.sh script:
subprocess.call(["./test.sh"])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经解决了这个问题。在我的 Python 脚本中,我使用 OpenCV 显示这些帧并使用 ffmpeg 记录它们。尝试运行 ffmpeg 命令并使用 OpenCV 将它们显示在屏幕上时出现冲突。
更具体地说,在创建 OpenCV CreateCameraCapture 对象时:
注释掉该行代码可以解决我的问题。 Python 和执行命令没有任何问题。
I have fixed the issue. In my Python script, I'm using OpenCV to display these frames as well as record them using ffmpeg. There is a conflict when trying to run the ffmpeg command and display them on the screen using OpenCV.
More specifically, when creating a OpenCV CreateCameraCapture object:
Commenting out that line of code fixes my problem. There aren't any issues with Python and executing commands.
您应该尝试使用 shell=True 参数运行 Popen。
You should try running Popen with shell=True argument.
在大多数情况下,
Shell =True
并不是一个好的选择。如果您需要管道作为输出,它不起作用。
使用
它为我解决了问题。
Shell =True
is not a good option in most cases.It do not works if you need pipe as output.
Use
It solves the problem for me.