6

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

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

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

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

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

2 回答 2

2

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

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

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

于 2016-05-04T12:58:50.603 回答
0

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

如果您愿意,可以通过代码创建流,或在 YouTube 界面中手动创建。

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

您可以使用以下代码通过list 方法获取流列表:

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

同样,上面的代码是一个stub

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

希望这对其他人有帮助。

于 2016-05-07T07:31:08.120 回答