我正在使用YouTube API逐块上传视频(请参见下面的代码)。但是,有时上传失败时会出现较大的文件(+ 1GB),但并非总是如此。上载显示已完成,但只能播放几分钟,其余的将被截断。我做了some research,但是没有明显的成功。我的问题:
非常感谢任何在正确方向上的帮助/领导。我什至要打赏500分,因为这让我发疯了(就这样...)
附录:该脚本通过Gearman服务器在命令行上运行,并设置了
set_time_limit(0);
。代码/功能只是摘录(对于较小的文件,有时甚至高达10GB的文件,都可以很好地运行)。编辑:根据上述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;
}
最佳答案
好吧,这是不可能完成的任务。您需要向他们发送大量邮件,以便(可能)获得答案。我已经尝试了几次,但是他们没有回应。
是的,这是编码问题。如果您尝试上传高清视频,并且视频被截断或缩短或其他任何原因,则是编码问题。 YouTube会定期提供它们。
不,它不能。上传视频后,您需要查看它才能看到错误。在过程的中间或上载的任何部分中都没有异常(exception)。
没关系如果您要同时上传一个视频或两个,三个,五个视频,那就没关系了。这只是上载。在过程中可能发生的唯一不好的事情就是失去连接。每个视频都朝着自己的方向发展。就像您要将多个文件从一个文件夹复制到另一个文件夹一样。
是的。您,我以及其他所有上传者。这是YouTube问题。这是他们的错误或一些编码/渲染/转码,无论他们遇到什么问题。都是因为YouTube处理了各种选择。
发生这种情况时,我的解决方案是在上传视频时使用HTTPS/SSL。而且有效。没有剪切,修剪,转码或编码/渲染问题。
关于php - YouTube API会截断视频[PHP],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33866689/