我无法使用PHP Youtube API v3上传超过8 MB的视频

时间:2017-11-21 01:44:53

标签: php youtube-api

我在我的网站上使用Youtube API v3 PHP脚本。当我上传少于8 MB时,它已成功上传。

但是当我上传超过8 MB时,它没有通过我的网站上传到Youtube。 Youtube API v3 PHP脚本中是否有文件大小限制?

这是我的Youtube上传脚本请任何人帮助我

<?php

    // get tokens and change file name
    require_once 'random.php' ;
    $new_name = swd_rand(11); 

    // Get the .txt token file name in the dir
    $txtfile = glob("*.txt");
        foreach($txtfile as $txtfile)
    {       
        $key = file_get_contents($txtfile);
        $file_title = basename($txtfile, ".txt");
        $new_token_name = $new_name . ".txt" ;
        rename($txtfile, $new_token_name);                                      
    }   



require_once 'google-api-php-client/src/Google/autoload.php'; 
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/YouTube.php';

/////// My API Keys
require_once 'my_api_keys.php' ;
$application_name = $my_app_name ; 
$client_id = $my_client_id ;         
$client_secret = $my_client_secret ;
//////

$scope = array('https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/youtubepartner');   


    $videoTitle = $title;
    $videoDescription =  $desc;
    $videoTags = array($tags);
    $videoStatus = $status;
    $videoCategory = $cat;



try{
    // Client init
    $client = new Google_Client();
    $client->setApplicationName($application_name);
    $client->setClientId($client_id);
    $client->setAccessType('offline');
    $client->setAccessToken($key);
    $client->setScopes($scope);
    $client->setClientSecret($client_secret);

    if ($client->getAccessToken()) {

         // Check to see if our access token has expired. If so, get a new one and save it to file for future use                 
        if($client->isAccessTokenExpired()) {
            $newToken = json_decode($client->getAccessToken());
            $client->refreshToken($newToken->refresh_token);       

            // Update token txt file
            file_put_contents($new_token_name, $client->getAccessToken());
        }

        $youtube = new Google_Service_YouTube($client);

        // Create a snipet with title, description, tags and category id
        $snippet = new Google_Service_YouTube_VideoSnippet();
        $snippet->setTitle($videoTitle);
        $snippet->setDescription($videoDescription);
        $snippet->setCategoryId($videoCategory);
        $snippet->setTags($videoTags);

        // Create a video status with privacy status. Options are "public", "private" and "unlisted".
        $status = new Google_Service_YouTube_VideoStatus();
        $status->setPrivacyStatus($videoStatus);
        //$status->setPrivacyStatus('unlisted');

        // Create a YouTube video with snippet and status
        $video = new Google_Service_YouTube_Video();
        $video->setSnippet($snippet);
        $video->setStatus($status);

        // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
        // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
        $chunkSizeBytes = 1 * 1024 * 1024;    //was 1 * 1024 * 1024

        // 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);

        // Create a request for the API's videos.insert method to create and upload the video.
        $insertRequest = $youtube->videos->insert("status,snippet", $video);

        // Create a MediaFileUpload object for resumable uploads.
        $media = new Google_Http_MediaFileUpload(
            $client,
            $insertRequest,
            'video/*',
            null,
            true,
            $chunkSizeBytes
        );
        $media->setFileSize(filesize($videoPath));

        //////////////// Video Upload to YouTube ////////////////
        // Read the media file and upload it chunk by chunk.
        $status = false;
        $handle = fopen($videoPath, "rb");
        while (!$status && !feof($handle)) {
            $chunk = fread($handle, $chunkSizeBytes);
            $status = $media->nextChunk($chunk);
        }

        fclose($handle);

        // Video has successfully been upload, now lets perform some cleanup functions for this video

        if ($status->status['uploadStatus'] == 'uploaded') {
            // Actions to perform for a successful upload
        }

        // If you want to make other calls after the file upload, set setDefer back to false
        $client->setDefer(false);       

        //>> get the video id        
    //echo $status['snippet']['title'];    
    $video_key=$status['id'] ; 


    } else{
        echo 'Problems creating the client';
    }

} catch(Google_Service_Exception $e) {
    print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage();
    print "Stack trace is ".$e->getTraceAsString();
}catch (Exception $e) {
    print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage();
    print "Stack trace is ".$e->getTraceAsString();
}


?>

0 个答案:

没有答案