如何在YouTube上上传大文件 [英] How to upload LARGE files on YouTube

查看:496
本文介绍了如何在YouTube上上传大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了两种方法在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)); ('mediaUpload'=> $ media));

$ result = $ youtube-> videos-> insert(status,snippet,$ video,
array

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

fclose($ handle);
}

我发现这个问题,其答案代码与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('我的客户ID');
$ client-> setClientSecret('我的密码');
$ client-> setRedirectUri('http:// localhost:8888 / mymediaapp2 / uploadvideo.php');
$ client-> setDeveloperKey('My developer key');
$ client-> setScopes(https://www.googleapis.com/auth/youtube.upload);
$ youTubeService =新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()){
echohere;
$ json = $ client-> getAccessToken();
$ jsonIterator = new RecursiveIteratorIterator(
RecursiveArrayIterator(json_decode($ json,TRUE)),
RecursiveIteratorIterator :: SELF_FIRST);
$ myvar =;
foreach($ jsonIterator as $ key => $ val){
if($ key ==access_token)
echoval 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'>上传< / a>;
}



?>

方法2的错误

  {error:{errors:[{domain:global,reason:required,message:Login 
必需,locationType:header,location:Authorization}],code:401,
message:
需要登录}}

请注意,我给了50个赏金来解答其中一个答案代码的问题。虽然问题还没有完全解决。 解决方案

您忘记设置API密钥,您可能会使用可恢复的上传。 / p>

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.

解决方案

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

这篇关于如何在YouTube上上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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