问题
I have stumbled upon an unexpected behavior of YouTube liveStream API.
Whenever I request liveStream.list
method, the only streams I get back are the ones initiated by my encoding software or liveStream.insert
calls.
What I am trying to retrieve instead is the stream that is proposed by YouTube in the default encoder setup:
Am I doing something wrong?
回答1:
You can retrieve information about the default, or "Stream Now" broadcast by passing persistent for the broadcastType parameter in liveBroadcast.list.
回答2:
Here's my final complete code for this , $api_stream_url
contains ServerURL/StreamKey
listLiveBroadcasts persistent – Return only persistent broadcasts.
listLiveStreams cdn - format
, rtmp
, streamName
, ingestionAddress
try {
// Execute an API request that lists broadcasts owned by the user who
// authorized the request.
$broadcastsResponse = $youtube->liveBroadcasts->listLiveBroadcasts(
'id,snippet,contentDetails',
array(
'broadcastType' => 'persistent',
'mine' => 'true',
));
$boundStreamId = $broadcastsResponse['items']['0']['contentDetails']['boundStreamId'];
$streamsResponse = $youtube->liveStreams->listLiveStreams('id,snippet,cdn', array(
// 'mine' => 'true',
'id' => $boundStreamId
));
/**
*
* (
[format] => 720p
[ingestionType] => rtmp
[ingestionInfo] => Array
(
[streamName] => 4vem-9233-mz3y-detq
[ingestionAddress] => rtmp://a.rtmp.youtube.com/live2
[backupIngestionAddress] => rtmp://b.rtmp.youtube.com/live2?backup=1
)
[resolution] => 720p
[frameRate] => 30fps
)
*/
$ingestionInfo = $streamsResponse['items']['0']['cdn']['ingestionInfo'];
/**
* (
[publishedAt] => 2016-06-08T12:58:38.000Z
[channelId] => UCcdDtElpr9XM5MA1stpK
[title] => Default Stream
[description] =>
[isDefaultStream] => 1
)
*/
$broadcastsResponse = $streamsResponse['items']['0']['snippet'];
$api_stream_key = $ingestionInfo->streamName;
$api_stream_address = $ingestionInfo->ingestionAddress;
/**
* Example:
* rtmp://a.rtmp.youtube.com/live2/4vem-9233-mz3y-detq
*/
$api_stream_url = $api_stream_address."/".$api_stream_key;
return false;
} catch (Google_Service_Exception $e) {
$htmlBody = sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody = sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
来源:https://stackoverflow.com/questions/38676534/youtube-livestreaming-api-method-livestream-list-doesnt-return-stream-proposed