How to livestream to Twitch and YouTube at the same time using ffmpeg and OBS Studio on a single Windows or Linux computer

I'm a big fan of OBS Studio, and I've recently started learning how to use ffmpeg for transcoding and remuxing my videos. One thing that has bugged me is that current versions of OBS Studio can only stream to one streaming service at a time (one RTMP server). There are services like restream.io that get around this, but you're basically paying them for a little bit of CPU/GPU time and some added latency to your stream, and maybe only getting combined chat in return.

Well, it just so happens that with a single ffmpeg command line invocation, you can output to multiple files and streaming services at once with very little overhead. Another benefit of ffmpeg over OBS Studio is that it supports more encoders such as hevc_amf which is the hardware H.265 encoder for AMD video cards. OBS Studio currently only supports h264_amf via the Advanced output mode. Strangely, OBS Studio has an included version of ffmpeg but the list of encoders it has does not include any AMD encoders, only NVidia's encoders, and OBS Studio also provides a really limited interface to its included ffmpeg.

If you have multiple computers - one for recording with OBS Studio and another for encoding with ffmpeg - there are already guides for how to do that, such as this one which suggests sending and receiving over UDP. However, as far as I can tell, this is only possible with at least two computers - it is impossible to send and receive on the same computer without doing weird networking hacks. This was a dead end for me for a long time. I was looking up the possibility of named pipes or other IPC between the ffmpeg inside OBS Studio and the separate ffmpeg on my system.

Finally, I found this reddit post and suddenly everything fell into place. Basically, you just host your own minimal RTMP server on your own system, stream to it from OBS Studio, and then use it as an input to ffmpeg. Heck, if you don't need to re-encode the stream at all, you can literally use just the RTMP server to rebroadcast to Twitch and YouTube simultaneously without needing ffmpeg at all.

You should be able to follow along in order to set up your copy of nginx on either Linux or Windows. For Windows specifically, I've found that the "nginx 1.7.11.3 Gryphon.zip" version works and includes RTMP support, though it is rather outdated coming from 2015 and I would discourage you from opening it up to the internet in any capacity. If you're more experienced you could try building the latest and most secure versions of everything yourself.

If you plan to just use nginx to restream to both Twitch and YouTube without using ffmpeg, be sure to adjust your streaming bitrate and such in OBS Studio - Twitch is the main limiting factor as YouTube will happily accept anything your upload speed can handle. If you'll be using ffmpeg however, I recommend configuring OBS Studio to stream lossless with fast encoding time, and then we'll be doing the actual lossy encoding in ffmpeg (this way you don't double-encode lossy video). You can use a software or hardware encoder in OBS Studio for this purpose, whichever works best for you.

Once you've installed nginx and added the minimum test configuration for the RTMP server, all you have to do is stream to it from OBS Studio as documented and then you're all set to use it as an input to ffmpeg. Here's an example command line to test it out without actually streaming anywhere so you can get an idea of the quality:

ffmpeg -i "rtmp://127.0.0.1/live/test" -c:v h264_amf -usage lowlatency -rc cbr -b:v 4000k -c:a copy twitch-recording.flv -c:v h264_amf -usage lowlatency -rc cbr -b:v 7500k -c:a copy youtube-recording.flv -c:v hevc_amf -rc cqp -qp_p 20 -qp_i 20 -c:a copy local-recrding.mkv -c:v copy -c:a copy raw-recording.mkv

In that command line there is one input (the RTMP stream from nginx) and four outputs (all saved to files with different encoding settings so you can compare the quality of each). Obviously when you are actually streaming you'd only need two or three outputs depending on whether you want to record locally or not. The audio is just copied from OBS Studio as-is, since it is already fine for Twitch and YouTube. The h264_amf and hevc_amf encoders for AMD graphics cards can be swapped out for h264_nvenc and hevc_nvenc for NVidia graphics cards. If you want to use Intel graphics for some reason, you can use h264_qsv. Be sure to use "ffmpeg -h full" and search for the encoder you are using to make sure you specify the correct options, as they differ from encoder to encoder.

If you can run the above command and your system doesn't catch fire, you should be more than ready when it comes time to actually stream. It is very important to note that you should always stop ffmpeg first before stopping the stream in OBS Studio. You can do so by pressing q or Ctrl+C (once) in the command prompt window or terminal where you are running ffmpeg. Then you can stop streaming in OBS Studio. If you want to shutdown nginx, run "nginx -s stop" in the directory with the executable.

To actually stream to Twitch and YouTube using your stream keys, you'll want to use a command like this:

ffmpeg -i "rtmp://127.0.0.1/live/test" -c:v h264_amf -usage lowlatency -rc cbr -b:v 4000k -c:a copy -f flv "rtmp://live.twitch.tv/app/YOUR-TWITCH-KEY-HERE" -c:v h264_amf -usage lowlatency -rc cbr -b:v 7500k -c:a copy -f flv "rtmp://a.rtmp.youtube.com/live2/YOUR-YOUTUBE-KEY-HERE"

This will take your stream and send 4000kb/s to Twitch and 7500kb/s to YouTube. It won't create a local recording, but that's easy enough to figure out from the earlier command. Again, you can substitute the encoders for NVidia or Intel's encoders. It's also possible to use software encoding with an encoder like libx264 but that will take a lot of CPU usage, which is why I recommend the hardware encoders instead. Also keep in mind you are likely to have better driver support on Windows.

By the way, if you're new to YouTube streaming, I recommend that you DON'T use the "Stream Now" beta - it's still quite buggy and your stream archive will end up several pages back in your video manager making it hard to find. Instead, create a scheduled event - this has several advantages: firstly, subscribers will see the stream notification like any other video in their subscription feed, letting them click "Set Reminder", and when they visit the stream URL they will see a countdown (so you don't have to do a countdown yourself). Secondly, if you stop your stream for any reason (system crash, power loss, internet outage, etc), the stream will not actually end until you click the Stop button on YouTube's livestream dashboard. You can literally stop streaming for an hour to eat, and then start again, and it will pick up like nothing happened. There will be no gap in the stream archive, just an instant cut. Just remember to always stop ffmpeg before you stop OBS Studio or bad things happen. If your internet goes out, stop ffmpeg and let OBS Studio keep streaming to nginx, then when you have internet again you cam start ffmpeg again.

Unfortunately Twitch isn't so kind, any interruption of your stream that lasts more than a few seconds will split your Twitch stream in half, making two separate archives. Viewers on Twitch also won't be able to rewind the stream and catch up to live at 1.5x/2x speed like they can on YouTube with YouTube DVR. Additionally, if you are a Twitch affiliate or partner, you are not allowed to use any streaming service other than Twitch, meaning you read this whole article for nothing. YouTube doesn't have such an exclusivity clause.

It's a shame, because Twitch and YouTube are pretty similar as far as features for streamers go: they both offer monthly paid subscriptions ("Subscribe" on Twitch and "Sponsor" on YouTube) and they also both offer paid chat messages ("bits" on Twitch and "Super Chats" on YouTube). Another thing to consider is that Twitch allows you to upload videos just like you would to YouTube, so you can upload to both just like you stream to both. Unfortunately, Twitch Uploads can't be monetized unless you're an affiliate or partner, which would prevent you from streaming on YouTube. Thankfully, the YouTube "Sponsor" feature is still available whether you stream on YouTube or not.

And no, this isn't a paid post, I'm just making sure newcomers are fully aware of the ins and outs of each platform, since this article is about streaming to both after all.

If you have any questions or trouble getting things set up, feel free to comment and ask - I prefer public communication so that others with the same problem can see that and get an answer too. If you need help learning to use OBS Studio, here's a great tutorial series. If you need help using ffmpeg, I'd recommend lots of Google searches, reading the wiki, reading the help, reading the documentation, etc. If you need help using nginx, I'm not sure what you did wrong but I wish you luck in fixing it.

Comments

  1. So the funny thing was, I saw this post earlier and just read through it and I honestly can't say I knew about the exclusivity clause on twitch as an affiliate, and I've been one for over 4 years. So I guess thanks for your post, I learnt something even if it wasn't what I was expecting, and as a result, will stop the simul-streams.

    ReplyDelete

Post a Comment

Remember the universal code of conduct