Bre*_*ett 6 youtube youtube-api google-api-php-client youtube-data-api youtube-livestreaming-api
我们将YouTube Live Streaming API与Google API PHP客户端结合使用,我无法弄清楚如何使用基本(预设)提取而不是自定义提取.
自定义的是正常的,但由于某些原因,即使您将它们称为相同的名称,它也会为您创建的每个流不断创建重复项.
所以我的问题是,我们如何让它使用基本的摄取或者能够选择一个自定义的,而不是每次都创建一个新的?
例如,您可以在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'],
));
Run Code Online (Sandbox Code Playgroud)
好吧,据我所知,无法使用基本摄取,但我想出了如何使其使用现有的自定义摄取。
如果您愿意,您可以通过代码创建流,也可以在 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>';
Run Code Online (Sandbox Code Playgroud)
请注意,上面的代码是一个存根;您可以在上面的链表方法中看到完整的示例;基本上,您仍然需要拨打电话Google_Client,Google_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
));
Run Code Online (Sandbox Code Playgroud)
同样,上面的代码是一个存根。
所以基本上底线是一旦你有了你想要使用的流 ID,你就可以完全删除流代码的创建,然后只需传入你应该已经在方法调用中的流 IDbind,你应该很好。
希望这对其他人有帮助。
| 归档时间: |
|
| 查看次数: |
833 次 |
| 最近记录: |