从curl或get_video_info获取youtube视频网址

iwe*_*wek 10 php youtube video

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

但是,当我将我的代码上传到我的网络服务器并尝试运行它时,没有一个视频网址有效.网址似乎很好,但有些参数,如IP,不同.我无法理解为什么它可以在运行xampp或mamp的本地机器上运行,但不能在我的Web服务器上运行.我甚至尝试在youtube视频页面上做一个卷曲,并注意到在本地,它会输出页面并播放视频,但在我的网络服务器上,所有的视频通话都得到404.

有关这个的任何信息?无论如何,我可以获取YouTube视频网址,以便我可以在html5视频标签中播放YouTube视频?这就是为什么keepvid和这样的网站使用该死的java小程序?

Gil*_*not 10

您应该看看youtube-dl项目我非常确定您可以理解正确的方法来实现您的目标.


Lic*_*son 6

我用PHP创建了一个脚本,最近将youtube视频流式传输到了clinets,我认为通过改变我的脚本可以满足你的目的.

这是我的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; 
    } 
}
?>
Run Code Online (Sandbox Code Playgroud)

我希望它可以帮到你.

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