1

I am using Youtube API for creating events from my application in Youtube for Live streaming.That successfully done by the following code.But my requirement:

1) I need to start the live stream from my application interface using youtube API.Is it possible?

2) When I create events from app using youtube API its automatically go to Custom (more encoding options) type.I need to create live streaming events in Quick (using Google+ Hangouts On Air) type.Is it possible?

My code:

<?php

// Call set_include_path() as needed to point to your client library.
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
//'2015-08-28T00:00:00.000Z'
//'2015-08-29T00:00:00.000Z'
/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
 ?>
 <script type="text/javascript">
    var datefield=document.createElement("input")
    datefield.setAttribute("type", "date")
    if (datefield.type!="date"){ //if browser doesn't support input type="date", load files for jQuery UI Date Picker
        document.write('<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />\n')
        document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"><\/script>\n')
        document.write('<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"><\/script>\n') 
    }
</script>

<script>
if (datefield.type!="date"){ //if browser doesn't support input type="date", initialize date picker widget:
    jQuery(function($){ //on document.ready
        $('.dateStart').datepicker();
        $('.dateEnd').datepicker();
    })
}
</script>

 <?php

$OAUTH2_CLIENT_ID = 'dsfsfsdfofgm9e4uvg.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'dsfsdfdsfPhf55tcCDLu';
$htmlBody="";
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}


// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
if(isset($_POST['title']) && isset($_POST['timeStart']) && isset($_POST['timeEnd']) && isset($_POST['Status']) && isset($_POST['dateStart']) && isset($_POST['dateEnd']))
{
  try {

    // Create an object for the liveBroadcast resource's snippet. Specify values
    // for the snippet's title, scheduled start time, and scheduled end time.
    $startTime  = $_POST['dateStart']."T".$_POST['timeStart'].":00+0530";
    $endTime  = $_POST['dateEnd']."T".$_POST['timeEnd'].":00+0530";
    //$startDatetime = new DateTime($startTime);
    //$endDatetime = new DateTime($endTime);

    //$startDatetime = $startDatetime->format(DateTime::ISO8601);
    //$endDatetime = $endDatetime->format(DateTime::ISO8601);
    $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
    $broadcastSnippet->setTitle($_POST['title']);
    $broadcastSnippet->setScheduledStartTime($startTime);
    $broadcastSnippet->setScheduledEndTime($endTime);

    // Create an object for the liveBroadcast resource's status, and set the
    // broadcast's status to "private".
    $status = new Google_Service_YouTube_LiveBroadcastStatus();
    $status->setPrivacyStatus($_POST['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 = $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('New Stream');

    // 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();
    $cdn->setFormat("1080p");
    $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 = $youtube->liveStreams->insert('snippet,cdn',
        $streamInsert, array());

    // Bind the broadcast to the live stream.
    $bindBroadcastResponse = $youtube->liveBroadcasts->bind(
        $broadcastsResponse['id'],'id,contentDetails',
        array(
            'streamId' => $streamsResponse['id'],
        ));

    $htmlBody .= "<h3>Your Broadcast success fully scheduled.</h3><ul>";
   $htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
        $broadcastsResponse['snippet']['title'],
        $broadcastsResponse['snippet']['publishedAt'],
        $broadcastsResponse['id']);
    $htmlBody .= '</ul>';
    $htmlBody .= "<h3>Added Stream</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
        $streamsResponse['snippet']['title'],
        $streamsResponse['id']);
    $htmlBody .= '</ul>';
    $htmlBody .= "<h3>Bound Broadcast</h3><ul>";
    $htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
        $bindBroadcastResponse['id'],
        $bindBroadcastResponse['contentDetails']['boundStreamId']);
    $htmlBody .= '</ul>';
    //$htmlBody .="<h3>Live  Broadcast</h3><div>";

    //$htmlBody .= sprintf("<iframe id='ytplayer' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/%s?autoplay=1'  frameborder='0'></iframe>",
        //  $broadcastsResponse['id']);
    $htmlBody .= '</div>';


  } catch (Google_Service_Exception $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
      // echo $e->getMessage();
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  }

  $_SESSION['token'] = $client->getAccessToken();
  } else {
  if(isset($_GET['propid']))
  {
  $projectname = $_GET['name'];
  $htmlBody = <<<END
   <form action="" method="post">
  <label>Event Title:  $projectname</label>
<input type="hidden" name="title" value="$projectname"/><br/><br/>
 <label>Start Time:</label>
<input type="date" name="dateStart"/>
 <select name="timeStart">
  <option value="00:00">12:00 AM</option>
  <option value="00:30">12:30 AM</option>
  <option value="01:00">01 AM</option>
  <option value="01:30">01:30 AM</option>
  <option value="02:00">02:00 AM</option>
  <option value="02:30">02:30 AM</option>
  <option value="03:00">03:00 AM</option>
  <option value="03:30">03:30 AM</option>
  <option value="04:00">04:00 AM</option>
  <option value="04:30">04:30 AM</option>
  <option value="05:00">05:00 AM</option>
  <option value="05:30">05:30 AM</option>
  <option value="06:00">06:00 AM</option>
  <option value="06:30">06:30 AM</option>
  <option value="07:00">07:00 AM</option>
  <option value="07:30">07:30 AM</option>
  <option value="08:00">08:00 AM</option>
  <option value="08:30">08:30 AM</option>
  <option value="09:00">09:00 AM</option>
  <option value="09:30">09:30 AM</option>
  <option value="10:00">10:00 AM</option>
  <option value="10:30">10:30 AM</option>
  <option value="11:00">11:00 AM</option>
  <option value="11:30">11:30 AM</option>
  <option value="12:00">12:00 PM</option>
  <option value="12:30">12:30 PM</option>
  <option value="13:00">01:00 PM</option>
  <option value="13:30">01:30 PM</option>
  <option value="14:00">02:00 PM</option>
  <option value="14:30">02:30 PM</option>
  <option value="15:00">03:00 PM</option>
  <option value="15:30">03:30 PM</option>
  <option value="16:00">04:00 PM</option>
  <option value="16:30">04:30 PM</option>
  <option value="17:00">5:00 PM</option>
  <option value="17:30">5:30 PM</option>
  <option value="18:00">6:00 PM</option>
  <option value="18:30">6:30 PM</option>
  <option value="19:00">7:00 PM</option>
  <option value="19:30">7:30 PM</option>
  <option value="20:00">8:00 PM</option>
  <option value="20:30">8:30 PM</option>
  <option value="21:00">9:00 PM</option>
  <option value="21:30">9:30 PM</option>
  <option value="22:00">10:00 PM</option>
  <option value="22:30">10:30 PM</option>
  <option value="23:00">11:00 PM</option>
  <option value="23:30">11:30 PM</option>
  </select>
<br/><br/>
 <label>End Time:</label>
<input type="date" name="dateEnd"/>
 <select name="timeEnd">
  <option value="00:00">12:00 AM</option>
  <option value="00:30">12:30 AM</option>
  <option value="01:00">01 AM</option>
  <option value="01:30">01:30 AM</option>
  <option value="02:00">02:00 AM</option>
  <option value="02:30">02:30 AM</option>
  <option value="03:00">03:00 AM</option>
  <option value="03:30">03:30 AM</option>
  <option value="04:00">04:00 AM</option>
  <option value="04:30">04:30 AM</option>
  <option value="05:00">05:00 AM</option>
  <option value="05:30">05:30 AM</option>
  <option value="06:00">06:00 AM</option>
  <option value="06:30">06:30 AM</option>
  <option value="07:00">07:00 AM</option>
  <option value="07:30">07:30 AM</option>
  <option value="08:00">08:00 AM</option>
  <option value="08:30">08:30 AM</option>
  <option value="09:00">09:00 AM</option>
  <option value="09:30">09:30 AM</option>
  <option value="10:00">10:00 AM</option>
  <option value="10:30">10:30 AM</option>
  <option value="11:00">11:00 AM</option>
  <option value="11:30">11:30 AM</option>
  <option value="12:00">12:00 PM</option>
  <option value="12:30">12:30 PM</option>
  <option value="13:00">01:00 PM</option>
  <option value="13:30">01:30 PM</option>
  <option value="14:00">02:00 PM</option>
  <option value="14:30">02:30 PM</option>
  <option value="15:00">03:00 PM</option>
  <option value="15:30">03:30 PM</option>
  <option value="16:00">04:00 PM</option>
  <option value="16:30">04:30 PM</option>
  <option value="17:00">5:00 PM</option>
  <option value="17:30">5:30 PM</option>
  <option value="18:00">6:00 PM</option>
  <option value="18:30">6:30 PM</option>
  <option value="19:00">7:00 PM</option>
  <option value="19:30">7:30 PM</option>
  <option value="20:00">8:00 PM</option>
  <option value="20:30">8:30 PM</option>
  <option value="21:00">9:00 PM</option>
  <option value="21:30">9:30 PM</option>
  <option value="22:00">10:00 PM</option>
  <option value="22:30">10:30 PM</option>
  <option value="23:00">11:00 PM</option>
  <option value="23:30">11:30 PM</option>
  </select>
<br/><br/>
<input type="hidden" name="Status" value="unlisted"/>
 <!--<label>Stream Status:</label>
 <select name="Status">
  <option value="public">Public</option>
  <option value="unlisted">Unlisted</option>
  <option value="private">Private</option>
</select> <br/><br/>-->
<label>Participants:</label>
<input type="text" name="emails" placeholder="Comma separated emails"/><br/><br/>
<input type ="submit" value="Submit"/>
</form>
END;
} else {

 $htmlBody = <<<END
 <p>Close this window and click the schedule button for live streaming</p>
<p>You need to <a href="#"  onclick="window.close();">Close Window</a> before proceeding.<p>
END;

}

}
} else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl" onclick="window.open('$authUrl', 'newwindow', 'width=600, height=400'); return false;">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>Bound Live Broadcast</title>
</head>
<body>

  <?=$htmlBody?>
</body>
</html>
Kichu
  • 1,641
  • 4
  • 21
  • 28
  • You can create a livestream with a specified start time by using [insert](https://developers.google.com/youtube/v3/live/docs/liveStreams/insert), but I'm not sure if you would consider that "starting a livestream". I believe you can only use Google+ Hangouts On Air if you livestream through your YouTube channel, not the API. – not_a_bot Sep 14 '15 at 18:33
  • I can able to create events using API.Is it possible to enable the google +Hangout option using youtube API? – Kichu Sep 15 '15 at 03:45
  • I think you might be able to do it if you also use the Google+ Hangouts API. See [here](https://developers.google.com/+/hangouts/button) and [here](https://developers.google.com/+/hangouts/api/gapi.hangout.onair). – not_a_bot Sep 15 '15 at 18:14
  • Is it possible to use google+hangout API with youtube live streaming API? – Kichu Sep 16 '15 at 03:58
  • I think you could, but I'm not quite sure how you would do it. Maybe you can get some information from [this](http://stackoverflow.com/a/27916747/4241842) answer. – not_a_bot Sep 16 '15 at 20:32
  • Hey Kichu did my answer help you? Please let me know if you have any other questions. – JAL Dec 27 '15 at 23:31

1 Answers1

0
  1. To start streaming from your application, you need to transition your liveBroadcast from the active to the live state. After binding your liveStream to your liveBroadcast, call transition on your liveBroadcast from testing to live.

  2. To change the quality options, set the cdn.format of your liveStream object:

Valid values for this property are:

  • 1080p
  • 1080p_hfr: This value is for 60 frames per second (fps), or high-frame-rate, -streams.
  • 240p
  • 360p
  • 480p
  • 720p
  • 720p_hfr: This value is for 60 frames per second (fps), or high-frame-rate, streams.
JAL
  • 41,701
  • 23
  • 172
  • 300
  • Hello @JAL, i'm having issues converting my youtube livestream object from ready to active using the youtube livestream API. I already created a broadcast object and also the livestream object and bind them together with the correct response gotten. But when try to transition it throws an error saying my stream is inactive. Please any assistance would be of great help as my job depends on it. Thank you – Henry9162 Jun 22 '20 at 13:19