YouTube API 截断视频 [PHP]
我正在使用YouTube API逐块上传视频(请参阅下面的代码)。但是,对于较大的文件(+ 1GB),上传有时会失败,但并非总是如此。上传显示为已完成,但只能播放几分钟,其余部分将被截断。我做了一些研究,但没有明显的成功。我现在的问题:
- 有没有可能直接联系YouTube(查看真正发生的事情的日志)?
- 这是编码问题吗?
- 是否可以通过API捕获/检测到错误(目前,不会引发异常)
- 如果您一次上传不同的视频(即并行),是否会发生这种情况?
- 是否有其他人遇到过此问题?
任何朝着正确方向的帮助/领导都非常感谢。我甚至会喊出500分的赏金,因为这让我发疯(刚刚做到了......)
附录:脚本在命令行上运行,通过Gearman服务器,设置。代码/函数只是一个提取(对于较小的文件,有时甚至高达10GB)运行良好)。set_time_limit(0);
编辑:根据上面的 aergistal 和 GeorgeQ 的评论,我已将 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;
}