通过 YouTube 上传视频文件时卡在 0% API
Video file is stuck at 0% percent when being uploaded via YouTube API
如标题所示,可以通过 YouTube 的上传功能上传和处理视频文件。但是,当我尝试以编程方式上传它时(通过 OAuth2 和 YouTube API v3),它总是停留在 0% 的处理速度。 SO 上有 youtuber 吗?有没有专门针对上传问题的论坛?
(PS,有一个similar question没有结果。)
UPDATED ERROR: 深入挖掘,似乎与视频元数据有关。我偶尔会收到以下错误:
Failed to start the resumable upload (HTTP 400: youtube.video, The
request metadata specifies an invalid video title.)
不幸的是,the error page for YouTube's API v3 并没有真正患上口臭...有人知道这个错误是什么意思吗?
更新代码:
目前,文件是逐块上传的(通常效果很好,但并非总是如此):
function uploadFile($dbfile) {
$client = $this->client;
$youtube = new Google_Service_YouTube($client);
$htmlBody = "";
try {
// Create a snippet with title, description, tags and category ID
// Create an asset resource and set its snippet metadata and type.
// This example sets the video's title, description, keyword tags, and
// video category.
$snippet = new Google_Service_YouTube_VideoSnippet();
$snippet->setTitle($dbfile->displayname);
// Numeric video category. See
// https://developers.google.com/youtube/v3/docs/videoCategories/list
$snippet->setCategoryId("22");
// Set the video's status to "private"
$status = new Google_Service_YouTube_VideoStatus();
$status->privacyStatus = "private";
// Associate the snippet and status objects with a new video resource.
$video = new Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$insertRequest = $youtube->videos->insert("status,snippet", $video);
// Create a MediaFileUpload object for resumable uploads.
$media = new Google_Http_MediaFileUpload(
$client,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($dbfile->localfile));
// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($dbfile->localfile, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
$client->setDefer(false);
$log = array("success" => true, "snippet_id" => $status["id"]);
} catch (Google_ServiceException $e) {
$log = array("success" => false, "errormsg" => $e->getMessage());
} catch (Google_Exception $e) {
$log = array("success" => false, "errormsg" => $e->getMessage());
}
return $log;
}
那么,视频未被处理可能还有其他问题,但我的是要插入的视频标题太长了。 YouTube 的限制是不超过 100 个字符。如果您尝试插入更长的视频标题,则会抛出上述异常。也许他们应该在他们的 API 文档中的某处注明这一点。
如标题所示,可以通过 YouTube 的上传功能上传和处理视频文件。但是,当我尝试以编程方式上传它时(通过 OAuth2 和 YouTube API v3),它总是停留在 0% 的处理速度。 SO 上有 youtuber 吗?有没有专门针对上传问题的论坛? (PS,有一个similar question没有结果。)
UPDATED ERROR: 深入挖掘,似乎与视频元数据有关。我偶尔会收到以下错误:
Failed to start the resumable upload (HTTP 400: youtube.video, The request metadata specifies an invalid video title.)
不幸的是,the error page for YouTube's API v3 并没有真正患上口臭...有人知道这个错误是什么意思吗?
更新代码: 目前,文件是逐块上传的(通常效果很好,但并非总是如此):
function uploadFile($dbfile) {
$client = $this->client;
$youtube = new Google_Service_YouTube($client);
$htmlBody = "";
try {
// Create a snippet with title, description, tags and category ID
// Create an asset resource and set its snippet metadata and type.
// This example sets the video's title, description, keyword tags, and
// video category.
$snippet = new Google_Service_YouTube_VideoSnippet();
$snippet->setTitle($dbfile->displayname);
// Numeric video category. See
// https://developers.google.com/youtube/v3/docs/videoCategories/list
$snippet->setCategoryId("22");
// Set the video's status to "private"
$status = new Google_Service_YouTube_VideoStatus();
$status->privacyStatus = "private";
// Associate the snippet and status objects with a new video resource.
$video = new Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$insertRequest = $youtube->videos->insert("status,snippet", $video);
// Create a MediaFileUpload object for resumable uploads.
$media = new Google_Http_MediaFileUpload(
$client,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($dbfile->localfile));
// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($dbfile->localfile, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
$client->setDefer(false);
$log = array("success" => true, "snippet_id" => $status["id"]);
} catch (Google_ServiceException $e) {
$log = array("success" => false, "errormsg" => $e->getMessage());
} catch (Google_Exception $e) {
$log = array("success" => false, "errormsg" => $e->getMessage());
}
return $log;
}
那么,视频未被处理可能还有其他问题,但我的是要插入的视频标题太长了。 YouTube 的限制是不超过 100 个字符。如果您尝试插入更长的视频标题,则会抛出上述异常。也许他们应该在他们的 API 文档中的某处注明这一点。