Jan*_*Jan 9 php youtube youtube-api
如标题所示,可以通过YouTube的上传功能上传和处理视频文件.但是,当我尝试以编程方式(通过OAuth2和YouTube API v3)上传时,它始终处于0%处理状态.在SO上有任何youtubers吗?是否有一些特殊的上传问题论坛?(PS,有一个类似的问题没有结果.)
更新错误:深入挖掘,似乎与视频元数据有关.偶尔会出现以下错误:
无法启动可恢复上传(HTTP 400:youtube.video,请求元数据指定无效的视频标题.)
不幸的是,YouTube的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;
}
Run Code Online (Sandbox Code Playgroud)
那么,视频未处理可能还有其他问题,但我的问题是要插入的视频标题太长。YouTube 的字符数限制为不超过 100 个。如果您尝试插入更长的视频标题,则会引发上述异常。也许他们应该在 API 文档中的某个地方注明这一点。
| 归档时间: |
|
| 查看次数: |
2962 次 |
| 最近记录: |