How to live broadcast from my sofa to my grandma like a pro? (1) From FFmpeg to YouTube.

kodsama

Kodsama

Posted on April 27, 2022

How to live broadcast from my sofa to my grandma like a pro? (1) From FFmpeg to YouTube.

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:

  1. Go to YouTube.com

  2. Click “View your channel” and select the appropriate channel

  3. Go to youtube.com/account_advanced

  4. Copy your Channel ID

  5. 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:

  1. Go to your YouTube account

  2. From the top right, click Create (the small camera with a + sign) and “Go live”.

  3. Follow the prompts to verify your channel (if not done yet).

  4. You will arrive on YouTube creator, select if you want to stream now or later.

  5. 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)

Image description

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 [...]
Enter fullscreen mode Exit fullscreen mode

The syntax of FFmpeg commands is (almost) always like this:

    ffmpeg [input options] -i [input] [output options] [output]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

[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
Enter fullscreen mode Exit fullscreen mode

And then use the device you want as input (here 0):

    ffmpeg -f avfoundation -i "0"
Enter fullscreen mode Exit fullscreen mode

[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
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 !

💖 💪 🙅 🚩
kodsama
Kodsama

Posted on April 27, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related