4

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

Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62

2 Answers2

6

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)
Artyer
  • 31,034
  • 3
  • 47
  • 75
0

I meet the same issue with error:

Input #0, dshow, from 'video=Webcam C170':
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, 30 fps, 30 tbr, 10000k tbn, 10000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> mjpeg (native))
Press [q] to stop, [?] for help
video=Webcam C170: I/O error
[swscaler @ 0000024a6b9a03c0] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'target.jpg' 

it seems that is not caused by pythin invoking issue. I tried to use os.system(cmd) or subprocess.call(cmd) or os.Popen(cmd) with cmd = 'ffmpeg -f dshow -i video="Webcam C170" -frames 1 target.jpg' or cmd = ['ffmpeg', '-f', 'dshow', '-i', 'video=Webcam C170', '-frames', '1', target] all get the error - video=Webcam C170: I/O error

even if I use os.system("get_image.bat") to invoke a dos batch script with content ffmpeg -f dshow -i video="Webcam C170" -frames 1 target.jpg it still failed with the same error video=Webcam C170: I/O error

It can only succeed when I type this script in dos window with script as following: ffmpeg -f dshow -i video="Webcam C170" -frames 1 target.jpg

Please share your experience on such kind of issue resolving if you have?

neil2021
  • 1
  • 1
  • `from subprocess import Popen, call cmd = ['ffmpeg', '-f', 'dshow', '-i', 'video=Webcam C170', '-frames', '1', "target2.jpg"] call(cmd) Popen(cmd) ` does not work also with error: video=Webcam C170: I/O error – neil2021 Aug 04 '21 at 08:13