通过Google PHP Client API v3将大型视频发布到youtube [英] Post large video to youtube via google php client api v3

查看:102
本文介绍了通过Google PHP Client API v3将大型视频发布到youtube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过最新版本的google client api(v3,最新签出源)将大型视频上传到youtube

I am trying to upload large videos to youtube via the latest version of the google client api (v3, latest checked out source)

我有发布视频的方法,但使它正常工作的唯一方法是将整个视频读取为一个字符串,然后通过data参数将其传递.

I have it posting the videos, but the only way I can get it to work is by reading the entire video into a string, and then passing it via the data parameter.

我当然不想将巨大的文件读入内存,但是api似乎没有其他方法可以做到这一点.似乎期望将字符串作为data参数.下面是我用来发布视频的代码.

I certainly do not want to read gigantic files into memory, but the api seems to offer no other way to do this. It seems to expect a string as the data parameter. Below is the code I'm 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 any way to post the data in chunks, or stream the data in some way so as to avoid reading the entire file into memory?

推荐答案

似乎以前不支持此用例.这是一个与最新版本的Google APIs PHP客户端一起使用的示例(来自

It looks like this use case wasn't supported before. Here's a sample that works with the very latest version of the Google APIs 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);
}

这篇关于通过Google PHP Client API v3将大型视频发布到youtube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆