Live streaming camera footage, such as watching pets or observing a bird’s nest, has become a popular hobby. Platforms like YouTube and Facebook allow anyone to view these streams from anywhere in the world, often driving significant traffic to your channel. In this guide, we’ll explore how to set up a live stream using a Raspberry Pi.
For this project, I’m using a Raspberry Pi 4, an IP camera, and a YouTube channel for live streaming. Start by flashing the Raspberry Pi SD card with the latest Raspbian Buster image, which can be downloaded from the official Raspberry Pi website.
Camera Setup
Lets start with the camera , you need to set up the camera on your local network . You can access your camera from a HTTP webpage for live viewing but that’s not enough for our streaming job. So, the camera uses a remote streaming protocol called RSTP which listens on port 554 . The port might be different on various cameras but you can get it from your cameras network settings . So a typical rstp link with camera username , password , IP looks as below :
rtsp://cam_username:cam_password@cam_IP:554/live.sdp
If you look at the above link , after port 554 you can see a string ‘live.sdp’. This is different for different cameras . The one I used ‘live.sdp’ is a standard string for Honeywell cameras . You can see this from your camera settings . So now we need to test this rstp link but you need an rstp player for this. The easily available rstp player is a VLC player . Open your VLC player , click on Media and click on Open Network stream . You can now provide your rstp link and you should now see your live cam feed here. If the live feed not working , you have some issues locally and first need to troubleshoot it .
Now that you have the RSTP working .But RSTP is not directly supported by YouTube and Youtube uses a protocol called RTMP(Real-Time Messaging Protocol). Here you need an encoder , so the encoder acts as a middle software to encode the live stream as per YouTubes’s streaming format and then feed it to Youtube . There are different encoders available , if you check YouTubes streaming help link , you will see lot of YouTube reccomended options . But FFMEPG is a popular enocoder/decoder available for Linux distros .
FFMPEG
FFMPEG is a multimedia framework which is used for various audio/video encoding , conversion etc.FFMPEG relies on a number of dependencies . So installing it on Linux was really tough in the beginning . If you search about it , you will see lots of tutorials about compiling it manually with the required dependencies . But because of its popularity and usefulness , this is now well packed with all the required dependecies and readily available via the latest Linux repositories . So installing this on the latest Raspberry pi Linux buster is very straight forward:
apt-get update
apt-get install ffmpeg
Once installed you can verify it from :
ffmpeg -version
So ffmpeg is succesfully installed now . Now we need to get the YouTube ready to accept the stream .You need a YouTube channel for this .
YouTube Setup
Login to YouTube studio from studio.youtube.com. click on create and go live.
Once you click on that it will take you to the streaming window , from here you need to copy the stream URL and stream key . You need to use them with the ffmpeg command .
Running ffmpeg
Now go back to your raspberry pi , and run the basic ffmpeg command
ffmpeg rtsp://cam_username:cam_password@cam_ip/live.sdp -f flv rtmp://a.rtmp.youtube.com/live2/youtube_stream_key
The above is just a basic ffmpeg command , by default it uses UDP and also uses all the default encoders and for tcp use the command below :
ffmpeg -rtsp_transport tcp rtsp://cam_username:cam_password@cam_ip/live.sdp -f flv rtmp://a.rtmp.youtube.com/live2/youtube_stream_key
You can use more custom filters like scale, encoder type, pix format etc with ffmpeg . YouTube support link is really helpful in uderstanding this .The below command is tested and working ,you can change the udp to tcp if you need, but udp is recommended for Live streams as its a connectionless protocol and doesn’t need TCP/IP acknowlgements which makes it faster .
ffmpeg -f lavfi -i anullsrc -rtsp_transport udp -i rtsp://cam_username:cam_password@cam_ip:554/live.sdp -vf scale=1280:720 -reorder_queue_size 4000 -max_delay 10000000 -vcodec h264_omx -b:v 4500k -pix_fmt yuv420p -f flv rtmp://a.rtmp.youtube.com/live2/stream_key
You can always try tuning your ffmpeg with the filters you need . Some of the filters used in the above command are explained below: the wiki ffmpeg page will be helpful .
-b:v target bit rate , adjusting this depends upon your upload speed
scale: is the resolution :: 1280X720 is for 720p@60fps
vcodec: is the enoder type , you can change this to other encoders like libx264, Lavf58.20 etc
-pix_fmt - pixel format
You can also live stream a video file thats already on your pi by using the command below:
ffmpeg -re -i /home/pi/myvideo.mp4 -vf scale=2560:1440 -c:v libx264 -b:v 2M -c:a copy -strict -2 -flags +global_header -bsf:a aac_adtstoasc -bufsize 2100k -f flv rtmp://a.rtmp.youtube.com/live2/xxxxxxxx--xxxxxxxxxx--xxx
Common Errors
Most of the issues can be solved by tuning your ffmpeg command . If you are using the UDP and started seeing lots of packet loss as below, your pi is struggling to cope with the packet transfer rate . You need to check your camera s video settings as well , you can tweak the settings by reducing the pixel rates, changing the HD formats etc . If are using a high end camera with high mega pixel, may be pi isn’t sufficient for you . You should think about upgrading to a more capable hardware as the encoding and all requires more processing capability .
[rtsp @ 0x1e361c0] max delay reached. need to consume packetbitrate=2911.7kbits/s dup=0 drop=5 speed=0.211x
[rtsp @ 0x1e361c0] RTP: missed 126 packets
[rtsp @ 0x1e361c0] max delay reached. need to consume packetbitrate=1184.8kbits/s dup=0 drop=5 speed=0.512x
[rtsp @ 0x1e361c0] RTP: missed 49 packets
Another popular error with ffmpeg is addressed here
Leave a Reply