具有数据的YouTube直接上传进度条?

3
我想使用gdata api为直接上传到YouTube的视频显示进度条。我通过一个ajax调用uploader.php脚本来启动上传,并在完成后更新用户所看到的页面上的状态。我真的很想设置一个计时器,每5秒更新一次进度条,这样用户就不会不耐烦并取消正在进行中的上传。
我看到很多人尝试找到一种方法,在使用直接上传时从上传中获取进度信息,其中包括使用YouTube API。我没有看到任何答案。这似乎是API应该支持的东西。难道没有办法做到这一点吗?
是否有完全不同的方法可以解决这个问题?
1个回答

0
你可以创建一个 PHP 页面,并使用 CURL 进行“基于浏览器的上传”。这意味着你可以控制上传过程并创建进度条。
以下是上传的示例。代码的底部是有趣的部分。它使用 CURL 进行“基于浏览器的上传”,然后返回结果;

// upload.php


require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();

// Define the authentication that will be used
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 

// Authenticate
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = 
  Zend_Gdata_ClientLogin::getHttpClient(
              $username = "USERNAME",
              $password = "PASSWORD",
              $service = 'youtube',
              $client = null,
              $source = 'HTML SOURCE CODE SNIPPET',
              $loginToken = null,
              $loginCaptcha = null,
              $authenticationURL);

$applicationId = 'YOUR APPLICATION ID';
$clientId = 'Upload videos to youtube using the youtube API';
$developerKey = 'YOUR DEVELOPER KEY';
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);



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

$myVideoEntry->setVideoTitle($videoTitle);
$myVideoEntry->setVideoDescription($VideoDescription);
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory($VideoCategory);

// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags($VideoTags);

$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl."?nexturl=http://YOUR_WEBPAGE.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file">
$post = array("file"=>"@".$VideoFile['tmp_name'], "token"=>$tokenValue);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);

echo $info;

您可以在这里阅读更多内容https://developers.google.com/youtube/2.0/developers_guide_php#Browser_based_Upload


1
那很糟糕,因为你必须将视频上传两次(首先在你的服务器上,第二次在YouTube上)。浏览器上传方法存在的原因就是为了避免这种情况。 - themihai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接