duanke3985 2013-06-01 12:26
浏览 46
已采纳

如何在YouTube上上传LARGE文件[关闭]

I have tried two methods to upload large files on YouTube but none of them works, each has its own issues, my aim is to find a correct answer to upload large files.

First Method:

It chunks the large files into different parts, and rather than sending them separately and upload them at once it uploads them separately. So at the end I have a long list of unloadable different parts of the file on Youtube rather than a single file.

Code of method 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);
}

I found this question which its answer code is similar to google_mediaFileupload but I am not sure how to use it.

Second method

I am also trying to use resumable upload to upload them but when I use the following code it runs into following error:

Code of method 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>";
}



?>

Error of method 2

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

Please note, I have given 50 bounty to one of the answers for solving one of the issues of the code. Although the problem is not completely solved yet.

  • 写回答

3条回答 默认 最新

  • douan7601 2013-06-09 03:21
    关注

    You have forgotten to set the API key and you may use resumable upload.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)