在使用 YouTube Live Streaming API 时使用基本摄取或避免重复的自定义摄取

Posted

技术标签:

【中文标题】在使用 YouTube Live Streaming API 时使用基本摄取或避免重复的自定义摄取【英文标题】:Using basic ingestion when using the YouTube Live Streaming API or avoiding duplicate custom ones 【发布时间】:2016-08-26 04:35:53 【问题描述】:

我们将 YouTube Live Streaming API 与 Google API php Client 结合使用,我无法弄清楚如何让它使用基本(预设)摄取而不是自定义摄取。

自定义是可以的,但出于某种原因,即使您将它们称为相同的名称,它也会不断为您创建的每个流创建重复项。

所以我的问题是,我们如何让它使用基本摄取或能够选择一个自定义的而不每次都创建一个新的?

例如,以下是您在 YouTube 帐户中手动设置流时可以选择的基本提取:

相关PHP代码:

// Create an object for the liveBroadcast resource's snippet. Specify values
// for the snippet's title, scheduled start time, and scheduled end time.
$broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
$broadcastSnippet->setTitle($this->title);
$broadcastSnippet->setDescription($this->desc);
$broadcastSnippet->setScheduledStartTime($this->start_time);

// Create an object for the liveBroadcast resource's status, and set the
// broadcast's status.
$status = new Google_Service_YouTube_LiveBroadcastStatus();
$status->setPrivacyStatus($this->privacy_status);

// Create the API request that inserts the liveBroadcast resource.
$broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
$broadcastInsert->setSnippet($broadcastSnippet);
$broadcastInsert->setStatus($status);
$broadcastInsert->setKind('youtube#liveBroadcast');

// Execute the request and return an object that contains information
// about the new broadcast.
$broadcastsResponse = $this->youtube->liveBroadcasts->insert('snippet,status', $broadcastInsert, array());

// Create an object for the liveStream resource's snippet. Specify a value
// for the snippet's title.
$streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
$streamSnippet->setTitle($this->stream_title);

// Create an object for content distribution network details for the live
// stream and specify the stream's format and ingestion type.
$cdn = new Google_Service_YouTube_CdnSettings();
# TODO: Update the below `Format` method to use the new 'resolution' and 'frameRate' methods
$cdn->setFormat($this->format);
$cdn->setIngestionType('rtmp');

// Create the API request that inserts the liveStream resource.
$streamInsert = new Google_Service_YouTube_LiveStream();
$streamInsert->setSnippet($streamSnippet);
$streamInsert->setCdn($cdn);
$streamInsert->setKind('youtube#liveStream');

// Execute the request and return an object that contains information
// about the new stream.
$streamsResponse = $this->youtube->liveStreams->insert('snippet,cdn', $streamInsert, array());

// Bind the broadcast to the live stream.
$bindBroadcastResponse = $this->youtube->liveBroadcasts->bind(
    $broadcastsResponse['id'], 'id,contentDetails',
    array(
        'streamId' => $streamsResponse['id'],
    ));

【问题讨论】:

【参考方案1】:

好的,据我所知,没有办法使用基本摄取,但我想出了如何让它使用现有的自定义摄取。

您可以根据需要通过代码创建流,也可以在 YouTube 界面中手动创建。

完成此操作后,您需要获取要与您创建的新广播相关联的流的流ID;我无法通过 YouTube 界面找到此信息的方法,因此您也可以使用 API 来完成。

您可以使用以下代码获取带有list method 的流列表:

// Execute an API request that lists the streams owned by the user who
// authorized the request.
$streamsResponse = $this->youtube->liveStreams->listLiveStreams('id,snippet', array(
    'mine' => 'true',
));

$htmlBody .= "<h3>Live Streams</h3><ul>";
foreach ($streamsResponse['items'] as $streamItem) 
    $htmlBody .= sprintf('<li>%s (%s)</li>', $streamItem['snippet']['title'],
                         $streamItem['id']);

$htmlBody .= '</ul>';

注意上面的代码是一个存根;你可以在上面的链表方法中看到一个完整的例子;基本上你仍然需要打电话给Google_ClientGoogle_Service_YouTube 并确保你有一个有效的访问令牌等。

一旦您获得了您应该通过上述过程获得的 stream id;然后,您可以执行以下操作来使用您想要的特定流:

// Create an object for the liveBroadcast resource's snippet. Specify values
// for the snippet's title, scheduled start time, and scheduled end time.
$broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
$broadcastSnippet->setTitle($this->title);
$broadcastSnippet->setDescription($this->desc);
$broadcastSnippet->setScheduledStartTime($this->start_time);

// Create an object for the liveBroadcast resource's status, and set the
// broadcast's status.
$status = new Google_Service_YouTube_LiveBroadcastStatus();
$status->setPrivacyStatus($this->privacy_status);

// Create the API request that inserts the liveBroadcast resource.
$broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
$broadcastInsert->setSnippet($broadcastSnippet);
$broadcastInsert->setStatus($status);
$broadcastInsert->setKind('youtube#liveBroadcast');

// Execute the request and return an object that contains information
// about the new broadcast.
$broadcastsResponse = $this->youtube->liveBroadcasts->insert('snippet,status', $broadcastInsert, array());

// Bind the broadcast to the live stream.
$bindBroadcastResponse = $this->youtube->liveBroadcasts->bind(
    $broadcastsResponse['id'], 'id,contentDetails',
    array(
        'streamId' => 'stream_id_here', // <-- Insert your stream ID here
    ));

同样,上面的代码是一个存根

所以基本上底线是,一旦你有了你想要使用的流 ID,你就可以完全删除流代码的创建,然后只需传入你应该已经进入的 流 ID调用bind 方法,你应该很好。

希望这对其他人有所帮助。

【讨论】:

【参考方案2】:

我不确定您所说的“基本摄取”是什么意思。根据 API,唯一可设置的摄取属性是 cdn.ingestionType,此时仅支持 RTMP 摄取。

您在门户网站中看到的编码器设置的等效值是 cdn.format 值,它提供了一个界面来为您的直播选择比特率分辨率对。此属性已于 2016 年 4 月 18 日弃用,取而代之的是两个新属性:cdn.frameRatecdn.resolution。 Web 门户中列出的比特率值是每种分辨率的推荐比特率,由您的编码器配置,而不是 API。

正确设置自定义cdn 格式不应导致重复的直播流对象。您的代码中的其他地方可能存在错误。如果您觉得这是一个 API 缺陷,我建议您为 Google here 开一张票。

【讨论】:

更新问题以显示基本摄取的示例。 是的,我知道 cdn.format 已弃用,但不幸的是,Google API PHP Client 尚未添加对新方法的支持 AFAIK;至少在 V1 分支中。 @Brett 对我来说听起来像是 PHP 包装器的缺陷。您是否在 GitHub 项目页面或 gdata-issues 上打开了问题? 我上周在 php 客户端页面上打开了一个关于何时添加支持的问题;但还没有打开其他任何东西。 是的,我也会在 Google 代码 gdata-issues 页面上报告该问题:code.google.com/p/gdata-issues/issues/list?q=label:APi-YouTube

以上是关于在使用 YouTube Live Streaming API 时使用基本摄取或避免重复的自定义摄取的主要内容,如果未能解决你的问题,请参考以下文章

FFMPEG音频问题Youtube Live

在使用 YouTube Live Streaming API 时使用基本摄取或避免重复的自定义摄取

Youtube Live Streaming API 的“开始环聊”按钮

是否可以使用 YouTube Live Stream API 通过我的手机摄像头进行广播?

从 Youtube Live API 中获取当前流 URL

通过 Youtube live API 的低延迟广播选项