YouTube实时流API中的cam在哪里? [英] Where is cam in YouTube live streaming API?

查看:87
本文介绍了YouTube实时流API中的cam在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法加载我的视频实时流式传输在YouTube上.我浏览了YouTube文档,尝试在Google上搜索,但没有获得成功的结果.

I am not able to load my video live streaming on YouTube. I have gone through the YouTube documentation, tried searching on Google but no result to get success.

我有一个运行完整的PHP示例代码,可以在YouTube上实时流式传输视频,并且正在广播视频,但消息却是

I have a complete running PHP example code to stream video live on YouTube and video is broadcasting but message is

请待命,马上开始

Please stand by, Starting Soon

我正在使用此代码

<?php

// Call set_include_path() as needed to point to your client library.
require_once 'Google/Service/Resource.php';
require_once 'Google/Service.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();

/*
 * 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.
 */
$OAUTH2_CLIENT_ID = '**************';
$OAUTH2_CLIENT_SECRET = '****************';

$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()) {
  try {
    // 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('New Broadcast');
    $broadcastSnippet->setScheduledStartTime('2015-04-15T00:00:00.000Z');
    $broadcastSnippet->setScheduledEndTime('2015-04-16T00:00:00.000Z');

    // 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('private');

    // 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 = '';
    $htmlBody .= "<h3>Added Broadcast</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>';

  } catch (Google_ServiceException $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()));
  }

  $_SESSION['token'] = $client->getAccessToken();
} 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">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>Bound Live Broadcast</title>
</head>
<body>
  <?=$htmlBody?>
</body>
</html>

  1. 使用代码段和状态(即插入方法)创建广播.
  2. 使用CDN(1080p,rtmp)和带有摘要插入的代码段创建实时流.
  3. 使用广播绑定方法将它们绑定在一起.

当我进入https://www.youtube.com/my_live_events时,它显示了创建的新事件,但我没有发现缺少使用实时流的cam的地方.

When I go to my https://www.youtube.com/my_live_events it is showing new event created, but I am not getting where is cam missing to use live streaming.

推荐答案

在步骤2中创建Live Stream对象时,将在响应中的cdn-> ingestionInfo下返回ingestionAddress.这是您的RTMP端点,您需要使用返回的streamName作为流密钥来发送视频.

When you create your Live Stream object in step 2, an ingestionAddress will be returned in the response under cdn -> ingestionInfo. This is your RTMP endpoint where you need to send your video, using the streamName returned as the stream key.

您的回复将类似于以下内容(我混淆了我的一些个人YouTube信息):

Your response will look something like this (I've obfuscated some of my personal YouTube info):

{
    cdn =     {
        format = 480p;
        ingestionInfo =         {
            backupIngestionAddress = "rtmp://b.rtmp.youtube.com/live2?backup=1";

            ingestionAddress = "rtmp://a.rtmp.youtube.com/live2";
            streamName = "xxxxxx";
        };
        ingestionType = rtmp;
    };
    etag = "\"xxxxxx\"";
    id = "xxxxxx";
    kind = "youtube#liveStream";
    snippet =     {
        channelId = "xxxxxx";
        description = "";
        publishedAt = "2015-04-15T14:33:43.000Z";
        title = "live_stream_test";
    };
}

相关文档此处.

这篇关于YouTube实时流API中的cam在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆