gpt4 book ai didi

javascript - 在 Youtube API 中启用直播

转载 作者:行者123 更新时间:2023-11-28 06:56:09 25 4
gpt4 key购买 nike

我正在使用 Youtube API 从 Youtube 中的应用程序创建事件以进行直播。通过以下代码成功完成了这一操作。但我的要求:

1) 我需要使用 youtube 从我的应用程序界面启动直播 API,可以吗?

2) 当我使用 YouTube API 从应用程序创建事件时,它会自动转到 自定义(更多编码选项) 类型。我需要在 Quick 中创建实时流媒体事件(使用 Google+ Hangouts On Air) 类型。可能吗?

我的代码:

<?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>

最佳答案

  1. 要从应用程序开始流式传输,您需要将 liveBroadcast 从事件状态转换为实时状态。将 liveStream 绑定(bind)到 liveBroadcast 后,从 testingliveBroadcast 上调用 transition 生活

  2. 要更改质量选项,请设置 cdn.format您的 liveStream 对象:

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.

关于javascript - 在 Youtube API 中启用直播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32558041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com