Skip to content

Instantly share code, notes, and snippets.

@kode54
Last active October 21, 2020 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kode54/36c007a1c4f55f07247443d3484b453d to your computer and use it in GitHub Desktop.
Save kode54/36c007a1c4f55f07247443d3484b453d to your computer and use it in GitHub Desktop.
Quick and dirty fix for Linux apps that don't natively support webcam formats other than raw pixels

This is a simple setup. Basically, you just need to install v4l2loopback for your kernel, sometimes a DKMS module.

Run this to load it manually, if it isn't automatically loaded and configured in your /etc settings:

sudo modprobe v4l2loopback exclusive_caps=1 card_label=<camera label>

For camera label, I went with "camerafun" but any alphanumeric string will work. This will be the name of the video source.

Then you run a script based on this to pipe from your camera's video device into the loopback, decoding it from whatever native format it is into raw video.

#!/bin/sh

ffmpeg -f v4l2 -input_format mjpeg -framerate 15 -video_size 1920x1080 -i /dev/video0 -c:v rawvideo -pix_fmt yuyv422 -f v4l2 /dev/video2 &> /dev/null

Adjust mjpeg, video_size, and framerate to match your camera's preferred format for the resolution you want. Also adjust the /dev/video paths to match your camera (video0 above) and the loopback (video2 above).

To find which formats your camera supports, use the following:

v4l2-ctl --list-formats-ext

For some reason, this only lists the formats and not which devices they're associated with here, so use the following to identify your devices:

v4l2-ctl --list-devices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment