gpt4 book ai didi

php - 通过 API 启动 YouTube 直播

转载 作者:行者123 更新时间:2023-12-04 13:33:45 37 4
gpt4 key购买 nike

我在使用 YouTube API 时遇到问题,我正在使用 ("google/apiclient": "2.7")
我创建了广播并将其绑定(bind)到流,然后将 RTMP URL 作为端点添加到我的实时流
但是我找不到在 YouTube 上开始直播的方法(在原始直播开始后)

        $access_token = $data['yt-access-token'];
$title = $data['title'];
$description = $data['description'];
//=======================================//
$client = new Google_Client();
$client->setClientId(env('GOOGLE_APP_ID'));
$client->setClientSecret(env('GOOGLE_SECRET'));
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setAccessToken($access_token);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
//=======================================//
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($title);
$broadcastSnippet->setDescription($description);
$broadcastSnippet->setScheduledStartTime('2020-08-20T00:00:00.000Z');
$broadcastSnippet->setScheduledEndTime('2020-08-25T00: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('public'); //private or public

// 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($title.' 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'],
)
);

$id = $streamsResponse->id;
$rtmp_url = $streamsResponse->cdn->ingestionInfo->ingestionAddress.'/'.$streamsResponse->cdn->ingestionInfo->streamName;

return [
'id' => $id,
'rtmp_url' => $rtmp_url
];

} 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()));
}
我在 YouTube 管理室中找到了这些选项,但在 API 中找不到它们
enter image description here
任何解决方案?

最佳答案

根据文档,您可以使用 LiveBroadcasts resource 的以下两个属性。 :

contentDetails.enableAutoStart (boolean)
Indicates whether this broadcast should start automatically when you start streaming video on the bound live stream.

contentDetails.enableAutoStop (boolean)
Indicates whether this broadcast should stop automatically around one minute after the channel owner stops streaming video on the bound video stream.


这两个属性可以通过两个修改端点 LiveBroadscasts.insert 随意设置。和 Livebroadcasts.update .

W.r.t.您的代码,您必须执行以下操作:
$contentDetails = new Google_Service_YouTube_LiveBroadcastContentDetails();
$contentDetails->setEnableAutoStart(true);
$contentDetails->setEnableAutoStop(true);
然后还有:
$broadcastInsert->setContentDetails($contentDetails);
并将调用替换为 $youtube->liveBroadcasts->insert和:
$broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status,contentDetails', $broadcastInsert, array());

关于php - 通过 API 启动 YouTube 直播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63471154/

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