php 从 curl 或 get_video_info 获取 youtube 视频 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8317199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:25:02  来源:igfitidea点击:

grabbing youtube video URL from curl or get_video_info

phpvideoyoutube

提问by iwek

So, I am working on a php project and one part of it is grabbing a youtube video url and inserting it into an html5 video tag. I was using a curl call to http://youtube.com/get_video_info?video_id=XXXand getting the right video file urls on my local machine.

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

But, when I uploaded my code to my web server and tried to run it, none of the video URLs have worked. The urls seemed fine but some parameters, like IP, where different. I can't understand why it works from my local machine running xampp or mamp but not on my web server. I even tried just doing a curl on the youtube video page and noticed that locally, it would output the page and play the video but on my webserver, all the video calls got 404s.

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

Any info about this? Anyway I can grab a youtube video url so that I can play youtube videos in a html5 video tag? Is this why keepvid and sites like that use the damn java applet?

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

回答by Gilles Quenot

You should take a look to youtube-dlproject I'm pretty sure you can takes some idea to understand the proper way to achieve your goal.

您应该查看youtube-dl项目,我很确定您可以通过一些想法来理解实现目标的正确方法。

回答by Licson

I've made a script in PHP to stream youtube videos to clinets recently and I think by alter a little bit of my script can fit your purpose.

我最近用 PHP 制作了一个脚本来将 youtube 视频流式传输到 clinets,我认为通过改变我的脚本的一点点可以满足您的目的。

Here is my PHP script:

这是我的 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; 
    } 
}
?>

I hope it can help you.

我希望它能帮助你。

P.S. You need to send the content from the server because the video URL varies by ISPs.

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

回答by georgedyer

The link that you extract only works for the IP address from which you extracted it. So, if you extract it from your local machine, it will work in the browser on your machine. However, if you extract it from your server, only the server will be able to access the video file from that link.

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

回答by Alim Sumarno

You can use this code for download youtube video :

您可以使用此代码下载 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>";
?>

回答by Chaoley

You can simply build an embed URL from the video ID

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

The big advantage here is that you can add your own parameters to the player as required.

这里的一大优势是您可以根据需要向播放器添加自己的参数。

For example an iframe src,

例如一个 iframe src,

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

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