PDA

View Full Version : Stream to YouTube Live with a Raspberry Pi and FFmpeg



-jeff
01-21-2024, 05:15 PM
This tutorial will explain how to set up a inexpensive Raspberry Pi to convert the RTSP stream from a common security camera to a RTMP stream required by YouTube. This will show the minimum required to make it happen.


You will need a Raspberry Pi 3B or 3B+ with the Raspberry Pi Legacy OS (Legacy 32-bit) installed. This is the full version that includes a desktop environment and FFmpeg already installed. You will need a good quality micro SD card to hold the operating system. I like the Samsung EVO Plus 32gb. You will also need a 5 volt power supply with a USB micro-B connector and capable of a continuous output of two amps.


We will compose a FFmpeg script to stream to YouTube. But first, a little about FFmpeg. From the FFmpeg web site, “A complete, cross-platform solution to record, convert and stream audio and video”. That pretty much sums it up. For more information about FFmpeg see: http://ffmpeg.org


So, let's get started. Install a micro SD card with the OS written to it and connect your raspberry pi to a monitor, mouse and keyboard. If this is the first boot, answer the initial questions based on your location etc and allow the raspberry to update the software as requested. When the update is completed the raspberry pi will reboot to the desktop environment.


For this tutorial we will use the desktop environment to set up the YouTube livestream. This can also be accomplished by using SSH to log into the raspberry pi command line without the desktop environment.


The information required for this FFmpeg script is:


Camera IP address: For this tutorial we will use 192.168.1.23:554
Camera user name: For this tutorial we will use “admin”
Camera password: For this tutorial we will use “ABC123”
YouTube Live URL: rtmp://a.rtmp.youtube.com/live2
YouTube Live stream key: 1234-1234-1234-1234-1234


Here is the FFmpeg script we will use:


/usr/bin/ffmpeg -use_wallclock_as_timestamps 1 -f lavfi -i anullsrc -rtsp_transport tcp -i rtsp://admin:ABC123@192.168.1.23:554/ -tune zerolatency -c:v copy -c:a aac -strict experimental -loglevel debug -f flv rtmp://a.rtmp.youtube.com/live2/1234-1234-1234-1234-1234


A little explanation about the script components:


/usr/bin/: This is the location of FFmpeg on your raspberry pi.
ffmpeg: This is the command for the FFmpeg utility, which is used for video and audio processing.

-use_wallclock_as_timestamps: This option tells FFmpeg to use the system wall clock time as timestamps for the input video. It ensures that the timestamps are synchronized with real-time.

-f lavfi -i anullsrc: This part specifies the input audio source using the lavfi filter. It generates silent audio using the anullsrc filter, which is useful when you want to stream video without audio. YouTube Live requires an audio stream to be present. If you do not have an audio stream, this will make YouTube Live happy. If you have camera audio you can leave this command out.

-rtsp_transport tcp -i rtsp://user-name:password@192.168.1.23:554: This segment specifies the input video source. It uses the Real-Time Streaming Protocol (RTSP) to access a video stream. The -rtsp_transport tcp option indicates that RTSP should be used with TCP as the transport protocol. It then specifies the RTSP URL with the username, password, IP address (192.168.1.23), port (554). Your camera may have additional characters after the port number. You can confirm your RTSP address is valid by entering it in VLC. If it works there, it will work for this script.

-tune zerolatency: This option is used to optimize the encoding for low-latency streaming. It's suitable for live streaming scenarios where minimizing delay is important.

-c:v copy: This specifies the video codec to be used. In this case, it's set to "copy," which means FFmpeg will not re-encode the video. It will simply copy the video stream as it is.

-c:a aac -strict experimental: These options specify the audio codec to be used, which is AAC. The -strict experimental flag allows FFmpeg to use experimental AAC encoding features. YouTube Live requires AAC audio.

-loglevel debug: This sets the log level to "debug," which means FFmpeg will provide detailed debugging information during the process. It's useful for troubleshooting but generates a lot of characters. If your script is working you can leave this command out.

-f flv: This specifies the output format as Flash Video (FLV). FLV is a popular format for live streaming.

rtmp://a.rtmp.youtube.com/live2/1234-1234-1234-1234-1234: This is the output destination URL. It points to a YouTube Live server, where the video stream will be sent. You'll need to replace "stream_key" with your actual YouTube Live stream key to ensure that the video is streamed to your channel.

On your raspberry pi desktop, click on the “>_” icon. This will bring up a command line window.


Copy and paste the above script in a text editor and fill in the required information with your parameters. When finished, copy the script to the clipboard and paste in the command line editor window and press enter. You should see the FFmpeg program start running and after a few seconds, your camera video will appear on YouTube Live.


This is a good way to confirm everything is running. But, if you reboot your raspberry pi, the stream encoder will not restart on its own. To automatically start the FFmpeg script we will generate a simple BASH script and make it auto start when the raspberry pi is rebooted using cron.


To do this, press “control c” to stop the existing FFmpeg process from running. Then, type “nano youtube_live.sh”. The nano command will bring up a text editor. Paste your FFmpeg script into the window and press “control x”. And press Y for yes and “enter”.


Now you will have a BASH script called “youtube_live.sh”. To make this a bootable script will enter “chmod +x youtube_live.sh”. To run this script from the command line you would enter “./youtube_live.sh”. To start this script automatically when the raspberry pi is rebooted we will use the cron utility. At the command line, enter “crontab -e”. This will bring up the cron scheduler. Arrow down to where you can enter text. Enter “@reboot sleep 5 && /home/pi/youtube_live.sh”. Then “control x” and “y” to store the crontab command.


Reboot the raspberry pi by typing “sudo reboot”, the raspberry pi will reboot and the FFmpeg script will automatically start.


There are a couple useful command line utilities that will help monitor what’s going on in the background of your raspberry pi.


First is the htop resource monitor. Use it from the command line by typing “htop”. This will give you a display of what is running in the background along with a basic resource monitor. To quit type “control c”.


Another helpful utility is ifstat. This will show you the incoming and outgoing bandwidth. To install, type “sudo apt-get install ifstat” at the command line. To run, type “ifstat” at the command line. To stop type “control c”.


To take the project a step further you can write a script that will monitor the bandwidth used. If it goes below a set parameter it will kill Ffmpeg and restart the streaming script.