Youtube API v3 - 获取 "Live Now" rtmp 和 streamkey

Youtube API v3 - Get "Live Now" rtmp and streamkey

Youtube 现在有一个直播部分,允许用户广播他们自己的直播会话。在这个 "Live Streaming" 部分,有 2 个选项:"Live Now [Beta]" 和 "Events"。

我知道 Youtube API 允许您检索事件的摄取 url 和流密钥,因此您可以向该目标广播,但它还需要手动管理许多其他步骤(例如发布流,将广播与流绑定,检查状态,开始,停止等。)。另一方面,"Live Now" 自动生成所有内容。

问题:如何从 Youtube API v3 检索 "Live Now" 摄取信息(rtmp url 和 streamkey)?

您无法检索 "Live Now" 摄取信息,因为 API 不区分 "Live Now" 和 "Events." 这两个选项作为接口提供在 API 对于最终用户,因此他们不必编写自己的与 API.

接口的应用程序

您将必须手动设置 liveBroadcastliveStream 对象,将它们与 liveBroadcasts.bind, test your stream, and transition to live on the liveStream object using status.streamStatus 绑定。

可以通过 livebroadcasts.list 检索默认广播,并将 broadcastType 设置为 "persistent"。

可以使用 boundstreamid livestreams.list 检索默认直播。

获取“立即直播”rtmp 和 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);
 // 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);
                    }
                }
          }