We are using the YouTube Live Streaming API in conjunction with the Google API PHP Client and I cannot work out how to make it use a basic (preset) ingestion rather than a custom one.
Custom ones are OK, but for some reason even if you call them the same name it continually creates duplicates for every stream you create.
So my question is, how do we get it to use basic ingestion or be able to select a custom one without creating a new one each time?
For example here is the basic ingestion you can select when you setup a stream manually within your YouTube account:
The relevant PHP code:
// 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'],
));
Ok, from what I can tell there is no way to use basic ingestion but I figured out how to make it use an existing custom ingestion.
You can create the stream via the code if you like or create it manually within the YouTube interface.
Once this is done you will need to get the stream ID
of the stream you want to associate with the new broadcast you have created; I could not tell a way to find out this info via the YouTube interface so you can do it with the API as well.
You can use the following code to get a list of streams with the 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>';
Note that the code above is a stub; you can see a full example at the linked list method above; basically you will still need to make calls to Google_Client
, Google_Service_YouTube
and make sure you have a valid access token etc..
Once you have your stream id which you should have gotten via the process above; you can then do something like the below to use that specific stream you want:
// 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
));
Again, the above code is a stub.
So basically the bottom line is once you have your stream ID you want to use you can remove the creation of a stream code out completely and then just pass in the stream id you should have already into the call to the bind
method and you should be good.
Hopefully this helps someone else.