Youtube API v3 - Get “Live Now” rtmp and streamkey

送分小仙女□ 提交于 2019-12-12 10:55:28

问题


Youtube now has a Live Streaming section that allows users to broadcast their own live stream sessions. In this "Live Streaming" section, there are 2 options: "Live Now [Beta]" and "Events".

  • Live Now is a fast and easy way to start a streaming session automatically just by pointing your video encoder to te specified RTMP Url and Stream Key. It will automatically detect incomming media and start broadcasting publicly.

  • Events is pretty much the same thing, but with advance settings, although it will not start automatically to broadcast, and you need to set everything pretty much manually.

I know Youtube API allows you to retrieve Event's ingestion url and streamkey, so you can broadcast to that target, but it also requires to manage many other steps manually (like publishing the stream, binding broadcasts with streams, check the status, start, stop, etc..). On the other hand "Live Now" makes everything automatically.

Question: How can I retrieve "Live Now" ingestion info (rtmp url and streamkey) from the Youtube API v3 ?


回答1:


You cannot retrieve "Live Now" ingestion info because the API does not differentiate between "Live Now" and "Events." Those two options are provided as interfaces on top of the API for an end user, so they don't have to write their own application that interfaces with the API.

You will have to manually set up liveBroadcast and liveStream objects, bind them with liveBroadcasts.bind, test your stream, and transition to live on the liveStream object using status.streamStatus.




回答2:


The default broadcast can be retrieved by livebroadcasts.list with broadcastType set to "persistent".

The default livestream can be retrieved by livestreams.list using boundstreamid.




回答3:


To Get “Live Now” rtmp and streamkey

       $broadcastsResponse = $youtube->liveBroadcasts->listLiveBroadcasts(
            'id,snippet,contentDetails',
            array(
                'broadcastType' => 'persistent',
                'mine' => 'true',
            ));

        $boundStreamId = $broadcastsResponse['items']['0']['contentDetails']['boundStreamId'];

        $streamsResponse = $youtube->liveStreams->listLiveStreams('id,snippet,cdn', array(
//            'mine' => 'true',
            'id' => $boundStreamId
        ));

        print_r($streamsResponse);



回答4:


 // Keep client_id,client_secret and redirect_uri the client_secrets.json
 UserCredential credential;
 string BoundStreamId = string.Empty;
 string StreamKey=string.Empty;
 using (var stream = new FileStream("client_secrets.json", FileMode.Open, 
   FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                new[] { YouTubeService.Scope.Youtube,YouTubeService.Scope.YoutubeReadonly},
                "user",
                CancellationToken.None,
                new FileDataStore(this.GetType().ToString())
             );
            }
if (credential != null)
            {
                var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "MyApp" // your app name
                });
 LiveBroadcastsResource.ListRequest lbRequest = youtubeService.LiveBroadcasts.List("id,snippet,contentDetails,status");
                lbRequest.BroadcastType = LiveBroadcastsResource.ListRequest.BroadcastTypeEnum.Persistent;
                lbRequest.MaxResults = 10;

                lbRequest.Mine = true;

                var bcResponse = await lbRequest.ExecuteAsync();

                IEnumerator<LiveBroadcast> iLB = bcResponse.Items.GetEnumerator();
                while (iLB.MoveNext() && string.IsNullOrEmpty(liveChatId))
                {
                  BoundStreamId = livebroadcast.ContentDetails.BoundStreamId;
                }
 LiveStreamsResource.ListRequest lsRequest =
                                   youtubeService.LiveStreams.List("id,snippet,cdn,status");
                lsRequest.MaxResults = 50;                    
                lsRequest.Id = BoundStreamId;

                var srResponse = await lsRequest.ExecuteAsync();

                IEnumerator<LiveStream> iLS = srResponse.Items.GetEnumerator();
 if (srResponse != null)
                {
                 foreach(LiveStream lvStream in srResponse.Items)
                    {
                       StreamKey= lvStream.Cdn.IngestionInfo.StreamName);
                    }
                }
          }


来源:https://stackoverflow.com/questions/33798901/youtube-api-v3-get-live-now-rtmp-and-streamkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!