Installing a H264 webcam on a Raspberry Pi.
The following can be completed in 15-30 minutes...
Ingredients
- Memory Card for the Pi (e.g. 16Gb)
- Raspberry Pi 5
- USB Webcam
Setting up the Basics
Installing The Operating System
On a Windows or Linux PC, Run the Raspberry Pi Imager, and Select:
- Raspberry Pi 5
- Select Rasberry Pi OS (64-bit)
- Your memory card (e.g. 16Gb)
Edit Settings, and:
- Set the device name, e.g. 'camserver'
- Fill in your Username and Password (for this example, we assume the username is 'user')
- Fill in the WiFi SSID and Password
- Under 'Services', ensure that SSH is enabled, and select 'Use password authentication'
ssh -l user camserver.local
Disabling USB Auto Suspend and Swapping to Memory Card
$ sudo vi /boot/firmware/cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=ef0fb16a-02 rootfstype=ext4 fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles cfg80211.ieee80211_regdom=GB usbcore.autosuspend=-1
$ sudo systemctl disable dphys-swapfile.service
$ sync; sudo reboot
Setting up the Audio
Finding the Webcam Audio Stream
$ ssh -l user camserver.local
Run the lsusb command to identify the webcam. If it is not obvious which of the devices _is_ the webcam, unplug it and run lsusb again to see what is missing.
$ lsusb
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 002: ID 05a3:9331 ARC International Camera
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Now run the alsactl info command, and look for the device (which will have a matching ID - in this case, 05A3:9331). Note the card and device number.
$ alsactl info | more
...
#
# Sound card
#
- card: 2
id: Camera
name: HD Web Camera
longname: HD Web Camera HD Web Camera at usb-xhci-hcd.1-1, high speed
driver_name: USB-Audio
mixer_name: USB Mixer
components: USB 05a3:9331
controls_count: 3
pcm:
- stream: CAPTURE
devices:
- device: 0
id: USB Audio
name: USB Audio
subdevices:
- subdevice: 0
name: subdevice #0
...
The card and device number form the hardware ID, which is used in ffmpeg in later sections. In this example, the hardware ID is hw:2,0
Install icecast 2
$ sudo apt-get icecast2
Edit / create an alsa2icecast service, making sure that you set the correct address for the the device (in this case: hw:2,0).
$ sudo vi /etc/systemd/system/alsa2icecast.service
[Unit]Description=Alsa to Icecast2 StreamerAfter=network.target
[Service]
ExecStart=/bin/bash -c '\
ffmpeg -v fatal \
-nostdin \
-f alsa \
-thread_queue_size 512 \
-channels 1 \
-sample_rate 44100 \
-i hw:2,0 \
-filter:a "volume=18dB" \
-err_detect compliant \
-codec:a libmp3lame \
-b:a 256k \
-f mp3 \
icecast://source:source@127.0.0.1:8000/birdbox.mp3'
Restart=always
RestartSec=2
[Install]
WantedBy=multi-user.target
$ sudo systemctl start alsa2icecast$ sudo systemctl enable alsa2icecast


