About me

Music producer, (ex-)DJ, electronics tinkerer, Linux enthusiast and coder.

Infrared motion sensor camera with Raspberry Pi Zero W, part 2


I decided to start with something simple like watching live video from the Pi camera on the local network. Raspbian comes with the raspivid utility, often used to simply record or pass camera picture to the HDMI output, but it's suitable for much more than that, especially with VLC, a multipurpose video toolkit.

Connecting the camera is relatively easy with ZIF connectors (the flippy bastards, as someone called them) in both ends, but check out the instructions if you have trouble. Note that Pi Zero has a slightly different connector, so the Pi Camera will need an adapter cable unless you buy a bundle that comes with it.

For a headless system the vlc-nox package (or vlc-bin in more recent distributions) is sufficient, so install that if you don't need the full VLC desktop version:

sudo apt install vlc-nox

The camera module needs to be enabled, so enter the Pi configuration utility:

sudo raspi-config

Enable the camera, exit the utility and sudo reboot.

RTSP streaming

RTSP seems to be the fastest protocol on a local network. Launch this command to start the server:

raspivid -o - -t 0 -n -vf -hf -w 1920 -h 1080 -fps 25 -b 5000000 -g 125 --inline --profile high --level 4.2 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264

Here is an explanation of the video options:
-o -
Sets output to standard output instead of a file, to pipe the video to the VLC server.
-t 0
Disables the timeout which would otherwise set the length of a recorded video.
-n
Disables video output to the HDMI connector. Remove this if you want to see the video on a monitor connected to your Pi.
-vf -hf
Vertical and horizontal flip. Remove these if your camera is not mounted upside down like mine.
-w 1920 -h 1080 -fps 25
Width, height and frame rate. Lower as necessary, such as 1280×720. The maximum frame rate at 1080p is 30, but using a low resolution (640×480) allows rates up to 90!
-b 5000000
Sets video bitrate (in bits per second) to 5 Mbit/s, which is OK for basic 1080p, but adjust this as you see fit.
-g 125 --inline
Sets I frames (key frames) to be sent every 5 seconds (at 25 fps) and places inline picture header information in them. Lower values may improve picture quality when it contains lots of tiny details and movement but also need a bigger bitrate, so it's worth experimenting.
--profile high --level 4.2
Defines a profile for HD video and a level big enough for high bitrates. Change if your video needs to work with a less capable decoder, and don't forget to adjust resolution and bitrate accordingly.
Then launch a media player on another computer, tablet or phone. On PC I tested playback with VLC and mpv, and on Android I used MX Player. Open a network stream with the following address, replacing raspberrypi.local with whatever host name or IP address yours is using:

rtsp://raspberrypi.local:8554/

Stop the stream server by pressing Ctrl+C.

HTTP streaming

Unfortunately RTSP is not an easy option if you wish to make the video stream public on the Internet, so here's an alternative using HTTP, which only needs port 8080 on your router forwarded to the Pi to make the stream public:

raspivid -o - -t 0 -n -vf -hf -w 1920 -h 1080 -fps 25 -b 5000000 -g 125 --inline --profile high --level 4.2 | cvlc -vvv stream:///dev/stdin --sout '#standard{access=http{mime=video/mp2t},mux=ts,dst=:8080}' :demux=h264

The video options are the same as with RTSP, so just point your player to this address, once again replacing raspberrypi.local with the actual address (or public address, if you forwarded the port):

http://raspberrypi.local:8080/

Don't forget to disable the port forwarding after you're done testing. Otherwise a random visitor might find the open HTTP port and take a peek. If you wish to keep the port forwarding active, it's best to protect the video stream with a password like this (replace username and password with your own):

raspivid -o - -t 0 -n -vf -hf -w 1920 -h 1080 -fps 25 -b 5000000 -g 125 --inline --profile high --level 4.2 | cvlc -vvv stream:///dev/stdin --sout '#standard{access=http{user=username,pwd=password,mime=video/mp2t},mux=ts,dst=:8080}' :demux=h264

Some players probably don't support authentication, but at least VLC and mpv work fine when you place the login info in the stream URL:

http://username:password@raspberrypi.local:8080/

Note that the H.264 video data is wrapped in a MPEG transport stream (instead of the typical MPEG-4 container), so it won't work in HTML5 video elements. See this page to find out which container formats can be used in live streaming.

Some observations

The picture from the NoIR camera is quite reddish and trippy, but that was expected. Noise is acceptable even in low-light conditions.

The hardware H.264 encoder in the Pi GPU is truly a blessing, as the CPU usage of a Pi Zero stays around 10–20% during live streaming over network without even using the full 1 GHz clock speed, so that leaves plenty of CPU power for controlling other things simultaneously.

There is a delay of a second or so, which is probably caused by video encoding and decoding buffers. It's not bad, but I'll try to shorten it as I proceed to the more challenging phases of this project

0 comments:

Post a Comment