APIによるYouTubeライブストリーミングを開始します

Aug 18 2020

YouTube APIで問題が発生しています。使用しています( "google / apiclient": "2.7")

ブロードキャストを作成してストリームにバインドし、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では見つかりませんでした

解決策はありますか?

回答

1 stvar Aug 23 2020 at 18:40

ドキュメントによると、次の2つのプロパティを自由に使用できますLiveBroadcasts resource。

contentDetails.enableAutoStart(boolean)
バインドされたライブストリームでビデオのストリーミングを開始したときに、このブロードキャストを自動的に開始するかどうかを示します。

contentDetails.enableAutoStop(boolean)
チャネル所有者がバインドされたビデオストリームでのビデオのストリーミングを停止してから約1分後にこのブロードキャストを自動的に停止するかどうかを示します。

これらの2つのプロパティは、2つの変更エンドポイントLiveBroadscasts.insertとを介して自由に設定できますLivebroadcasts.update。


コードを作成するには、次のようなことを行う必要があります。

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