1

I have a small python program that works very well to capture short videos from webcams in linux (at least for laptops that have built-in webcams) using a sub-process with ffmpeg.

Now i'm trying to write the same program to capture webcams in windows, and i know i can't use the generic "/dev/video0" that works pretty well in linux, but i thought something like naming it "Integrated Camera" should be enough, but it fails.

Here's my linux code (that works):

    import sys
    from subprocess import call
    from datetime import datetime
    def record_webcam(seconds):
        cam = '/dev/video0'
        timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')
        filename = timestamp + 'something.mkv' #generated with more complexity in the actual code, but that isn't important
        ffmpeg_cmd = 'ffmpeg -t {} -an -i {} -c:v libx264 -preset veryslow -crf 25 {}'.format(seconds, cam, filename).split()
        p = call(ffmpeg_cmd)
        return filename if p == 0 else False

    if __name__ == '__main__':
        record_webcam(sys.argv[1])

I have looked at the documentation for ffmpeg and tried to search for solution but so far i'm lost...

I know that "Integrated Camera"s are only available on some laptops and not others, and that it won't capture other cameras connected, but it's enough for my use case... but if you want a challenge I would also like to know how to apply it to any windows-pc with a camera regardless of what it's called.

Also, is it easier or more recommended to do what i'm trying here only with python tools, like OpenCV?

Thanks in advance! Edit: I answered my own question with a partial solution if anyone is interested based on a comment from @Mulvya, but if anyone can still explain to me the part about OpenCV I would still like to hear it...

Follow up question here: ffmpeg through python subprocess fails to find camera

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
  • On WIndows, you'll have to use either the [dshow](https://ffmpeg.org/ffmpeg-devices.html#dshow) or [gdigrab](https://ffmpeg.org/ffmpeg-devices.html#gdigrab) input device. – Gyan Jun 03 '17 at 20:01
  • tried it, but it fails to find "Integrated Camera" and i think there is also a problem with mixing it with libx264 encoding, but maybe i'm wrong about that. can you give an example of such a command that would work for any webcam? or at least *most* integrated ones? – Ofer Sadan Jun 03 '17 at 20:25
  • 2
    There's no universal command. Read the [examples](https://ffmpeg.org/ffmpeg-devices.html#Examples-2) at the linked docs. First you'll have to check the list of available devices, then select one and capture that. There's no issue with encoding using x264. Add `-preset ultrafast -tune zerolatency`. – Gyan Jun 03 '17 at 20:34
  • I added my own answer to the problem based on your comment... Thank you very much! – Ofer Sadan Jun 04 '17 at 08:24

1 Answers1

2

I did it... based on what @Mulvya commented, i was able to list all cameras withing the code and extract a command from them, i now have a different issue about that, but i will ask another question for it. In the meanwhile, if anyone is interested in automatically selecting the first available camera on windows through python and ffmpeg, my solution is this:

    import re
    from subprocess import Popen, PIPE
    list_cmd = 'ffmpeg -list_devices true -f dshow -i dummy'.split()
    p = Popen(list_cmd, stderr=PIPE)
    for line in iter(p.stderr.readline,''):
        if flagcam:
            cam = re.search('".*"',line.decode(encoding='UTF-8')).group(0)
            cam = 'video=' + cam if cam else ''
            flagcam = False
        elif 'DirectShow video devices'.encode(encoding='UTF-8') in line:
            flagcam = True
        elif 'Immediate exit requested'.encode(encoding='UTF-8') in line:
            break

the variable "cam" now holds the name of the cam as it is in DirectShow on windows

Follow up question here if anyone wants to help EDIT: also solved

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62