我正在使用YouTube API逐块上传视频(请参阅下面的代码)。然而,对于较大的文件(+1GB),上传有时会失败,但并非总是如此。上传显示已完成,但只能播放几分钟,其余时间被截断。我做了一些研究,但没有取得明显的成功。我现在的问题:
- 有没有可能直接联系YouTube(查看真实情况的日志)
- 这是编码问题吗
- 是否可以通过API捕获/检测错误(目前没有抛出异常)
- 如果你同时上传不同的视频(即并行上传),会发生这种情况吗
- 其他人遇到过这个问题吗
非常感谢在正确方向上提供的任何帮助/引导。我甚至会悬赏500分,因为这让我疯了(刚刚这么做…)
附录:脚本通过Gearman服务器在命令行上运行,并设置了set_time_limit(0);
。代码/函数只是一个摘录(在较小的文件中运行得很好,有时甚至高达10GB)。
EDIT:根据aeregistal和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;
}
是否有可能直接联系YouTube(查看到底发生了什么)?
这是一项不可能完成的任务。你需要给他们发很多邮件(也许)才能得到答复。我试过几次,但他们没有回应。
这是编码问题吗?
是的,这是编码问题。如果你试图上传高清视频,如果它被截断或缩短或其他什么,这是一个编码问题。YouTube定期发布。
是否可以通过API捕获/检测到错误(目前,否抛出异常)
不,它不能。你需要在上传视频时看到它才能看到错误。在上传过程中或上传的任何部分都没有例外。
如果你同时上传不同的视频(在平行)?
没关系。如果你同时上传一个视频或两个、三个、五个视频,那也没关系。这只是一个上传。在这个过程中唯一可能发生的坏事就是失去连接。每个视频都有自己的方向。这就像是将多个文件从一个文件夹复制到另一个文件夹。
其他人遇到过这个问题吗?
是的。你,我和其他上传者。这是YouTube的问题。这是他们的错误或一些编码/渲染/代码转换,无论他们有什么问题。这都是因为YouTube的处理选择。
当这种情况发生在我身上时,我的解决方案是在上传视频时使用HTTPS/SSL。它奏效了。没有剪切、修剪、代码转换或编码/渲染问题。
看起来脚本超时了。请在第一行尝试此代码:set_time_limit(0);
上传显示已完成,但只能播放几分钟,其余时间被截断。我做了一些研究,但没有取得明显的成功。
这是编码问题吗?
好的。。您正在使用"分块"上传。换句话说:这是一个"可恢复的"上传,如YT上传API中所述。
我的第一个猜测是:这是(其中一个请求的)content-range
标头问题。所有部分都必须在YT服务器端完全按字节对齐,否则您将只得到二进制文件的第一部分。引用:上传区块,请注意内容范围标题上的蓝色框。
google-api-php-client
应正确处理此问题。但错误可能是任何情况:API和客户端不同步,cURL配置问题,未设置头,未正确计算范围。
是否可以通过API捕获/检测错误(目前没有抛出异常)
调试客户端不是你的工作。必须启用GuzzleHttpRequestOptions::DEBUG
才能查看所有标头是否正确。然后,您可以尝试将上传的状态与上传本身并行(第二次狂饮请求)。
有没有可能直接联系YouTube(查看真实情况的日志)?
是的,您正在使用Google_Http_MediaFileUpload
,这是Google-API-PHP-client
的一部分。
只需在他们的Github回购中打开一个问题:https://github.com/google/google-api-php-client/issues
我的建议是:
- 留下PHP洋葱
PHP(ext_curl(libcurl))) + yourscript(google-api-client(guzzle)))
- PHP洋葱的意思是:你的脚本使用谷歌api客户端,它使用guzzle,它使用PHP_ext_curl,它内部使用libcurl
- 您有多个层,所有层都可能发生错误
- 底线:让我们简单地绕过PHP堆栈从CLI进行测试
- 尝试使用
cURL
在CLI上重现分块上传问题 - 使用第二控制台来请求已上传块之间的活动上传的状态
- 然后,如果从CLI上传
- 失败:这表明YT服务器有问题
- 成功:将CLI的头与PHP脚本进行比较(在调试模式下大吃大喝),以更接近问题