【发布时间】:2016-02-25 07:15:22
【问题描述】:
我正在使用 YouTube API 逐块上传视频(参见下面的代码)。但是,上传有时会因文件较大 (+1GB) 而失败,但并非总是如此。上传显示为已完成,但只能播放几分钟,其余部分被截断。我做了some research,但没有明显的成功。我现在的问题:
- 是否可以直接联系 YouTube(查看真实情况的日志)?
- 这是编码问题吗?
- 能否通过API捕获/检测错误(目前不抛出异常)
- 如果您同时(即并行)上传不同的视频,会发生这种情况吗?
- 还有其他人遇到过这个问题吗?
非常感谢任何朝着正确方向提供的帮助/引导。我什至会提出 500 分的赏金,因为这让我发疯(刚刚做到了......)
附录:脚本通过 Gearman 服务器在命令行上运行,并设置了set_time_limit(0);。代码/函数只是一个摘录(在较小的文件中运行良好,有时甚至高达 10GB)。
编辑:根据上面的 aergistal 和 GeorgeQ 的 cmets,我已将 while 循环更改为直接读取块(不再是 feof())并将状态保存到数据库中。
/*
Uploads one file to youtube chunk by chunk
*/
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("SO Example");
// Numeric video category. See
// https://developers.google.com/youtube/v3/docs/videoCategories/list
$snippet->setCategoryId("22");
// Set the video's status to "public". Valid statuses are "public",
// "private" and "unlisted".
$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);
// Specify the size of each chunk of data, in bytes. Set a higher value for
// reliable connection as fewer chunks lead to faster uploads. Set a lower
// value for better recovery on less reliable connections.
$chunkSizeBytes = 1 * 1024 * 1024;
// Setting the defer flag to true tells the client to return a request which can be called
// with ->execute(); instead of making the API call immediately.
$client->setDefer(true);
// Create a request for the API's videos.insert method to create and upload the video.
$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 && ($chunk = (fread($handle, $chunkSizeBytes))) !== FALSE) {
$status = $media->nextChunk($chunk);
$data = array("filename" => $dbfile->localfile, "status" => print_r($status, true));
$db->saveLog($data);
}
/* the old code
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
*/
fclose($handle);
// If you want to make other calls after the file upload, set setDefer back to false
$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;
}
【问题讨论】:
-
你是如何运行脚本的?通过浏览器或命令行?
-
@DaImTo 命令行,Gearman 服务器。我已更改问题以反映附加信息。
-
附注Why it's bad to use feof() to control a loop。如果文件指针由于某种原因无效,您甚至可以创建无限循环。
-
@aergistal 谢谢,那么在这种特殊情况下该怎么做呢?
-
尝试在
$status = $media->nextChunk($chunk);之后记录$status的值。
标签: php youtube youtube-api