【发布时间】:2015-09-29 19:23:44
【问题描述】:
正如标题所示,可以通过 YouTube 的上传功能上传和处理视频文件。但是,当我尝试以编程方式(通过 OAuth2 和 YouTube API v3)上传它时,它总是卡在 0% 的处理速度上。 SO上有youtuber吗?有上传问题的专门论坛吗? (PS,有一个similar question没有结果。)
UPDATED ERROR:深入挖掘,似乎与视频元数据有关。我偶尔会收到以下错误:
无法启动可恢复上传(HTTP 400:youtube.video,The 请求元数据指定了无效的视频标题。)
不幸的是,the error page for YouTube's API v3 并没有真正患上 logorrhoea... 有谁知道这个错误是什么意思?
更新代码: 目前文件是逐块上传的(通常效果很好,但并非总是如此):
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;
}
【问题讨论】:
-
您应该发布MCVE。您究竟是如何以编程方式执行此操作的?
-
我已更新问题并附上相关代码。
-
您的网络连接稳定吗?您可能希望将
chunkSizeBytes设置得更低,以便在不太可靠的连接上更好地恢复。除此之外,我能想到的唯一可能出现问题的地方是您设置文件大小时。您是否尝试过调试以更好地了解问题所在? -
感谢您对此进行调查。嗯,确实是这样(我们确实有一个 250 兆位的骨干网)。不过,我会尝试减小块大小。
-
是真的没有上传还是只是没有收到进度更新?后者也是通过通常的 YT 用户界面上传时的常见问题。您可能希望在
while()循环中插入一些调试输出。
标签: php youtube youtube-api