从S3获取视频并用PHP上传到YouTube [英] Getting a video from S3 and Uploading to YouTube in PHP

查看:111
本文介绍了从S3获取视频并用PHP上传到YouTube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以将视频文件上传到YouTube:

I have some code working that uploads a video file up to YouTube:

$yt = new Zend_Gdata_YouTube($httpClient);

// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

// create a new Zend_Gdata_App_MediaFileSource object
$filesource = $yt->newMediaFileSource('file.mov');
$filesource->setContentType('video/quicktime');
// set slug header
$filesource->setSlug('file.mov');

我在S3中有视频,我想将它们上传到YouTube.我们的S3帐户中的视频是公开的,因此我可以使用wget之类的命令.在运行此脚本(shell_exec("wget ".$s3videoURL))之前,我是否应该运行可获取视频文件并在本地下载的命令?

I have videos in S3 and I want to upload them to YouTube. The video in our S3 account is public, so i can use a command like wget. Should I run a command that wgets the video file and downloads it locally before I run this script (shell_exec("wget ".$s3videoURL))?

还是我应该尝试输入MediaFileSource作为S3文件本身的URL?

Or should I try to enter the MediaFileSource as the URL of the S3 file itself?

主要是,我只需要稳定(而不是经常超时的解决方案);速度和本地存储并不是很重要(上传视频文件后,我可以在本地删除它).

Mainly, I just need stability (not a solution subject to frequent time-outs); speed and local storage isn't really important (I can locally delete video file once its been uploaded).

解决这个问题的最佳方法是什么?

What would be the best way to go about this?

谢谢!

更新:我可能应该提到,此脚本每次执行将向YouTube上传约5个视频.

Update: I should probably mention that this script is going to be uploading about 5 videos to YouTube per execution.

推荐答案

这是一个老问题,但我相信我有一个更好的答案.

This is an old question but i believe i have a better answer.

您不必将视频写入HDD,也不必将整个内容保存在RAM中(我认为这是一个大文件).

You don't have to write video to HDD and you can't keep the whole thing in RAM (I assume it is a big file).

您可以使用PHP AWS开发工具包和Google客户端库来缓冲来自S3的文件并将其动态发送到YouTube.使用registerStreamWrapper方法将S3注册为文件系统,并使用来自YouTube API的断点续传.然后,您要做的就是从f3读取S3的块并将其发送到YouTube.这样,您甚至可以限制RAM的使用.

You can use PHP AWS SDK and Google Client libraries to buffer file from S3 and send it to YouTube on the fly. Use registerStreamWrapper method to register S3 as file system and use resumable uploads from YouTube API. Then all you have to do is reading chunks from S3 with fread and sending them to YouTube. This way you can even limit the RAM usage.

我假设您从Google_Video类创建了视频对象(代码中的$ video).这是完整的代码.

I assume you created the video object ($video in code) from Google_Video class. This is a complete code.

<?php
require_once 'path/to/libraries/aws/vendor/autoload.php';
require_once 'path/to/libraries/google-client-lib/autoload.php';

use Aws\S3\S3Client;

$chunkSizeBytes = 2 * 1024 * 1024; // 2 mb
$streamName = 's3://bucketname/video.mp4';

$s3client = S3Client::factory(array(
                    'key'    => S3_ACCESS_KEY,
                    'secret' => S3_SECRET_KEY,
                    'region' => 'eu-west-1' // if you need to set.
                ));
$s3client->registerStreamWrapper();

$client = new Google_Client();
$client->setClientId(YOUTUBE_CLIENT_ID);
$client->setClientSecret(YOUTUBE_CLIENT_SECRET);
$client->setAccessToken(YOUTUBE_TOKEN);

$youtube = new Google_YoutubeService($client);
$media = new Google_MediaFileUpload('video/*', null, true, $chunkSizeBytes);

$filesize = filesize($streamName); // use it as a reguler file.
$media->setFileSize($filesize);

$insertResponse = $youtube->videos->insert("status,snippet", $video, array('mediaUpload' => $media));
$uploadStatus = false;

$handle = fopen($streamName, "r");
$totalReceived = 0;
$chunkBuffer = '';
while (!$uploadStatus && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $chunkBuffer .= $chunk;
    $chunkBufferSize = strlen($chunkBuffer);
    if($chunkBufferSize > $chunkSizeBytes) {
        $fullChunk = substr($chunkBuffer, 0, $chunkSizeBytes);
        $leapChunk = substr($chunkBuffer, $chunkSizeBytes);
        $uploadStatus = $media->nextChunk($insertResponse, $fullChunk);
        $totalSend += strlen($fullChunk);

        $chunkBuffer = $leapChunk;
        echo PHP_EOL.'Status: '.($totalReceived).' / '.$filesize.' (%'.(($totalReceived / $filesize) * 100).')'.PHP_EOL;
    }

    $totalReceived += strlen($chunk);
}

$extraChunkLen = strlen($chunkBuffer);
$uploadStatus = $media->nextChunk($insertResponse, $chunkBuffer);
$totalSend += strlen($chunkBuffer);
fclose($handle);

这篇关于从S3获取视频并用PHP上传到YouTube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆