Getting a video from S3 and Uploading to YouTube in PHP

后端 未结 4 1060
忘了有多久
忘了有多久 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:25

    This is an old question but i believe i have a better answer.

    You don't have to write video to HDD and you can't keep the whole thing in RAM (I assume it is a big file).

    You can use PHP AWS SDK and Google Client libraries to buffer file from S3 and send it to YouTube on the fly. Use registerStreamWrapper method to register S3 as file system and use resumable uploads from YouTube API. Then all you have to do is reading chunks from S3 with fread and sending them to YouTube. This way you can even limit the RAM usage.

    I assume you created the video object ($video in code) from Google_Video class. This is a complete code.

     S3_ACCESS_KEY,
                        'secret' => S3_SECRET_KEY,
                        'region' => 'eu-west-1' // if you need to set.
                    ));
    $s3client->registerStreamWrapper();
    
    $client = new Google_Client();
    $client->setClientId(YOUTUBE_CLIENT_ID);
    $client->setClientSecret(YOUTUBE_CLIENT_SECRET);
    $client->setAccessToken(YOUTUBE_TOKEN);
    
    $youtube = new Google_YoutubeService($client);
    $media = new Google_MediaFileUpload('video/*', null, true, $chunkSizeBytes);
    
    $filesize = filesize($streamName); // use it as a reguler file.
    $media->setFileSize($filesize);
    
    $insertResponse = $youtube->videos->insert("status,snippet", $video, array('mediaUpload' => $media));
    $uploadStatus = false;
    
    $handle = fopen($streamName, "r");
    $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($insertResponse, $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($insertResponse, $chunkBuffer);
    $totalSend += strlen($chunkBuffer);
    fclose($handle);
    

提交回复
热议问题