Post big video to youtube via google php client api v3

I am trying to upload large videos to youtube using the latest google api client (v3, latest verified source)

I have a video post, but the only way to get it to work is to read the entire video into a string and then pass it through a data parameter.

I certainly don't want to read giant files in memory, but the api doesn't seem to offer any other way to do this. The string seems to be expected to be as a parameter data

. Below is the code I am using to post the video.

$snippet = new Google_VideoSnippet();
$snippet->setTitle("Test title2");
$snippet->setDescription("Test descrition");
$snippet->setTags(array("tag1", "tag2"));
$snippet->setCategoryId("22");

$status = new Google_VideoStatus();
$status->privacyStatus = "private";

$video = new Google_Video();
$video->setSnippet($snippet);
$video->setStatus($status);

$videoData = file_get_contents($pathToMyFile);
$youtubeService->videos->insert("status,snippet", $video, array("data" => $videoData, "mimeType" => "video/mp4"));

      

Is there a way to post the data in chunks, or pass the data in some way to avoid reading the entire file into memory?

+3


source to share


1 answer


It looks like this use case was not supported before. Here's a sample that works with the most recent version of the Google API PHP Client (from https://code.google.com/p/google-api-php-client/source/checkout ).



if ($client->getAccessToken()) {
  $videoPath = "path/to/foo.mp4";
  $snippet = new Google_VideoSnippet();
  $snippet->setTitle("Test title2");
  $snippet->setDescription("Test descrition");
  $snippet->setTags(array("tag1", "tag2"));
  $snippet->setCategoryId("22");

  $status = new Google_VideoStatus();
  $status->privacyStatus = "private";

  $video = new Google_Video();
  $video->setSnippet($snippet);
  $video->setStatus($status);

  $chunkSizeBytes = 1 * 1024 * 1024;
  $media = new Google_MediaFileUpload('video/mp4', null, true, $chunkSizeBytes);
  $media->setFileSize(filesize($videoPath));

  $result = $youtube->videos->insert("status,snippet", $video,
      array('mediaUpload' => $media));

  $status = false;
  $handle = fopen($videoPath, "rb");
  while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $uploadStatus = $media->nextChunk($result, $chunk);
  }

  fclose($handle);
}

      

+4


source







All Articles