Getting a video from S3 and Uploading to YouTube in PHP

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

    $chunkSizeBytes = 2 * 1024 * 1024; // 2 mb

        $s3client = $this->c_aws->getS3Client();
        $s3client->registerStreamWrapper();
    
        try {
    
            $client = new \Google_Client();
    
            $client->setAccessType("offline");
            $client->setApprovalPrompt('force');
    
            $client->setClientId(GOOGLE_CLIENT_ID);
            $client->setClientSecret(GOOGLE_CLIENT_SECRET);
            $token = $client->fetchAccessTokenWithRefreshToken(GOOGLE_REFRESH_TOKEN);
    
    
            $client->setAccessToken($token);
    
            $youtube = new \Google_Service_YouTube($client);
    
            // Create a snippet with title, description, tags and category ID
            // Create an asset resource and set its snippet metadata and type.
            // This example sets the video's title, description, keyword tags, and
            // video category.
            $snippet = new \Google_Service_YouTube_VideoSnippet();
            $snippet->setTitle($title);
            $snippet->setDescription($summary);
            $snippet->setTags(explode(',', $keywords));
    
            // Numeric video category. See
            // https://developers.google.com/youtube/v3/docs/videoCategories/list
    

    // $snippet->setCategoryId("22");

            // Set the video's status to "public". Valid statuses are "public",
            // "private" and "unlisted".
            $status = new \Google_Service_YouTube_VideoStatus();
            $status->privacyStatus = "public";
    
    
            // Associate the snippet and status objects with a new video resource.
            $video = new \Google_Service_YouTube_Video();
            $video->setSnippet($snippet);
            $video->setStatus($status);
    
            // Setting the defer flag to true tells the client to return a request which can be called
            // with ->execute(); instead of making the API call immediately.
            $client->setDefer(true);
    
            $insertRequest = $youtube->videos->insert("status,snippet", $video);
    
            $media = new \Google_Http_MediaFileUpload(
                $client,
                $insertRequest,
                'video/*',
                null,
                true,
                $chunkSizeBytes
            );
    
            $result = $this->c_aws->getAwsFile($aws_file_path);
    
            $media->setFileSize($result['ContentLength']);
    
            $uploadStatus = false;
    
            // Seek to the beginning of the stream
            $result['Body']->rewind();
    
            // Read the body off of the underlying stream in chunks
            while (!$uploadStatus && $data = $result['Body']->read($chunkSizeBytes)) {
    
                $uploadStatus = $media->nextChunk($data);
    
            }
            $client->setDefer(false);
            if ($uploadStatus->status['uploadStatus'] == 'uploaded') {
                // Actions to perform for a successful upload
                 $uploaded_video_id = $uploadStatus['id'];
                return ($uploadStatus['id']);
            }
        }catch (\Google_Service_Exception $exception){
            return '';
            print_r($exception);
        }
    

提交回复
热议问题