Skip to content

Instantly share code, notes, and snippets.

@brutella
Last active February 9, 2024 09:48
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brutella/a71e8d7aa90af2c53ab500c4125bc178 to your computer and use it in GitHub Desktop.
Save brutella/a71e8d7aa90af2c53ab500c4125bc178 to your computer and use it in GitHub Desktop.
How to enable the camera module on a Raspberry Pi

Enable camera module

Edit your /boot/config.txt file and make sure the following lines look like this:

start_x=1             # essential
gpu_mem=128           # at least, or maybe more if you wish
disable_camera_led=1  # optional, if you don't want the led to glow

Load bcm2835-v4l2 module

sudo modprobe bcm2835-v4l2

or add bcm2835-v4l2 to /etc/modules.

Create loopback device

The video device /dev/video0 can only be accessed by one process at a time. Use a loopback device to allow mutli process access at /dev/video1. https://raspberrypi.stackexchange.com/a/19897

Install v4l2loopback

# Install kernel headers
# Note: Kernel headers may not be up-to-date. 
# To downgrade the firmware to a specific firmware use sudo rpi-update <commit hash from https://github.com/Hexxeh/rpi-firmware>
sudo apt-get install raspberrypi-kernel-headers

# Download source and install
git clone https://github.com/umlaeute/v4l2loopback
cd v4l2loopback
make
sudo make install

Load v4l2loopback module via

sudo modprobe v4l2loopback video_nr=1 # creates /dev/video1
# sudo modprobe v4l2loopback devices=1 

If you want to load the module on startup add v4l2loopback to /etc/modules and the options to /etc/modprobe.d/v4l2loopback.conf. https://askubuntu.com/a/822136

# /etc/modprobe.d/v4l2loopback.conf
options v4l2loopback video_nr=1

Then copy the video from /dev/video0 to /dev/video1.

ffmpeg -f video4linux2 -i /dev/video0 -vcodec copy -f v4l2 /dev/video1
@XavierColombel
Copy link

XavierColombel commented May 20, 2022

I've an error on Raspberry Pi OS (Bulleye).

When I type:

sudo modprobe v4l2loopback video_nr=1 # creates /dev/video1
# sudo modprobe v4l2loopback devices=1 

I've got:

pi@raspberrypi:~/Desktop/v4l2loopback $ sudo modprobe v4l2loopback video_nr=1
modprobe: FATAL: Module v4l2loopback not found in directory /lib/modules/5.15.32-v7l+
pi@raspberrypi:~/Desktop/v4l2loopback $ sudo modprobe v4l2loopback devices=1 
modprobe: FATAL: Module v4l2loopback not found in directory /lib/modules/5.15.32-v7l+

@clippy20
Copy link

clippy20 commented Jun 9, 2022

Try installing v4l2loopback-dkms. That seemed to have resolved the issue for me.

sudo apt install v4l2loopback-dkms

@douglassllc
Copy link

Raspberry Pi OS (Bullseye) 32 bit
Using libcamera, not the legacy camera support
Pi 4 with a Camera V3

I have installed v4l2loopback via apt install v4l2loopback-dkms
When I run ffmpeg -f video4linux2 -i /dev/video0 -vcodec copy -f v4l2 /dev/video1 I receive the following error:

[video4linux2,v4l2 @ 0x1d89cf0] ioctl(VIDIOC_G_PARM): Inappropriate ioctl for device
[video4linux2,v4l2 @ 0x1d89cf0] Time per frame unknown
[video4linux2,v4l2 @ 0x1d89cf0] ioctl(VIDIOC_STREAMON): Invalid argument

Any recommendations on resolving?

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