且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ffmpeg通过python子进程找不到相机

更新时间:2023-11-18 16:15:58

问题是,在命令行中, video =Lenovo EasyCamera您可以使用测试的python文件查看这个空格:

  import sys 
print(sys.argv [1:])





 > python print_argv.py video =Lenovo EasyCamera
['video = Lenovo EasyCamera']
> python print_argv.pyvideo = Lenovo EasyCamera
['video = Lenovo EasyCamera']
> python
>>>从子进程导入Popen
>>> cmd = ['python','print_argv.py','video =Lenovo EasyCamera']
>>> p = Popen(cmd)
['video =Lenovo EasyCamera'

ffmpeg认为您正在寻找名为Lenovo EasyCamera而不是 Lenovo EasyCamera

$ b $的设备b

因此,您需要更改命令,使其不在引号中,因为 Popen 将不会在空格上拆分。

 从子进程导入Popen 
cmd = [...,'-i','video = Lenovo EasyCamera',... ]
p = Popen(cmd)


Weird problem here, i use this command to capture my webcam through ffmpeg (through cmd on windows):

ffmpeg -y -t 300 -rtbufsize 1024M -f dshow -i video="Lenovo EasyCamera" -c:v libx264 -preset veryslow -crf 25 Desktop.mkv

and everything works fine. But when i try the very same command through python as a subprocess it fails. Here's the python code:

from subprocess import Popen
cmd = ['ffmpeg', '-y', '-t', '300', '-rtbufsize', '1024M', '-f', 'dshow', '-i', 'video="Lenovo EasyCamera"', '-c:v', 'libx264', '-preset', 'veryslow', '-crf', '25', 'Desktop.mkv']
p = Popen(cmd)

Outputs the following error and freezes:

[dshow @ 00000000023a2cc0] Could not find video device with name ["Lenovo EasyCamera"] among source devices of type video.
video="Lenovo EasyCamera": I/O error

Can anyone figure this out and tell me what i'm doing wrong? Or is it some known bug in python or the subprocess module (using python 3.6.1, but not attached to the specific version if it will help me solve this problem)?

Thanks in advance!

P.S. This question is a follow up to this one, if that's relevant: How to grab laptop webcam video with ffmpeg in windows

The problem is that, in the commandline, video="Lenovo EasyCamera" uses the quotes to make sure the space doesn't make it another argument.

You can see this with a test python file:

import sys
print(sys.argv[1:])

> python print_argv.py video="Lenovo EasyCamera"
['video=Lenovo EasyCamera']
> python print_argv.py "video=Lenovo EasyCamera"
['video=Lenovo EasyCamera']
> python
>>> from subprocess import Popen
>>> cmd = ['python', 'print_argv.py', 'video="Lenovo EasyCamera"']
>>> p = Popen(cmd)
['video="Lenovo EasyCamera"']

ffmpeg thinks you're looking for a device called "Lenovo EasyCamera" instead of Lenovo EasyCamera

So, you need to change your command so that it is not in quotes, as Popen will not split it on spaces.

from subprocess import Popen
cmd = [..., '-i', 'video=Lenovo EasyCamera', ...]
p = Popen(cmd)