如何在PHP中恢复到YouTube的可恢复上传
我正在使用可恢复上传方法将大型文件从服务器传输到YouTube,请参见下面的代码。当我在PHP处理过程中发现错误时,如何保存/恢复上载?更准确地说,上传的实际状态存储在哪里?上传中断后,文件指针放在哪里如何在PHP中恢复到YouTube的可恢复上传,php,algorithm,upload,youtube-api,Php,Algorithm,Upload,Youtube Api,我正在使用可恢复上传方法将大型文件从服务器传输到YouTube,请参见下面的代码。当我在PHP处理过程中发现错误时,如何保存/恢复上载?更准确地说,上传的实际状态存储在哪里?上传中断后,文件指针放在哪里 /* Uploads one file to youtube chunk by chunk */ function uploadFile($dbfile) { $client = $this->client; $you
/*
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 && !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上传类似大小的视频时,你能成功上传吗?@not_a_机器人:谢谢你的关注。嗯,是的,我可以上传文件,但不总是这样。这些文件的大小约为3-5GB。每次失败时是否都会收到特定的错误消息?我想你要找的答案是在你阅读媒体文件并逐块上传的部分。似乎它应该保存最新的块,您可以使用它继续上传视频。如果您的internet连接不太可靠,可以尝试减少$chunkSizeBytes。查看客户端源代码也可能有帮助: