douzhuanqian8244 2013-01-22 18:27 采纳率: 100%
浏览 51
已采纳

通过google php 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)

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.

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?

  • 写回答

1条回答 默认 最新

  • dongxing7318 2013-01-27 20:43
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型