Skip to content

Instantly share code, notes, and snippets.

@keilmillerjr
Last active February 19, 2024 07:51
Show Gist options
  • Save keilmillerjr/e414a2846d6ec9aeb598775c91c2f472 to your computer and use it in GitHub Desktop.
Save keilmillerjr/e414a2846d6ec9aeb598775c91c2f472 to your computer and use it in GitHub Desktop.
Using DSLR as a webcam on Arch/Manjaro Linux

Using DSLR as a webcam on Arch/Manjaro Linux

Many thanks to Ben Chapman and his blog post How to Use Your DSLR Camera as a Webcam in Linux.

v4l2loopback is a linux kernel module that allows you to create "virtual video devices". Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system.

Compatibility

Supported Cameras

This guide was created using the following:

Operating System Kernel Camera
Arch Linux x86_64 5.17.9-arch1-1 Cannon EOS 1500D (Rebel T7)

All commands are intended to be used with Arch Linux. Commands related to packages will have to be altered if using a different distrobution.

Installation

  1. Install kernel headers.
    • $ sudo pacman -S "linux$(uname -r | awk -F. '{print $1$2}')-headers"
  2. Install dependancies.
    • $ sudo pacman -S gphoto2 v4l-utils v4l2loopback-dkms ffmpeg

Load v4l2loopback kernel module

Test v4l2loopback kernel module by loading it upon demand.

  1. Load v4l2loopback kernel module.
    • $ sudo modprobe v4l2loopback exclusive_caps=1 max_buffers=2
  2. View all loaded kernel modules and confirm v4l2loopback is loaded.
    1. $ modinfo v4l2loopback
    2. $ lsmod

Automatically load v4l2loopback kernel module

$ sudo nano /etc/modules-load.d/modules.conf                                    
# List of modules to load at boot
dslr-webcam
$ sudo nano /etc/modprobe.d/v4l2loopback.conf
# Module options for Video4Linux, needed for our DSLR Webcam
alias dslr-webcam v4l2loopback
options v4l2loopback exclusive_caps=1 max_buffers=2

gphoto2 testing

Connect camera vis usb and turn camera on. Change the following settings on the camera (Canon EOS Rebel T7), else it will not connect to PC via USB.

  • Auto power off > Disable
  • Wi-Fi/NFC > Disable
$ lsusb
$ gphoto2 --auto-detect
$ gphoto2 --summary`
$ gphoto2 --abilities`

If connection is verified via previous commands, try taking an image and saving it to your computer.

$ cd ~
$ gphoto2 --capture-image-and-download

Webcam usage

$ gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0

Stream can now be reconized by various apps, such as Cheese, OBS Studio, QT V4L2 test Utility, Shotcut, VLC media playter, etc. ctrl + c will end the stream.

If you obtain an Error (-53: 'Could not claim the USB device') from gphoto, killing all gphoto processes will resolve the issue. Use $ pkill -f gphoto2.

You can create an alias to so you do not have to copy and paste the command each time. Bash users can add the following to ~/.bashrc, ZSH .zshrc, and oh-myzsh ~/.oh-my-zsh/custom/alias.zsh (or whichever custom file you create).

alias start_dslr='pkill -f gphoto2 && gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0'

Restarting your terminal session will include alias. You can then start the stream with $ start_dslr.

@djhunter67
Copy link

Excellent work. I found this page from a google search, FYI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment