YouTube API 截断视频 [PHP]
YouTube API truncates videos [PHP]
我正在使用 YouTube API 逐块上传视频(请参阅下面的代码)。但是,有时上传较大的文件 (+1GB) 会失败,但并非总是如此。上传显示已完成,但只能播放几分钟,其余时间已完成。运行。我做了 some research 但没有明显的成功。我现在的问题:
- 是否可以直接联系 YouTube(查看真实情况的日志)?
- 这是编码问题吗?
- 是否可以通过API报错caught/detected(目前没有抛出异常)
- 如果您同时上传不同的视频(即并行),会发生这种情况吗?
- 还有其他人遇到过这个问题吗?
任何方向正确的 help/lead 都非常感谢。我什至会喊出 500 点的赏金,因为这让我发疯(刚刚做到了......)
附录:脚本在命令行上 运行,通过 Gearman 服务器,设置了 set_time_limit(0);
。 code/function 只是一个摘录(运行 非常适合较小的文件,有时甚至高达 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;
}
看起来脚本超时了。
在第一行尝试此代码:set_time_limit(0);
Is there a possibility to contact YouTube directly (seeing logs of
what's really going on)?
好吧,这是一个不可能完成的任务。您需要向他们发送大量邮件才能(也许)得到答复。试了几次都没有反应。
Is this some encoding issue?
是的,这是编码问题。如果您尝试上传高清视频,如果它被截断或缩短或发生任何其他情况,那是编码问题。 YouTube 会定期提供它们。
Can the error be caught/detected via the API (at the moment, no
exception is thrown)
不,不能。您需要在上传视频时查看视频才能看到错误。在过程中或上传的任何部分都没有异常。
Can this happen if you are uploading different videos at once (in
parallel, that is)?
没关系。如果您同时上传一个视频或两个、三个、五个视频,都没有关系。这只是一个上传。过程中唯一可能发生的坏事是失去连接。每个视频都有自己的方向。就像将多个文件从一个文件夹复制到另一个文件夹一样。
Has anyone else encountered this issue?
是的。你、我和其他所有上传者。这是一个 YouTube 问题。这是他们的错误或一些编码/渲染/转码,无论他们有什么问题。这都是因为 YouTube 的处理选择。
当我遇到这种情况时,我的解决方案是在上传视频时使用 HTTPS/SSL。它奏效了。没有剪切、修剪、转码和编码/渲染问题。
The upload is shown to be complete but only a couple of minutes can be played and the rest is truncated. I did some research but with no apparent success.
Is this some encoding issue?
好的..您正在使用 "chunked" 上传。换句话说:它是 "resumable" 上传,如 YT Upload API.
中所述
我的第一个猜测:它是一个 content-range
header 问题(其中一个请求)。
所有部分都必须在 YT server-side 上完美对齐 byte-wise,否则你最终只会得到二进制文件的第一部分。参考:
Upload Chunks and please note the blue box on Content Range Header.
google-api-php-client
应该能正确处理这个问题。但是 bug-wise 它可以是任何东西:API out-of-sync 客户端,cURL 配置问题,header 未设置,范围计算不正确。
Can the error be caught/detected via the API (at the moment, no exception is thrown)
调试客户端不是你的工作。必须启用 GuzzleHttp\RequestOptions::DEBUG
才能查看所有 header 是否正确。
然后您可以尝试将上传状态与上传本身并行提取(第二个 guzzle 请求)。
Is there a possibility to contact YouTube directly (seeing logs of what's really going on)?
是的,您正在使用 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 onion 的意思是:你的脚本使用google-api-client,它使用guzzle,它使用php_ext_curl,它在内部使用libcurl
- 你有多个层,所有层都可能发生错误
- bottom-line:让我们简单地绕过 PHP 堆栈以从 CLI
进行测试
- 尝试使用
cURL
在 CLI 上重现分块上传问题
- 使用第二个控制台请求上传块之间的活动上传状态
- 然后,如果从 CLI 上传
- 失败:表示 YT 服务器有问题
- 成功:将来自 CLI 的 header 与 PHP 脚本(guzzle 在调试模式下)进行比较以更接近问题
我正在使用 YouTube API 逐块上传视频(请参阅下面的代码)。但是,有时上传较大的文件 (+1GB) 会失败,但并非总是如此。上传显示已完成,但只能播放几分钟,其余时间已完成。运行。我做了 some research 但没有明显的成功。我现在的问题:
- 是否可以直接联系 YouTube(查看真实情况的日志)?
- 这是编码问题吗?
- 是否可以通过API报错caught/detected(目前没有抛出异常)
- 如果您同时上传不同的视频(即并行),会发生这种情况吗?
- 还有其他人遇到过这个问题吗?
任何方向正确的 help/lead 都非常感谢。我什至会喊出 500 点的赏金,因为这让我发疯(刚刚做到了......)
附录:脚本在命令行上 运行,通过 Gearman 服务器,设置了 set_time_limit(0);
。 code/function 只是一个摘录(运行 非常适合较小的文件,有时甚至高达 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;
}
看起来脚本超时了。
在第一行尝试此代码:set_time_limit(0);
Is there a possibility to contact YouTube directly (seeing logs of what's really going on)?
好吧,这是一个不可能完成的任务。您需要向他们发送大量邮件才能(也许)得到答复。试了几次都没有反应。
Is this some encoding issue?
是的,这是编码问题。如果您尝试上传高清视频,如果它被截断或缩短或发生任何其他情况,那是编码问题。 YouTube 会定期提供它们。
Can the error be caught/detected via the API (at the moment, no exception is thrown)
不,不能。您需要在上传视频时查看视频才能看到错误。在过程中或上传的任何部分都没有异常。
Can this happen if you are uploading different videos at once (in parallel, that is)?
没关系。如果您同时上传一个视频或两个、三个、五个视频,都没有关系。这只是一个上传。过程中唯一可能发生的坏事是失去连接。每个视频都有自己的方向。就像将多个文件从一个文件夹复制到另一个文件夹一样。
Has anyone else encountered this issue?
是的。你、我和其他所有上传者。这是一个 YouTube 问题。这是他们的错误或一些编码/渲染/转码,无论他们有什么问题。这都是因为 YouTube 的处理选择。
当我遇到这种情况时,我的解决方案是在上传视频时使用 HTTPS/SSL。它奏效了。没有剪切、修剪、转码和编码/渲染问题。
The upload is shown to be complete but only a couple of minutes can be played and the rest is truncated. I did some research but with no apparent success.
Is this some encoding issue?
好的..您正在使用 "chunked" 上传。换句话说:它是 "resumable" 上传,如 YT Upload API.
中所述我的第一个猜测:它是一个 content-range
header 问题(其中一个请求)。
所有部分都必须在 YT server-side 上完美对齐 byte-wise,否则你最终只会得到二进制文件的第一部分。参考:
Upload Chunks and please note the blue box on Content Range Header.
google-api-php-client
应该能正确处理这个问题。但是 bug-wise 它可以是任何东西:API out-of-sync 客户端,cURL 配置问题,header 未设置,范围计算不正确。
Can the error be caught/detected via the API (at the moment, no exception is thrown)
调试客户端不是你的工作。必须启用 GuzzleHttp\RequestOptions::DEBUG
才能查看所有 header 是否正确。
然后您可以尝试将上传状态与上传本身并行提取(第二个 guzzle 请求)。
Is there a possibility to contact YouTube directly (seeing logs of what's really going on)?
是的,您正在使用 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 onion 的意思是:你的脚本使用google-api-client,它使用guzzle,它使用php_ext_curl,它在内部使用libcurl
- 你有多个层,所有层都可能发生错误
- bottom-line:让我们简单地绕过 PHP 堆栈以从 CLI 进行测试
- 尝试使用
cURL
在 CLI 上重现分块上传问题
- 使用第二个控制台请求上传块之间的活动上传状态
- 然后,如果从 CLI 上传
- 失败:表示 YT 服务器有问题
- 成功:将来自 CLI 的 header 与 PHP 脚本(guzzle 在调试模式下)进行比较以更接近问题