我们在 WordPress 网站上运行以下函数,该函数访问 Amazon S3(公共(public))存储桶,并尝试将视频推送到 YouTube 进行处理,并获取一些标题、描述、标签等后元。
除了实际的视频上传之外,该脚本似乎运行良好。我们在 YouTube 上创建了具有正确标题、描述、隐私和标签的视频对象,但视频本身绝对拒绝处理,并且 YouTube 报告永远处理。
下面是上传的相关代码块:
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();
if ( ! empty( $args['title'] ) )
$snippet->setTitle( $args['title'] );
if ( ! empty( $args['description'] ) )
$snippet->setDescription( $args['description'] );
// if ( ! empty( $args['tags'] ) )
// $snippet->setTags( $args['tags'] );
// Numeric video category. See
// https://developers.google.com/youtube/v3/docs/videoCategories/list
$snippet->setCategoryId("28");
// 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 = 50 * 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.
$ytclient->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(
$ytclient,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$videoFileSize = filesize($videoPath);
$media->setFileSize( $videoFileSize );
什么可能导致失败?
最佳答案
您没有发送内容长度,YouTube 正坐在那里等待“更多”内容。
查看您的代码,您正在使用可恢复上传。
更新了更多信息:
来自您的代码
$videoFileSize = filesize($videoPath);
哪里
$videoPath = $s3dir . $args['file_path'];
和
$s3dir is "s3://coughvideo/"
因此,当您在此处执行 setFileSize 时:
$media = new Google_Http_MediaFileUpload(
$ytclient,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$videoFileSize = filesize($videoPath);
$media->setFileSize( $videoFileSize );
非常肯定的文件大小仅适用于本地文件,因此您将无法获取远程 URL 的文件大小。
你将会有一个
- 首先向 Amazon 请求文件大小并使用它
- 将其下载到本地,以便您可以计算出文件大小(不确定您是否想要这样做)。
- 使用下面的 CURL 示例(不确定您的 S3 实例上是否有此选项 - 取决于您的安全性)。
PHP: Remote file size without downloading file
编辑:
将上面的“坏”代码切换为:
$head = get_headers($videoPath,1); // get the header array from Amazon S3
$videoSize = $head['Content-Length']; // Get Mr. FileSize
$media->setFileSize($videoSize); // Set Mr. FileSize
这可以使文件大小就位。
关于php - 从 S3 帐户将视频推送到 YouTube 成功,但视频永远是 "processing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23771138/