如何在YouTube上上传LARGE文件

时间:2013-06-01 12:26:22

标签: php upload google-api youtube-api google-api-php-client

我尝试了两种方法在YouTube上上传大文件,但没有一种方法有效,每种方法都有自己的问题,我的目的是找到一个正确的答案来上传大文件。

第一种方法:

它将大文件分成不同的部分,而不是单独发送它们并立即上传它们单独上传它们。所以最后我在Youtube上有一长串无法加载的文件,而不是一个文件。

方法1的代码

if ($client->getAccessToken()) {
  $videoPath = "path/to/foo.mp4";
  $snippet = new Google_VideoSnippet();
  $snippet->setTitle("Test title2");
  $snippet->setDescription("Test descrition");
  $snippet->setTags(array("tag1", "tag2"));
  $snippet->setCategoryId("22");

  $status = new Google_VideoStatus();
  $status->privacyStatus = "private";

  $video = new Google_Video();
  $video->setSnippet($snippet);
  $video->setStatus($status);

  $chunkSizeBytes = 1 * 1024 * 1024;
  $media = new Google_MediaFileUpload('video/mp4', null, true, $chunkSizeBytes);
  $media->setFileSize(filesize($videoPath));

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

  $status = false;
  $handle = fopen($videoPath, "rb");
  while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $uploadStatus = $media->nextChunk($result, $chunk);
  }

  fclose($handle);
}

我发现这个question的答案代码与google_mediaFileupload相似,但我不知道如何使用它。

第二种方法

我也尝试使用可恢复上传来上传它们但是当我使用以下代码时会遇到以下错误:

方法2的代码

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php';
require_once 'google-api-php-client/src/service/Google_MediaFileUpload.php';
session_start();

$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('My Client ID');
$client->setClientSecret('My secret code');
$client->setRedirectUri('http://localhost:8888/mymediaapp2/uploadvideo.php');
$client->setDeveloperKey('My developer key');
$client->setScopes("https://www.googleapis.com/auth/youtube.upload");
$youTubeService = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
echo "here";
$json = $client->getAccessToken();
$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);
$myvar = "";
foreach ($jsonIterator as $key => $val) {
   if($key == "access_token")
    echo "val is" .$val;
}

    $service_url = 'https://www.googleapis.com/upload/youtube/v3/videos?
    uploadType=resumable&part=snippet,status';
    $c = curl_init($service_url);
    $curl_post_data = array(
        "Authorization" => $val,
        "Content-Length" => '255',
        "Content-Type" => 'application/json; charset=UTF-8',
        "X-Upload-Content-Length" => '30000',
        "X-Upload-Content-Type" => 'video/mov'
    );

    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $curl_post_data);
    $curl_resp = curl_exec($c);

    curl_close($c);
    echo $curl_resp;
}else {
  $authUrl = $client->createAuthUrl();
  print "<a href='$authUrl'>upload</a>";
}



?>

方法2错误

{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login 
Required", "locationType": "header", "location": "Authorization" } ], "code": 401, 
"message": 
"Login Required" } } 

请注意,我已经为解决其中一个问题的答案之一提供了50个赏金。虽然问题尚未完全解决。

3 个答案:

答案 0 :(得分:0)

您应该在Dev Console中创建一个项目,并从该控制台获取您的客户端ID和密码。

Getting Started Guide包含所有信息。

首先,您将从Registering your Application开始。

您也可以watch this video了解如何操作。

答案 1 :(得分:0)

目前,PHP client library支持Resumable上传,但大多数大型上传都来自直接上传。

应该只是在constructor of Google_MediaFileUpload中设置参数。

有一个使用类here的例子,相关的行是:

$media = new Google_MediaFileUpload('video/mp4', null, true, $chunkSizeBytes);

对于身份验证,您可以使用this one中的示例。 尝试创建已安装的应用程序和其他,在本地测试时使用该客户端ID和密码。

答案 2 :(得分:-1)

您忘记设置API密钥,并且可以使用可恢复的上传。