代码之家  ›  专栏  ›  技术社区  ›  ozfarman

使用哪个API/调用来访问基于重定向程序.googlevideo.com的视频文件?

  •  3
  • ozfarman  · 技术社区  · 11 年前

    我的问题是,我应该使用YouTube API加载我从玻璃上录制的视频,并将其作为通知的附件发送到服务器吗?或者,我应该使用镜像api调用吗?

    例如,我收到的视频的URL如下所示: http://redirector.googlevideo.com/videoplayback?id=875f7e1335214880&itag=22&source=picasa&cmo=sensitive_content%3Dyes&ip=0.0.0.0&ipbits=0&expire=1384018362&sparams=id,itag,source,ip,ipbits,expire&signature=AB8FC431423D6C86024A36F170ECF20C6F02223C.3FA395F9092F2EE5D6B14ACF49A4C18725A8846B&key=lh1

    我确实检查了这个类似的答案: Displaying Video on Webpage from Google Glass ,但不确定是否真的应该使用YouTube API。如果我应该这样做,有人能为我指明正确的方向吗?我应该调查哪些电话?

    谢谢

    快速更新以添加更多上下文,因此,如果我录制10秒的视频并通过自定义联系人卡共享到我的服务器,我会立即获得以下内容(这是我自己通过错误日志进行的调试输出):

    [error] Error Downloading Attachment - HTTP ResponseCode:404
    [error] ContentUrl:         
    [error] ContentType:        video/mp4
    [error] id                  ps:5933350025312253298
    [error] IsProcessing:       1
    

    所以,这完全有道理,因为视频仍在处理中。 现在,如果我等几分钟,然后与服务器重新共享视频,我会得到以下内容:

    [error] Error Downloading Attachment - HTTP ResponseCode:302
    [error] ContentUrl:         http://redirector.googlevideo.com/videoplayback?id=e0d5f76ca1fff29a&itag=22&source=picasa&cmo=sensitive_content%3Dyes&ip=0.0.0.0&ipbits=0&expire=1384058057&sparams=id,itag,source,ip,ipbits,expire&signature=CD41362EC3D3DDCD9EA3A63003B4C1A1F95D52C.BAE1608B827661FD47FC8D68DCBCE32F683A013D&key=lh1
    [error] ContentType:        video/mp4
    [error] id                  ps:5933350025312253298
    [error] IsProcessing:       0
    

    相比之下,图像附件总是有效的,并且看起来是这样的:

    [error] Downloading Attachment - HTTP ResponseCode:200
    [error] ContentUrl:         https://www.googleapis.com/mirror/v1/timeline/7f13b01d-ee26-4b02-b6c3-7f885a452fc9/attachments/ps:5933353306000857698?alt=media
    [error] ContentType:        image/jpeg
    [error] id                  ps:5933353306000857698
    [error] IsProcessing:       0
    

    这就是问题所在。我无法找到访问视频数据的方法,部分原因是它是一个重定向,(似乎??)需要某种身份验证交换来处理,而且我无法确定使用哪个API来帮助我做到这一点。

    那么,回到我的问题。。。有没有一种建议的方法可以让我处理重定向器url,将视频下载到我的服务器上,或者甚至只将其托管在原地,但像G+备份云播放器一样将youtube风格的播放器包裹起来?我的意图是让视频公开。

    谢谢 更新:这是我正在使用的代码的要点。我刚开始使用Glass团队提供的示例PHP快速启动:

    https://gist.github.com/ozfarman/6953473

    2 回复  |  直到 7 年前
        1
  •  2
  •   Tim Flack    11 年前

    使用谷歌镜像Api客户端检查与时间线项目关联的附件的状态。根据视频的长度,视频可能需要几分钟才能播放。奇怪的是,如果你按照302的重定向,你最终会进入Picasa域,考虑到这是视频,这有点奇怪,但这确实表明谷歌在移交视频之前正在对你的视频进行一些预处理。

    这段代码是我编写的排队过程的一部分,用于处理向谷歌查询时间线项目的处理状态。来自谷歌的回调被插入到我的队列服务器中,并由工作人员拾取,该工作人员每X秒主动检查一次状态,以确定媒体是否准备就绪,如果超过循环数,则只需重新安排作业由另一个工作人员拾取。主动检查而不是在回调中返回非200代码是非常有益的,因为谷歌会对他们的api调用进行指数级回退,所以对于需要一些时间才能进入正确状态的视频,你可能会在谷歌的回调之间等待很长时间。

    公共函数运行($data){ $timeline_item_id=$data['timeline_iitem_id'];

        try{
            $glassUtils = new Google_Glass_Utilities();
            $mirrorClient = new Google_Glass_MirrorClient();
            $client = $mirrorClient->get_google_api_client();
            $client->setAccessToken($data['credentials']);
            $mirror_service = new Google_MirrorService($client);
            echo "\nTIMELINE ITEM ID: " . $data['timeline_item_id'] . "\n";
            $timeline_item = $mirror_service->timeline->get($timeline_item_id);
            $caption = $timeline_item->getText();
        }catch (Exception $e){
            echo "ERROR";
            echo $e->getMessage();
            return true;
        }
    
        //Blocker that waits for the item to be in the active state
        //Should be faster than waiting for extra callbacks from Google
        try{
            $checkCount = 1;
            while(true){
                $blnProceed = true;
    
                echo "Check " . $checkCount . " of " . $this->maxSearchChecks . " for attachments on timeline item " . $timeline_item_id . "\n";
    
                $attachments = $mirror_service->timeline_attachments->listTimelineAttachments($timeline_item_id);
                foreach ($attachments->getItems() as $attachment) {
                    echo "Attachment content type: " . $attachment->getContentType() . "\n";
                    //echo "Attachment content URL: " . $attachment->getContentUrl();
                    if($attachment->getIsProcessingContent() && $attachment->getContentType() == 'video/mp4'){
                        $blnProceed = false;
                    }
                }
    
                if(!$blnProceed){
                    echo "Attachments were not ready\n";
                    if($checkCount == $this->maxSearchChecks){
                        echo "Job hit max checks, requeueing\n";
                        $task = new BackgroundTask();
                        $task->addTask('GlassVideoImport', $saveData);
                        $task->submit();
                        GearmanPearManager::$LOG[] = "Success";
                        return true;
                    }else{
                        $checkCount += 1;
                        echo "Sleeping for " . $this->sleepSecs . " seconds before recheck\n";
                        sleep($this->sleepSecs);
                    }
                }else{
                    echo "GOT THE TIMELINE ITEM AND ATTACHMENT IS READY\n\n\n\n";
                    //Continue
                    break;
                }
            }
        }catch (Exception $e){
            echo "ERROR";
            echo $e->getMessage();
            return true;
        }
    
        //Got here so now go get the attachment
        try{
            $attachment = $mirror_service->timeline_attachments->get($data['timeline_item_id'], $data['timeline_attachment_id']);
            $attachmentBinary = $this->downloadAttachment($data['timeline_item_id'], $attachment);
    
            //Do something with the binary of the attachment
    
    
            GearmanPearManager::$LOG[] = "Success";
            return true;
        }
        catch( Exception $e ) {
            echo $e->getMessage() . "\n";
            @unlink($fileRes['localFile']);
            GearmanPearManager::$LOG[] = "Failure";
            return true;
        }
    }
    
    private function downloadAttachment($itemId, $attachment) {
        $request = new Google_HttpRequest(
        $attachment->getContentUrl(), 'GET', null, null);
        $httpRequest = Google_Client::$io->authenticatedRequest($request);
        if ($httpRequest->getResponseHttpCode() == 200) {
            return $httpRequest->getResponseBody();
        } else {
            return null;
        }
    }
    
        2
  •  2
  •   Tony Allevato    11 年前

    您应该使用GoogleAPI客户端下载附件URL,它将为您处理授权和任何重定向。请参阅中提供的示例 Timeline.attachments.get 文档,无论您使用哪种语言进行编码。