6

動画ファイルを 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)か?

または、MediaFileSource を S3 ファイル自体の URL として入力する必要がありますか?

主に、安定性が必要なだけです(頻繁なタイムアウトの対象となるソリューションではありません)。速度とローカル ストレージはそれほど重要ではありません (ビデオ ファイルがアップロードされたらローカルで削除できます)。

これについて最善の方法は何ですか?

ありがとう!

更新: このスクリプトを実行すると、1 回の実行で約 5 本の動画が YouTube にアップロードされます。

4

4 に答える 4

2

「MediaFileSource」は実際のファイルでなければなりません。URL を必要としないため、動画を YouTube に送信する前に、動画を S3 からサーバーにコピーする必要があります。

使用量が少ない場合は、おそらく「shell_exec」で問題を解決できますが、さまざまな理由から、Zend S3 ServiceまたはcURLを使用して S3 からファイルをプルする方がよいでしょう。

于 2012-04-12T13:15:41.763 に答える
-1

$chunkSizeBytes = 2 * 1024 * 1024; // 2MB

    $s3client = $this->c_aws->getS3Client();
    $s3client->registerStreamWrapper();

    try {

        $client = new \Google_Client();

        $client->setAccessType("offline");
        $client->setApprovalPrompt('force');

        $client->setClientId(GOOGLE_CLIENT_ID);
        $client->setClientSecret(GOOGLE_CLIENT_SECRET);
        $token = $client->fetchAccessTokenWithRefreshToken(GOOGLE_REFRESH_TOKEN);


        $client->setAccessToken($token);

        $youtube = new \Google_Service_YouTube($client);

        // 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($title);
        $snippet->setDescription($summary);
        $snippet->setTags(explode(',', $keywords));

        // 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 = "public";


        // Associate the snippet and status objects with a new video resource.
        $video = new \Google_Service_YouTube_Video();
        $video->setSnippet($snippet);
        $video->setStatus($status);

        // 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);

        $insertRequest = $youtube->videos->insert("status,snippet", $video);

        $media = new \Google_Http_MediaFileUpload(
            $client,
            $insertRequest,
            'video/*',
            null,
            true,
            $chunkSizeBytes
        );

        $result = $this->c_aws->getAwsFile($aws_file_path);

        $media->setFileSize($result['ContentLength']);

        $uploadStatus = false;

        // Seek to the beginning of the stream
        $result['Body']->rewind();

        // Read the body off of the underlying stream in chunks
        while (!$uploadStatus && $data = $result['Body']->read($chunkSizeBytes)) {

            $uploadStatus = $media->nextChunk($data);

        }
        $client->setDefer(false);
        if ($uploadStatus->status['uploadStatus'] == 'uploaded') {
            // Actions to perform for a successful upload
             $uploaded_video_id = $uploadStatus['id'];
            return ($uploadStatus['id']);
        }
    }catch (\Google_Service_Exception $exception){
        return '';
        print_r($exception);
    }
于 2017-04-07T11:37:58.077 に答える