视频上传停止工作 - Youtube API

问题描述 投票:1回答:2

我使用PHP脚本将一些日常视频上传到Youtube频道(基于此代码示例:https://developers.google.com/youtube/v3/code_samples/php#resumable_uploads

问题出在这个循环之后:

// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($videoPath, "rb");
while (!$status && !feof($handle)) {
  $chunk = fread($handle, $chunkSizeBytes);
  $status = $media->nextChunk($chunk);
}

通常情况下,$ status变量在上传完成后具有视频ID($ status ['id']),但自1月中旬以来,所有上传都失败并出现以下错误之一:

当我访问Youtube频道时,我可以看到状态为“准备上传”的新视频或者固定百分比。

我的代码几个月没有变化,但现在我无法上传任何新视频。

任何人都可以帮我知道什么是错的?提前致谢!

php youtube-api youtube-api-v3
2个回答
0
投票

也许你可以尝试这样,然后让MediaFileUpload切割块本身:

$media = new \Google_Http_MediaFileUpload(
   $client,
   $insertRequest,
   'video/*',
   file_get_contents($pathToFile),  <== put file content instead of null
   true,
   $chunkSizeBytes
);
$media->setFileSize($size);

$status = false;
while (!$status) {
   $status = $media->nextChunk();
}

1
投票

如果你需要实现一个进度指示器,那么@pom提出的解决方案(顺便说一下)并没有真正解决这个问题。

我面临着与@astor相同的问题;在调用->nextChunk获取最后一块之后我得到:

上传的字节数必须等于或大于262144,最终请求除外(建议它是262144的整数倍)。收到的请求包含38099字节,不符合此要求。

See this log file

代码是来自google/google-api-php-client doc的复制粘贴。日志文件显示第一个块的大小比其他块(最后一个除外)略大,我无法解释。另一个“奇怪”的事情是它的尺寸在测试后改变了测试。最后一个块的大小似乎是正确的,最后应该上传所有字节。但是,上传最后一个块->nextChunk($chunk)中的剩余字节会引发此错误。

一个重要的精度是我的源文件在AWS S3上。文件操作(filesize,fread,fopen)使用Amazon S3 Stream Wrapper完成。它可能会添加一些导致问题的标头或IDK。编辑:我没有本地文件的问题

有没有人遇到同样的问题? ?

...
  $chunkSizeBytes = 5 * 1024 * 1024;
  $client->setDefer(true);
  $request = $service->files->create($file);

  $media = new Google_Http_MediaFileUpload(
      $client,
      $request,
      'text/plain',
      null,
      true,
      $chunkSizeBytes
  );
  $media->setFileSize(filesize(TESTFILE));
  // Upload the various chunks. $status will be false until the process is
  // complete.
  $status = false;
  $handle = fopen(TESTFILE, "rb");
  while (!$status && !feof($handle)) {
    // read until you get $chunkSizeBytes from TESTFILE
    // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
    // An example of a read buffered file is when reading from a URL
    $chunk = readVideoChunk($handle, $chunkSizeBytes);
    $status = $media->nextChunk($chunk);
  }
  // The final value of $status will be the data from the API for the object
  // that has been uploaded.
  $result = false;
  if ($status != false) {
    $result = $status;
  }
  fclose($handle);
}
function readVideoChunk ($handle, $chunkSize)
{
    $byteCount = 0;
    $giantChunk = "";
    while (!feof($handle)) {
        // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
        $chunk = fread($handle, 8192);
        $byteCount += strlen($chunk);
        $giantChunk .= $chunk;
        if ($byteCount >= $chunkSize)
        {
            return $giantChunk;
        }
    }
    return $giantChunk;
}
© www.soinside.com 2019 - 2024. All rights reserved.