【问题标题】:Progress bar for YouTube direct uploads with data?YouTube 直接上传数据的进度条?
【发布时间】:2012-01-13 18:52:29
【问题描述】:

我想显示一个使用 gdata api 直接上传 youtube 的进度条。我通过对 uploader.php 脚本的 ajax 调用开始上传,当它完成时,我会更新用户看到的页面上的状态。我真的很想设置一个计时器以每 5 秒左右更新一次进度条,这样用户就不会不耐烦并取消正在进行的完美上传。

我看过很多帖子,人们试图找到一种方法,在上传过程中通过 YouTube API 使用直接上传来获取进度信息。我没有看到任何答案。这似乎应该在 API 中得到支持。有没有办法做到这一点?

有没有完全不同的方法,行得通?

【问题讨论】:

    标签: javascript progress-bar youtube-api gdata


    【解决方案1】:

    您可以创建一个 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

    【讨论】:

    • 这很糟糕,因为您必须两次上传视频(第一次在您的服务器上,第二次在 youtube 上)。浏览器上传方法的存在只是为了避免这种情况。
    猜你喜欢
    • 2012-04-01
    • 2011-05-24
    • 2017-10-22
    • 2014-09-21
    • 2013-05-01
    • 2016-08-16
    • 2012-07-06
    • 2018-10-28
    • 2012-03-08
    相关资源
    最近更新 更多