正如标题所示,视频文件可以通过 YouTube 的上传功能上传和处理。然而,当我尝试以编程方式上传它(通过 OAuth2 和 YouTube API v3)时,它总是陷入 0% 的处理状态。 SO 上有 youtubers 吗?有没有专门的论坛可以解决上传问题?
(PS,有一个类似的问题 https://stackoverflow.com/questions/15575100/status-of-video-uploaded-via-youtube-data-api-is-always-processing-python没有结果。)
更新错误: 深入挖掘,似乎与视频元数据有关。我偶尔会收到以下错误:
无法启动断点续传(HTTP 400:youtube.video,
请求元数据指定了无效的视频标题。)
很遗憾,YouTube API v3 的错误页面 https://developers.google.com/youtube/v3/docs/errors并没有真正患有流涕......有谁知道这个错误意味着什么?
更新的代码:目前,文件是逐块上传的(通常效果很好,但并非总是如此):
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;
}