archive2021

raspistill is gone in Bullseye: moving camera scripts to libcamera-apps

Bullseye replaces the old Raspberry Pi camera stack with libcamera. How the new commands map to raspistill and raspivid, how to stream to ffmpeg, what breaks (picamera, /dev/video0), and when to stay on Buster.

Raspberry Pi OS moved to Debian 11 “Bullseye” in early November 2021 (CNX Software report). For me the biggest change is the camera. The closed-source camera stack behind raspistill, raspivid and the Python picamera library is replaced by libcamera, an open source camera framework on top of normal Linux drivers. Raspberry Pi explained the change in a separate post on 17 November (Raspberry Pi blog).

I have two Pis with camera modules. One records short clips when there is motion, one makes a snapshot every few minutes. Both use raspivid or raspistill in shell scripts. I flashed Bullseye on a spare SD card to see what has to change before I move them.

The short version

  • raspistill, raspivid and raspiyuv are not there. The new tools are libcamera-hello, libcamera-jpeg, libcamera-still, libcamera-vid and libcamera-raw. They are installed by default.
  • The picamera Python library does not work with the new stack. There is no Python interface for libcamera yet. Raspberry Pi says it is working on Picamera2, with no date.
  • The old stack can be turned back on, but the Raspberry Pi team presents this as a temporary way out, not something to build on.
  • Buster is still available for download. For a camera project that works today, staying on Buster is a valid choice.

Old commands to new commands

Old New Notes
raspistill -o a.jpg libcamera-still -o a.jpg libcamera-jpeg is a simpler version
raspivid -t 10000 -o a.h264 libcamera-vid -t 10000 -o a.h264 raw H.264, same as before
raspistill -t 0 (preview only) libcamera-hello -t 0 good first test that the camera is found
-w 1280 -h 720 --width 1280 --height 720 long options; check --help for short forms
-n (no preview) -n or --nopreview

Many options have the same idea but a different name. I did not find a full mapping, so I went through libcamera-still --help and libcamera-vid --help for each option in my scripts.

A first test:

libcamera-hello -t 5000
libcamera-still -n -o /tmp/test.jpg --width 1920 --height 1080

If libcamera-hello says no cameras are available, check the ribbon cable first, then /boot/config.txt. Bullseye has camera_auto_detect=1 there, which loads the right driver for official camera modules. The old start_x=1 line from Buster belongs to the legacy stack. If you copied your old config.txt over, it can be the reason the new tools do not see the camera.

My snapshot script

Before:

raspistill -n -w 1280 -h 720 -q 85 -o /var/lib/cam/latest.jpg

After:

libcamera-still -n --width 1280 --height 720 -q 85 \
  --immediate -o /var/lib/cam/latest.jpg

Without --immediate, libcamera-still waits for a timeout (five seconds by default) before it takes the picture, so auto exposure can settle. For a still every few minutes the wait does not matter. If you run it often, -t 1000 is a middle way. I have not tested how good the exposure is with a short timeout in low light.

Video to ffmpeg

libcamera-vid can write H.264 to stdout. This is the easiest way to keep using ffmpeg for segmenting or for sending to an RTSP server:

libcamera-vid -t 0 -n --inline --width 1280 --height 720 --framerate 25 -o - \
  | ffmpeg -f h264 -framerate 25 -i - -c:v copy \
      -f segment -segment_time 300 -reset_timestamps 1 -strftime 1 \
      /var/lib/cam/%Y%m%d-%H%M%S.mp4

--inline repeats the SPS/PPS headers with every key frame. Without it, a reader that starts in the middle of the stream cannot decode it. ffmpeg needs -f h264 because a raw stream from a pipe has no container to detect, and -framerate because raw H.264 has no reliable timing. The segment options are the same as in my ffmpeg camera notes.

libcamera-vid can also listen on a TCP port itself:

libcamera-vid -t 0 -n --inline --listen -o tcp://0.0.0.0:8888

Then ffplay tcp://raspberrypi.local:8888 on another machine shows the stream. I only use this for a quick test of focus and framing. For anything that runs longer, or for more than one viewer, I would put a real streaming server between the camera and the viewers.

What breaks

picamera. Any Python script that does import picamera fails on Bullseye with the new stack. My motion script uses picamera to get frames into OpenCV. The options I see now:

  • Stay on Buster for this Pi.
  • Read frames from libcamera-vid through a pipe (raw YUV with --codec yuv420) and decode them in Python with numpy. I tried this briefly. It works, but you have to calculate the frame size yourself and handle the stride, and I do not trust my version yet.
  • Wait for Picamera2.

OpenCV VideoCapture(0). On Buster with the bcm2835-v4l2 module, /dev/video0 gave frames directly, and cv2.VideoCapture(0) worked. With libcamera, there are still /dev/video* devices, but they are the low-level sensor and ISP nodes. Opening them like a webcam does not give a usable picture. A USB webcam is not affected; it still works through V4L2 as before.

Old tutorials. Most guides online use raspistill and picamera. For a while, every search result will be for the old stack. Check the date of a guide before following it.

Desktop preview over VNC or without a monitor. The preview window uses the new KMS display driver. On a headless Pi I use -n everywhere and do not miss it.

Upgrade in place or fresh install

The advice I read from the Raspberry Pi side is to do a fresh install, not to upgrade Buster to Bullseye with apt. There are many changes beyond the camera (the display driver, the desktop, network configuration on the desktop). For my camera Pis I will do a fresh install on a new card and keep the old card as a fallback, the same way as for any OS move.

What I decided

The snapshot Pi moves to Bullseye. Its script changed by one line, and that was the whole work.

The motion Pi stays on Buster for now. Its code depends on picamera, it works, and Buster gets security updates as Debian oldstable for some time. I will look at it again when Picamera2 has a first release. I think the new stack is the right direction, because it is open source and uses standard Linux interfaces, but for now it is a step back for Python users.