Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to upload LARGE files on YouTube
    text
    copied!<p>I have tried two methods to upload large files on YouTube but none of them works, each has its own issues, my aim is to find a correct answer to upload large files. </p> <p><strong>First Method:</strong> </p> <p>It chunks the large files into different parts, and rather than sending them separately and upload them at once it uploads them separately. So at the end I have a long list of unloadable different parts of the file on Youtube rather than a single file.</p> <p><strong>Code of method 1</strong></p> <pre><code>if ($client-&gt;getAccessToken()) { $videoPath = "path/to/foo.mp4"; $snippet = new Google_VideoSnippet(); $snippet-&gt;setTitle("Test title2"); $snippet-&gt;setDescription("Test descrition"); $snippet-&gt;setTags(array("tag1", "tag2")); $snippet-&gt;setCategoryId("22"); $status = new Google_VideoStatus(); $status-&gt;privacyStatus = "private"; $video = new Google_Video(); $video-&gt;setSnippet($snippet); $video-&gt;setStatus($status); $chunkSizeBytes = 1 * 1024 * 1024; $media = new Google_MediaFileUpload('video/mp4', null, true, $chunkSizeBytes); $media-&gt;setFileSize(filesize($videoPath)); $result = $youtube-&gt;videos-&gt;insert("status,snippet", $video, array('mediaUpload' =&gt; $media)); $status = false; $handle = fopen($videoPath, "rb"); while (!$status &amp;&amp; !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $uploadStatus = $media-&gt;nextChunk($result, $chunk); } fclose($handle); } </code></pre> <p>I found this <a href="https://stackoverflow.com/questions/2126330/uploading-large-video-via-youtube-api-causing-out-of-memory">question</a> which its answer code is similar to google_mediaFileupload but I am not sure how to use it.</p> <p><strong>Second method</strong> </p> <p>I am also trying to use resumable upload to upload them but when I use the following code it runs into following error: </p> <p><strong>Code of method 2</strong> </p> <pre><code>&lt;?php require_once 'google-api-php-client/src/Google_Client.php'; require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php'; require_once 'google-api-php-client/src/service/Google_MediaFileUpload.php'; session_start(); $client = new Google_Client(); $client-&gt;setApplicationName('Google+ PHP Starter Application'); $client-&gt;setClientId('My Client ID'); $client-&gt;setClientSecret('My secret code'); $client-&gt;setRedirectUri('http://localhost:8888/mymediaapp2/uploadvideo.php'); $client-&gt;setDeveloperKey('My developer key'); $client-&gt;setScopes("https://www.googleapis.com/auth/youtube.upload"); $youTubeService = new Google_YoutubeService($client); if (isset($_GET['code'])) { $client-&gt;authenticate(); $_SESSION['token'] = $client-&gt;getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } if (isset($_SESSION['token'])) { $client-&gt;setAccessToken($_SESSION['token']); } if ($client-&gt;getAccessToken()) { echo "here"; $json = $client-&gt;getAccessToken(); $jsonIterator = new RecursiveIteratorIterator( new RecursiveArrayIterator(json_decode($json, TRUE)), RecursiveIteratorIterator::SELF_FIRST); $myvar = ""; foreach ($jsonIterator as $key =&gt; $val) { if($key == "access_token") echo "val is" .$val; } $service_url = 'https://www.googleapis.com/upload/youtube/v3/videos? uploadType=resumable&amp;part=snippet,status'; $c = curl_init($service_url); $curl_post_data = array( "Authorization" =&gt; $val, "Content-Length" =&gt; '255', "Content-Type" =&gt; 'application/json; charset=UTF-8', "X-Upload-Content-Length" =&gt; '30000', "X-Upload-Content-Type" =&gt; 'video/mov' ); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_POSTFIELDS, $curl_post_data); $curl_resp = curl_exec($c); curl_close($c); echo $curl_resp; }else { $authUrl = $client-&gt;createAuthUrl(); print "&lt;a href='$authUrl'&gt;upload&lt;/a&gt;"; } ?&gt; </code></pre> <p><strong>Error of method 2</strong> </p> <pre><code>{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } } </code></pre> <p><strong>Please note,</strong> I have given 50 bounty to one of the answers for solving one of the issues of the code. Although the problem is not completely solved yet.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload