How to live broadcast from my sofa to my grandma like a pro? (1) From FFmpeg to YouTube.
Kodsama
Posted on April 27, 2022
How to live broadcast from my sofa to my grandma like a pro? (1) From FFmpeg to YouTube.
In a short set of articles I will explain how to stream video from your sofa to end users like a pro using FFmpeg and different services. We will go briefly through the technical details and get our hands dirty with some command lines.
Here I will introduce the protocols supported by YouTube and use FFmpeg (a fantastic open source media toolbox) to stream content from point A (my computer) to the world (of which your grandma is part of!).
YouTube is an American online video sharing and social media platform owned by Google. It is by far the biggest video sharing site on the internet, but most of what is below applies to other websites such as Facebook, Vimeo, Dailymotion, Twitch, etc.
Our goal is to broadcast live content from our computer to our grandma using YouTube live.
Set up YouTube for the live broadcast
1. Get the link for your grandma
First things first, we need to find the direct link to our live so that we can send it to our grandma:
Go to YouTube.com
Click “View your channel” and select the appropriate channel
Go to youtube.com/account_advanced
Copy your Channel ID
Now, type in youtube.com/channel/PASTE YOUR CHANNEL ID HERE/live
Your permanent YouTube Live Page should look like this: https://www.youtube.com/channel/UC7qJ6z7MVPtqP8/live
2. Set up the live in YouTube
We need to set up the live streaming from the receiver side. Again, each streaming platform is slightly different, here I will walk you through YouTube
It is fairly easy:
Go to your YouTube account
From the top right, click Create (the small camera with a + sign) and “Go live”.
Follow the prompts to verify your channel (if not done yet).
You will arrive on YouTube creator, select if you want to stream now or later.
Select “streaming software” as source
On YouTube Creator we find the information of interest:
Stream URL (something of the form rtmp://[…].youtube.com/live)
Stream Key (a serie of characters)
Copy those and save them somewhere for later use.
How to Encode Videos for Video Sharing Sites
1. Install FFmpeg
First of we need to have FFmpeg installed on the computer, to install it we can follow the tutorial available online: Windows Linux MacOS
When it is installed you then need to open a terminal/Powershell session and verify it is properly installed, type in the first line below and you should see text with information about FFmpeg like in the second line:
ffmpeg -version
ffmpeg version x.x.x Copyright (c) 2000-20xx the FFmpeg developers [...]
The syntax of FFmpeg commands is (almost) always like this:
ffmpeg [input options] -i [input] [output options] [output]
Below I will go through the different parts in brackets and explain how they are used.
2. Encode with FFmpeg: the [input] and [input options]
Now, we need to locate the video file we want to stream (if any) or open the device we want to stream from (our input), transform its content to suit YouTube and send it there.
If you are using a file then you should point to the file (here an mp4 file, this could be avi, mkv, etc):
ffmpeg /myfolder/myfile.mp4
[input options] here is empty and [input] is “/myfolder/myfile.mp4”
If you want to use your computer camera (here a macbook pro) then first find a list of devices (first line) and its answer below:
ffmpeg -f avfoundation -list_devices true -i ""
[AVFoundation indev @ 0x139f04ff0] AVFoundation video devices
[AVFoundation indev @ 0x139f04ff0] [0] FaceTime HD Camera
[AVFoundation indev @ 0x139f04ff0] [1] Capture screen 0
[AVFoundation indev @ 0x139f04ff0] AVFoundation audio devices:
[AVFoundation indev @ 0x139f04ff0] [0] MacBook Pro Microphone
And then use the device you want as input (here 0):
ffmpeg -f avfoundation -i "0"
[input options] here is “-f avfoundation” and [input] is “-i “0””
3. Encode with FFmpeg: the [output options]
We need to make sure that the content we input is compatible for YouTube. You can find a list of supported codecs on the YouTube wiki.
As of today (2022–04) only the H.264 video encoding with AAC or MP3 audio compression are supported.
In the example below we chose a H.264, level 4.2 for 1080p 60 FPS video with AAC sound.
The FFmpeg [output options] look like that:
-c:v libx264 -framerate 60 -crf 18 -maxrate 50M -pix_fmt yuv420p -c:a aac -b:a 128k
Above we tell FFmpeg to use:
H.264 for the video (-c:v libx264)
60 pictures per second (-framerate 60)
video quality of 18 (-crf 18) but reduce the image quality to not go over 50 megabytes per second (-maxrate 50M | 50 MBps corresponds to level 4.2)
use image format yuv420p (-pix_fmt yuv420p)
Use AAC for audio (-c:a aac) with 128 kilobits per second (-b:a 128k)
If you want to go deeper in the different options, you can dig into the FFmpeg page for streaming or the page for parameters and protocols.
4. Encode with FFmpeg: the [output]
Now that we have the [input options] [input] and [output options] we just need to define the [output], it is simple it looks like that ([YouTube_url] and [YouTube_key] being the ones you copied from YouTube):
-f flv [YouTube_url]/[YouTube_key]
What is flv?
flv is the keyword for FFmpeg for Real-Time Messaging Protocol (RTMP). It is a communication protocol for streaming audio, video, and data over the Internet, it was developped by Macromedia (later Adobe) to stream between Flash Player (this is where flv comes from) to a server. It is still one of the most used broadcast protocols on the internet today. These links usually start by rtmp://
Abracadabra!
Now that we have set up the whole chain, we just need to put everything together, remember FFmpeg commands look like this:
ffmpeg [input options] -i [input] [output options] [output]
So your command would look something like that:
ffmpeg -f avfoundation -i "0" -c:v libx264 -framerate 60 -crf 18 -maxrate 50M -pix_fmt yuv420p -c:a aac -b:a 128k -f flv rtmp://a.rtmp.youtube.com/live2/mykey
Run the command above and you should see your video preview in YouTube Studio !
Now your grandma can use the link you sent her to see your live… magic !
Posted on April 27, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.