【问题标题】:grabbing youtube video URL from curl or get_video_info从 curl 或 get_video_info 获取 youtube 视频 URL
【发布时间】:2012-01-09 04:08:43
【问题描述】:

所以,我正在开发一个 php 项目,其中一部分是抓取一个 youtube 视频 url 并将其插入到 html5 视频标签中。我正在使用 curl 调用 http://youtube.com/get_video_info?video_id=XXX 并在我的本地机器上获取正确的视频文件 url。

但是,当我将代码上传到我的网络服务器并尝试运行它时,所有视频 URL 都不起作用。网址看起来不错,但某些参数(例如 IP)有所不同。我不明白为什么它可以在我运行 xampp 或 mamp 的本地机器上运行,但在我的 Web 服务器上却不行。我什至尝试在 youtube 视频页面上卷曲,并注意到在本地,它会输出页面并播放视频,但在我的网络服务器上,所有视频通话都得到 404。

有这方面的信息吗?无论如何,我可以获取一个 youtube 视频网址,以便我可以在 html5 视频标签中播放 youtube 视频?这就是keepvid和类似网站使用该死的Java小程序的原因吗?

【问题讨论】:

  • 既然你得到了 404,那么使用了什么 URL?你能发布一些获取信息和匹配数据的示例代码吗?
  • YouTube 不是不允许你做这样的事情吗?
  • 其实是一个403,可以在这个网址看到:o-o.preferred.iad09s12.v5.lscache8.c.youtube.com/…
  • 如果 YouTube 不允许,那么为什么它可以在我的本地机器上运行,为什么他们有这个请求:youtube.com/get_video_info?video_id=XXX
  • 注意这是非法的,明确违反 YT ToS

标签: php video youtube


【解决方案1】:

您可以简单地从视频 ID 构建一个嵌入 URL

这里最大的好处是你可以根据需要给播放器添加自己的参数。

例如一个 iframe src,

http://www.youtube.com/embed/{$video_id}?theme=light&autoplay=1&...

【讨论】:

    【解决方案2】:

    您可以使用此代码下载 youtube 视频:

    <?php
    $id='5HDw7sQE2H0';
    $dt=file_get_contents("http://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en");
    $x=explode("&",$dt);
    $t=array(); $g=array(); $h=array();
    echo "<pre>\r\n";
    foreach($x as $r){
        $c=explode("=",$r);
        $n=$c[0]; $v=$c[1];
        $y=urldecode($v);
        $t[$n]=$v;
    }
    $streams = explode(',',urldecode($t['url_encoded_fmt_stream_map']));
    foreach($streams as $dt){ 
        $x=explode("&",$dt);
        foreach($x as $r){
            $c=explode("=",$r);
            $n=$c[0]; $v=$c[1];
            $h[$n]=urldecode($v);
        }
        $g[]=$h;
    }
    print_r($g);
    echo "\r\n</pre>";
    ?>
    

    【讨论】:

    • 您可能需要添加签名才能使其工作
    • 在第 10 行,$y 从未使用过。我认为您为第 11 行写的内容是$t[$n] = urldecode($v)。或者更确切地说,$table[$name] = urldecode($value),因为谁拿枪指着你的脑袋,强迫你使用糟糕的单字母变量名?
    • 我也使用过这种 URL,但我无法让它返回高于 720p 的质量。任何想法获得 1080p?
    【解决方案3】:

    您提取的链接仅适用于您提取它的 IP 地址。因此,如果您从本地计算机中提取它,它将在您计算机上的浏览器中运行。但是,如果您从服务器中提取它,则只有服务器能够从该链接访问视频文件。

    【讨论】:

      【解决方案4】:

      我最近用 PHP 编写了一个脚本来将 youtube 视频流式传输到客户,我认为通过修改我的脚本可以满足您的目的。

      这是我的 PHP 脚本:

      <?php 
      @set_time_limit(0); //disable time limit to make sure the whole video is streamed to the client
      $id = $_GET['id']; //the youtube video ID
      $type = $_GET['type']; //the MIME type of the desired format
      
      parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id='.$id),$info); //get video info
      $streams = explode(',',$info['url_encoded_fmt_stream_map']); //split the stream map into streams
      
      foreach($streams as $stream){ 
          parse_str($stream,$real_stream); //parse the splitted stream
          $stype = $real_stream['type']; //the MIME type of the stream
          if(strpos($real_stream['type'],';') !== false){ //if a semicolon exists, that means the MIME type has a codec in it
              $tmp = explode(';',$real_stream['type']); //get rid of the codec
              $stype = $tmp[0]; 
              unset($tmp); 
          } 
          if($stype == $type && ($real_stream['quality'] == 'large' || $real_stream['quality'] == 'medium' || $real_stream['quality'] == 'small')){ //check whether the format is the desired format 
              header('Content-type: '.$stype); //send the HTTP headers
              header('Transfer-encoding: chunked'); //necessary for streaming
              @readfile($real_stream['url'].'&signature='.$real_stream['sig']); //send the content to the client
              ob_flush(); //disable PHP caching
              flush(); //flush the content out
              break; 
          } 
      }
      ?>
      

      希望对你有帮助。

      附:您需要从服务器发送内容,因为视频 URL 因 ISP 而异。

      【讨论】:

      • 它可以工作,但有人在 pascal/delphi 中有类似的代码吗?
      【解决方案5】:

      你应该看看youtube-dl项目我很确定你可以采取一些想法来理解实现目标的正确方法。

      【讨论】:

      • 好的,我刚刚检查并测试了它,效果很好,但我想知道他们是如何做到的。我不懂python,他们用get_video_info吗?
      • 如果你不懂python,我猜你知道grepack ;)
      • 好的,使用 grep 我看到他们确实使用了 get_video_info,它与我的 php 代码非常相似。换句话说,他们提供的 url 可以在本地工作,但不能在服务器上工作。由于文件确实在服务器上下载,我认为这是浏览器 IP 的问题。在本地,我的浏览器与我的本地计算机相同,但当我使用浏览器从服务器访问页面时,情况并非如此。因此,该 URL 对于我的服务器是正确的,这就是它可以工作和下载的原因,但作为最终用户,该 URL 不正确。还有其他想法吗?
      猜你喜欢
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2012-05-22
      • 2011-03-28
      • 2011-05-02
      • 1970-01-01
      相关资源
      最近更新 更多