I have the following ffmpeg command that works when I enter it into command prompt: ffmpeg -list_devices true -f dshow -i dummy
. I would like to convert this ffmpeg command into equivalent ffmpeg-python code. How would I do it? Also, this command only works in Windows: is there a way to convert this command into ffmpeg-python code that works across all operating systems? There is documentation about how to convert ffmpeg commands into ffmpeg-python code, but this command is different from the ones in the documentation. Any help would be greatly appreciated! Thanks!
Asked
Active
Viewed 601 times
0

watersheep23
- 339
- 1
- 3
- 17
-
Just so you are aware, all the `ffmpeg-python` module does is convert API calls into command-line invocations. If that's the call you want, you might as well use `subprocess` to call the command line. – Tim Roberts Jan 31 '22 at 05:02
-
So I would use ```subprocess```, however I am trying to find a way to convert the command into code that does not require the installation of ffmpeg. If I were to use ```subprocess```, I would have to still install ffmpeg for the code to work, right? As far as I'm aware, ```ffmpeg-python``` does not require the installation of ffmpeg. That is why I'm trying to convert the command into code using ```ffmpeg-python```. – watersheep23 Jan 31 '22 at 05:07
-
AFAIK, `ffmpeg-python` does require external python installation and both `run()` & `run_async()` calls in `ffmpeg-python` do call `subprocess.run()` & `.Popen()` respectively under the hood – kesh Jan 31 '22 at 16:55
-
Your ffmpeg call doesn't work in non-Windows because `-f dshow` is a Windows only device (DirectShow). You need to query for equivalent devices in Linux/Mac. Suggest reading up on FFmpeg's Devices Documentation and their related Wiki entries – kesh Jan 31 '22 at 16:58
-
According to this [post](https://stackoverflow.com/questions/70064040/ffmpeg-unexpected-exit-code-1-for-list-devices-and-list-options), you (probably) can't use `ffmpeg-python`. When `-list_devices` is `true`, FFmpeg ends with an error. The error raises an exception, and we can't read the output. I have tried: `try:` and `out = ffmpeg.output(ffmpeg.input('dummy', list_devices='true', f='dshow'), '').global_args('-hide_banner', '-report').run(capture_stderr=True)` and `except ffmpeg._run.Error:` and `pass`. The most I could get is the log file (it lists the devices, but it's "ugly"). – Rotem Jan 31 '22 at 17:55
-
*As far as I'm aware `ffmpeg-python` does not require the installation of ffmpeg* -- You would be wrong. `ffmpeg-python` assumes `ffmpeg` is already installed and on your path. – Tim Roberts Jan 31 '22 at 18:35
-
Thanks for correcting me @TimRoberts, that makes more sense. – watersheep23 Jan 31 '22 at 19:24
-
@watersheep23 or it would be a case of `fighting without fighting - Bruce Lee`. Although I'm unclear as to how good a python programmer he was. ;) – Rolf of Saxony Jan 31 '22 at 21:08
1 Answers
0
Ran into the same thing and my good friend ChatGPT proposed a solution that seems to work on my system:
import subprocess
from typing import Sequence
def get_video_input_devices_on_windows() -> Sequence[str]:
command = "ffmpeg -list_devices true -f dshow -i dummy"
try:
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).decode()
except subprocess.CalledProcessError as e:
output = e.output.decode()
video_devices = []
lines = output.split("\n")
for line in lines:
if "(video)" in line:
device_name = line.split('"')[1]
video_devices.append(device_name)
return video_devices
print(get_video_input_devices_windows())
On my system, this gives me:
['USB2.0 HD IR UVC WebCam', 'USB Video']
Which is correct. Note you'll have to have ffmpeg accessible from your path (e.g. in your current working directory).

Peter
- 12,274
- 9
- 71
- 86