ffmpeg and OBS simulcasting to YouTube and Twitch

19 May 2024

One of the questions I often get is how I livestream the Low-level streaming protocols in Go series to both YouTube and Twitch. There’s a bunch of software-as-a-services that can handle this for you, like restream.io and mux.com. But I just use a single ffmpeg command:

ffmpeg -f flv -listen 1 -i rtmp://127.0.0.1/ -map 0 -c copy \
-flags +global_header \
-f tee "[f=flv]$twitch|[f=flv]$youtube"

That mostly works – with one problem that I finally may have fixed. Compare the 1080p renditions of each of these videos:

The rendition on YouTube sucks! YouTube warns us about it during streaming, but it also shows a crazy warning like “unsupported codec”; as if we’re not using h.264? Strange.

To transmit to Twitch and YouTube we’re running the ffmpeg command:

ffmpeg -f flv -listen 1 -i rtmp://127.0.0.1/ \
    -c copy -f flv $twitch \
    -c copy -f flv $youtube

ffmpeg listens on the loopback interface for connections from OBS. Instead of specifying two codecs and two ouptuts, apparently we can use the tee muxer:

ffmpeg -f flv -listen 1 -i rtmp://127.0.0.1/ -map 0 -c copy \
    -flags +global_header \
    -f tee "[f=flv]$twitch|[f=flv]$youtube"

On the +global_header option, the ffmpeg wiki article Encoding for streaming sites says:

Since ffmpeg has a ‘different relationship’ with each rtmp server, the long-term headers need to be “out of band” at the container level inthe “extradata”. This is achieved with the global_header flag which very likely will be necessary based upon your encoding options

I wonder if that option may help with some of the weird stream buffering and encoding issues we saw on YouTube. Let’s hope it’s better!