// REPLACE with the path to your file that you want to upload $videoPath = "/path/to/file.mp4"; // Create a snipet with title, description, tags and category id $snippet = new Google_VideoSnippet(); $snippet->setTitle("Test title"); $snippet->setDescription("Test description"); $snippet->setTags(array("tag1", "tag2")); // Numeric video category. See // https://developers.google.com/youtube/v3/docs/videoCategories/list $snippet->setCategoryId("22"); // Create a video status with privacy status. Options are "public", "private" and "unlisted". $status = new Google_VideoStatus(); $status->privacyStatus = "public"; // Create a YouTube video with snippet and status $video = new Google_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; // Create a MediaFileUpload with resumable uploads $media = new Google_MediaFileUpload('video/*', null, true, $chunkSizeBytes); $media->setFileSize(filesize($videoPath)); // Create a video insert request $insertResponse = $youtube->videos->insert("status,snippet", $video, array('mediaUpload' => $media)); $uploadStatus = false; // Read file and upload chunk by chunk $handle = fopen($videoPath, "rb"); while (!$uploadStatus && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $uploadStatus = $media->nextChunk($insertResponse, $chunk);
// Since a unique video id is given, it will only return 1 video. $video = $videoList[0]; $videoSnippet = $video['snippet']; $tags = $videoSnippet['tags']; // $tags is null if the video didn't have any tags, so we will check for this and // create a new list if needed if (is_null($tags)) { $tags = array("tag1", "tag2"); } else { array_push($tags, "tag1", "tag2"); } // Construct the Google_Video with the updated tags, hence the snippet $updateVideo = new Google_Video($video); $updateSnippet = new Google_VideoSnippet($videoSnippet); $updateSnippet->setTags($tags); $updateVideo->setSnippet($updateSnippet); // Create a video update request $updateResponse = $youtube->videos->update("snippet", $updateVideo); $responseTags = $updateResponse['snippet']['tags']; $htmlBody .= "<h3>Video Updated</h3><ul>"; $htmlBody .= sprintf('<li>Tags "%s" and "%s" added for video %s (%s) </li>', array_pop($responseTags), array_pop($responseTags), $videoId, $updateResponse['snippet']['title']); $htmlBody .= '</ul>'; } } catch (Google_ServiceException $e) { $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } catch (Google_Exception $e) { $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } $_SESSION['token'] = $client->getAccessToken(); } else { // If the user hasn't authorized the app, initiate the OAuth flow