Getting a video from S3 and Uploading to YouTube in PHP

后端 未结 4 1111
忘了有多久
忘了有多久 2021-02-06 11:02

I have some code working that uploads a video file up to YouTube:

$yt = new Zend_Gdata_YouTube($httpClient);

// create a new VideoEntry object
$myVideoEntry = n         


        
4条回答
  •  迷失自我
    2021-02-06 11:37

    I had to make some changes to @previous_developer 's answer to make it work with Youtube Data API V3 (Please upvote him as I could not find any working code except his).

    $streamName = 's3://BUCKET-NAME/VIDEO.mp4';
    
    
    /**
    Since I have been using Yii 2. Use the AWS 
    SDK directly instead.
    */
    
        $aws = Yii::$app->awssdk->getAwsSdk();
        $s3client = $aws->createS3();
    
    
        $s3client->registerStreamWrapper();
    
    
        $service = new \Google_Service_YouTube($client);
    
        $snippet = new \Google_Service_YouTube_VideoSnippet();
        $snippet->setTitle("Test title");
        $snippet->setDescription("Test descrition");
        $snippet->setTags(array("tag1","tag2"));
        $snippet->setCategoryId("22");
    
        $status = new \Google_Service_YouTube_VideoStatus();
        $status->privacyStatus = "public";
    
        $video = new \Google_Service_YouTube_Video();
        $video->setSnippet($snippet);
        $video->setStatus($status);
    
        $client->setDefer(true);
        $insertResponse = $service->videos->insert("status,snippet", $video);
    
    
        $media = new MediaFileUpload(
            $client,
            $insertResponse,
            'video/*',
            null,
            true,
            false
        );
    
        $filesize = filesize($streamName); // use it as a reguler file.
        $media->setFileSize($filesize);
    
    
        $chunkSizeBytes = 2 * 1024 * 1024; // 2 mb
    
        $uploadStatus = false;
    
        $handle = fopen($streamName, "r");
        $totalSend = 0;
        $totalReceived = 0;
        $chunkBuffer = '';
        while (!$uploadStatus && !feof($handle)) {
            $chunk = fread($handle, $chunkSizeBytes);
            $chunkBuffer .= $chunk;
            $chunkBufferSize = strlen($chunkBuffer);
            if($chunkBufferSize > $chunkSizeBytes) {
                $fullChunk = substr($chunkBuffer, 0, $chunkSizeBytes);
                $leapChunk = substr($chunkBuffer, $chunkSizeBytes);
                $uploadStatus = $media->nextChunk($fullChunk);
                $totalSend += strlen($fullChunk);
    
                $chunkBuffer = $leapChunk;
                echo PHP_EOL.'Status: '.($totalReceived).' / '.$filesize.' (%'.(($totalReceived / $filesize) * 100).')'.PHP_EOL;
            }
    
            $totalReceived += strlen($chunk);
        }
    
        $extraChunkLen = strlen($chunkBuffer);
        $uploadStatus = $media->nextChunk($chunkBuffer);
        $totalSend += strlen($chunkBuffer);
        fclose($handle);
    
    
    
        // If you want to make other calls after the file upload, set setDefer back to false
        $client->setDefer(false);
    

提交回复
热议问题