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'

Then select Install, and wait.

Booting the Pi

Connect a webcam (preferably one supporting H.264)
Insert the memory card into the Pi, and power up
The LED will flash Red, then Green, and after that will blink green every time the memory card is accessed.
If the LED remains Red, this indicates that your power supply cannot provide sufficient current.

Logging In
ssh -l user camserver.local

Disabling USB Auto Suspend and Swapping to Memory Card


Modify the cmdline.txt to add an additional switch to the end, use systemctl to disable the swapfile and then reboot the device:

$ 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  Icecast2
Install icecast 2 
$ sudo apt-get icecast2 
Add an Audio Streaming Service

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 Streamer
After=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
Once created, start and enable the service.

$ sudo systemctl start alsa2icecast
$ sudo systemctl enable alsa2icecast

Configure Icecast2


Setting up the Video

Installing Motion

...