Skip to content

Instantly share code, notes, and snippets.

@stiltr
Last active April 6, 2021 05:39
Show Gist options
  • Save stiltr/bd8ace06b86ad1bec18de2718d3abba0 to your computer and use it in GitHub Desktop.
Save stiltr/bd8ace06b86ad1bec18de2718d3abba0 to your computer and use it in GitHub Desktop.
Streaming to the new YouTube Studio with ffmpeg and nginx-rtmp

Streaming to the new YouTube Studio with ffmpeg and nginx-rtmp

I've been successfully streaming to YouTube from a Teradek Vidiu Pro hardware encoder, but wanted to play with streaming to both YouTube and Facebook at the same time. I also wanted to be able to stream pre-recorded videos as if they were live. To accomplish this, I turned to nginx-rtmp and ffmpeg. The only problem was, it didn't work. There are plenty of tutorials out there for setting up nginx-rtmp and for using ffmpeg to send rtmp streams, but no matter what I tried I couldn't get the stream to start on YouTube. I'd start streaming to YouTube, and I could see in the Live Dashboard it would change from No data to Excellent connection, but that was it. The Go Live button was disabled and there was nothing I could do about it. I suspect this is also why I was unable to get castr.io to stream to YouTube as well.

However, after much trial and error, I found that if I switched from Live Control Room to Stream Now Classic, I was able to stream successfully! While that would solve my problem for the moment, Stream Now Classic is deprecated and going away soon. Plus, I like the workflow and features of the new Live Control Room. So I had to find a better answer.

I tried to find any documentation that would explain the difference between the two and came up empty. It may be out there, but I didn't find it. My next thought was that maybe something in the Live Streaming API could shed some light on this. I setup a new project over on the Google Developer Console and started to play around with the API. You need to get an OAuth2 authorization token for the YouTube account you want to interact with. I just setup a basic OAuth2 client under the credentials section and used that along with curl and the limited input device flow to get the tokens I needed. Once I had that, I tried calling some API methods. I had some success, but dealing with those long curl strings was a pain, so I swapped over to Postman and kept working. I dumped the list of LiveBroadcasts and LiveStreams from the account and eventually noticed this LiveBroadcast property: contentDetails.enableAutoStart. It was set to false. So I swapped over to the update method and changed it to true. Note:

If you are submitting an update request, and your request does not specify a value for a property that already has a value, the property's existing value will be deleted.

So make sure to grab the current list before you try and change anything.

Once I'd updated enableAutoStart to be true, I launched ffmpeg and lo and behold my stream went live just like I hoped it would! So now, all I have to do is go in and change any new events I create to autostart and I can stream without issue. Depending on your use case, you should also be able to enable the contentDetails.monitorStream to see the stream in the preview window before going live. You can manually transition between monitoring and live output with the transition method, though I haven't tested it yet.

Update: After more testing, I found that when creating a new stream by copying the settings from a previous stream, enableAutoStart is copied as well, so you only have to make the change once!

Hopefully this answer some questions about why your stream isn't working like you expect. Best of luck!

~stiltr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment