Stream Video From Raspberry Pi to an RTMP Server

Enable the Camera with raspi-config

I wanted to do some testing with video streaming from my Raspberry Pi to RTMP output. This is what most streaming platforms, such as Twitch and YouTube provide to send streams there.

I didn’t want to send my stream to a public endpoint, so I used Cloudflare Stream.

Enable Camera Support on the Pi

I assume that Raspberry Pi OS is used (the light version works fine).

First off, execute raspi-config in a Terminal window and navigate to Interface Options, then hit return, select Camera and enable it. Afterwards the device might require a reboot.

Next, install ffmpeg:

sudo apt-get install ffmpeg

If you want to find out the capabilities of your camera/video module, such as frame rate, video modes and resolution etc., you might want to use v4l, or Video4Linux to get the details.

Stream the Video

raspivid -t 0 -w 1280 -h 720 -fps 30 -o - | ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i pipe:0 -c:v copy -c:a aac -strict experimental -f flv rtmps://example.com:443/live/

raspivid is the tool to interact with the Raspberry Pi Camera module. -t specifies the timeout after the video capture will stop, with 0 it'll stream continuously. -w and -h takes the resolution you want to stream at, and -fps, well, the frames per second. With -o - the file will be sent to stdout instead of a file, so we can pick it up with ffmpeg in the next step.

-i anullsrc=channel_layout=stereo:sample_rate=44100 in the ffmpeg command will create silent audio output.