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

Using basic ingestion when using the YouTube Live Streaming API or avoiding duplicate custom ones

我们将 YouTube 直播 API 与 Google API PHP Client 结合使用,但我不知道如何让它使用基本(预设)摄取而不是自定义摄取。

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

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

例如,当您在 YouTube 帐户中手动设置流时,您可以 select 进行基本提取:

相关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'],
    ));

我不确定你所说的 "basic ingestion." 是什么意思 根据 API,唯一可设置的摄取 属性 是 cdn.ingestionType,它只支持 RTMP 摄取时间。

您在 Web 门户中看到的编码器设置的等效项是 cdn.format value, provided an interface to select bitrate-resolution pair for your Live Broadcast. This property was deprecated on April 18, 2016 in favor of two new properties: cdn.frameRate and cdn.resolution。 Web 门户中列出的比特率值是每个分辨率的推荐比特率,由您的编码器配置,而不是 API。

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

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

您可以根据需要通过代码创建流,也可以在 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 并确保您拥有有效的访问令牌等。

一旦您获得了您应该通过上述过程获得的 流 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 方法并且你应该很好。

希望这对其他人有帮助。