• Linux
  • FreeBSD
  • Networking
  • Python
  • AWS
  • WebDev
  • About Us
Tech Tips and Tutorials
Menu
  • Linux
  • FreeBSD
  • Networking
  • Python
  • AWS
  • WebDev
  • About Us
Home  /  Linux  /  How to Live stream a camera to Youtube using FFMPEG in Raspberry pi 4

How to Live stream a camera to Youtube using FFMPEG in Raspberry pi 4

Written by
Linux Leave a Comment

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

Share On
Share on Facebook
Share on Twitter
Share on Google+
Share on Whatsapp

 Previous Article FFMPEG error : Port missing in uri : Invalid argument
Next Article   How to use nohup to run a command in the background

Related Posts

  • Linux Shell Scripts for System Administrators

  • SOCKS PROXY

  • How to grep IP addresses from log files in Linux

Leave a Reply

Cancel reply

  • Popular Posts
  • Recent Posts
  • Linux Shell Scripts for System Administrators
  • Cacti 1.1.38 ERROR Opening .rrd No Such file or directory
  • Apache configuration for Cacti
  • How to create RAM disk in Ubuntu 18.04
  • Linux Shell Scripts for System Administrators
  • SOCKS PROXY
  • How to install XAMPP and access phpMyAdmin in MAC OS
  • How to increase the root volume in a Linux ec2 instance

Linux

  • Linux Shell Scripts for System Administrators
  • SOCKS PROXY
  • How to grep IP addresses from log files in Linux
  • How to empty or delete a large file in Linux

Categories

  • AWS (5)
  • FreeBSD (12)
  • Linux (29)
  • Networking (3)
  • Python (5)
  • Uncategorized (4)
  • WebDev (1)
  • Connect on Facebook
  • Connect on YouTube
© Copyright 2020.TechieNix. All Rights Reserved.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Non-necessary

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.