发布大型视频到YouTube通过谷歌的PHP客户端API V3(Post large video t

2019-07-18 13:15发布

我试图通过谷歌的客户端API的最新版本的大型视频上传到YouTube(V3,最新检出的代码)

我把它发布的视频,但我可以让它开始工作的唯一方法是通过读取整个视频转换成字符串,然后通过数据参数传递给它。

我当然不想读巨大的文件到内存,但是API似乎提供没有其他办法可以做到这一点。 这似乎期待一个字符串作为data参数。 下面是我使用上发布该视频的代码。

$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"));

有没有办法在块以某种方式发布数据,或者流中的数据,从而避免整个文件读入内存?

Answer 1:

它看起来像这种使用情况并没有之前的支持。 下面是使用了最新版本的谷歌的API的PHP客户端(从工作的样本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);
}


文章来源: Post large video to youtube via google php client api v3