Jan*_*Jan 30 php youtube youtube-api
我使用YouTube API按块上传视频块(请参阅下面的代码).但是,上传有时会因较大的文件(+ 1GB)而失败,但并非总是如此.上传显示已完成,但只能播放几分钟,其余内容将被截断.我做了一些研究,但没有取得明显的成功.我的问题现在:
非常感谢任何正确方向的帮助/领导.我甚至会打出500分的赏金,因为这让我发疯了(刚刚完成......)
附录:脚本在命令行上通过Gearman Server运行,并带有set_time_limit(0);set.代码/功能只是一个提取(运行较小的文件,有时甚至高达10GB).
编辑:根据airgistal和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;
}
Run Code Online (Sandbox Code Playgroud)
是否有可能直接联系YouTube(查看实际情况的日志)?
嗯,这是一个不可能完成的任务.您需要向他们发送大量邮件(或许)获得答案.我试了好几次,但没有回复.
这是一些编码问题吗?
是的,这是编码问题.如果您尝试上传高清视频,如果它被截断或缩短或者无论如何,这都是编码问题.YouTube定期提供这些内容.
可以通过API捕获/检测错误(此刻,不会抛出异常)
不,它不能.您需要在上传视频时看到该视频才能看到错误.在流程的中间或上传的任何部分都不例外.
如果您一次上传不同的视频(并行,即),会发生这种情况吗?
没关系.如果您同时上传一个视频或两个,三个,五个视频,则无关紧要.这只是一个上传.只有在过程中可能发生的坏事才是失去联系.每个视频都走向自己的方向.就像将多个文件从一个文件夹复制到另一个文件夹一样.
还有其他人遇到过这个问题吗?
是.你,我和其他一大堆上传者.这是一个YouTube问题.这是他们的错误或一些编码/渲染/转码,无论他们有什么问题.这完全是因为YouTube处理选择.
当发生这种情况时,我的解决方案是在上传视频时使用HTTPS/SSL.它奏效了.没有剪切,修剪,转码,也没有编码/渲染问题.
| 归档时间: |
|
| 查看次数: |
838 次 |
| 最近记录: |