Youtube API v3 - 获取“正在直播”rtmp和流密钥

8
Youtube现在有一个直播流部分,允许用户广播自己的实时流会话。在这个“直播流”部分中,有两个选项:“现在直播[Beta]”和“事件”。
- “现在直播”是一种快速简便的方法,只需将视频编码器指向指定的RTMP Url和Stream Key即可自动开始一个流会话。它会自动检测传入的媒体并公开开始广播。 - “事件”基本上是一样的,但具有高级设置,尽管它不会自动开始广播,您需要手动设置几乎所有东西。
我知道Youtube API允许您检索“事件”的摄取URL和流密钥,因此您可以广播到该目标,但它还需要手动管理许多其他步骤(如发布流,将广播与流绑定,检查状态,启动,停止等) 。另一方面,“现在直播”使一切都自动化。
问题:如何从Youtube API v3中检索“现在直播”的摄取信息(rtmp url和stream key)?

嗨,omarojo,你找到获取它的方法了吗?我也在寻找那个API,似乎像“XSplit”和“Wirecast”这样的YouTube验证程序可以通过API获取流名称。 - rhbc73
@rhbc73 我成功地使用API创建了自己的直播和直播流(可能与Wirecast等软件所做的相同)。尽管YouTube最近更新了其API文档,以提供默认摄入数据用于YouTube Livestream Now功能,在其liveBroadcast.list端点中。但我还没有能够确认这一点。 - omarojo
我知道我们可以创建一个直播,创建一个直播流,将直播绑定到流,然后流式传输到流名称(这是直播流的属性)。但是流名称将与“立即直播”所拥有的名称不同。因此,您创建的直播不会出现在“立即直播”中,而是出现在“活动”部分中? - rhbc73
这是正确的。使用“立即流式传输”摄取端点的唯一好处是,直播活动的开始和结束时间取决于您何时启动/停止使用编码器发送视频数据。否则,使用Live Events需要您负责所有直播周期,特别是过渡到“完整”状态(结束事件)。 - omarojo
谢谢omarojo,没错,使用直播事件需要您在YouTube网页上手动“开始预览”和“开始流式传输”。 您知道这两个操作是否可以通过某些API(转换API?)执行,而不是在YouTube网页上单击鼠标? 您还说:“Youtube最近更新了其API文档,以提供Youtube Livestream Now功能的默认摄取数据”,您有链接吗? (我尝试通过liveBroadcast.list获取它,但没有返回任何内容) - rhbc73
4个回答

12

通过将broadcastType设置为“persistent”,可以使用livebroadcasts.list检索默认广播。

可以使用boundstreamid通过livestreams.list检索默认的直播流。


谢谢!除非您阅读文档中的所有细则,否则无法知道您可以检索默认的实时广播。 - ben.bourdin
哇!它的工作就像魔法一样!有没有办法获取公共URL以分发给观众? - kemmitorz
非常感谢!@ben.bourdin,我在哪里可以找到官方文档? - deadbeef
@deadbeef 这是我使用的主要资源:YouTube Live API文档。每个端点都有一个“API浏览器”,非常好用,可以轻松测试。 - ben.bourdin

3
您无法获取“现在直播”摄入信息,因为API不区分“现在直播”和“事件”。这两个选项是作为用户界面提供的API,使得最终用户不必编写与API交互的应用程序。
您需要手动设置 liveBroadcastliveStream 对象,并使用liveBroadcasts.bind将它们绑定,测试您的流,并使用status.streamStatus来转换到liveStream对象上的实时流。

2
获取“直播中”rtmp和流密钥
       $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);

1
 // 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);
                    }
                }
          }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接