使用YouTube直播流媒体API时使用基本摄取或避免重复自定义

时间:2016-05-01 09:21:30

标签: youtube youtube-api google-api-php-client youtube-data-api youtube-livestreaming-api

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

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

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

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

YouTube encoder

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

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

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

如果您愿意,可以通过代码创建流,也可以在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
    ));

同样,上面的代码是 stub

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

希望这有助于其他人。