从curl或get_video_info抓取youtube视频URL


grabbing youtube video URL from curl or get_video_info

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

但是,当我将代码上传到我的web服务器并试图运行它时,没有一个视频url有效。url看起来很好,但是一些参数,比如IP,就不一样了。我不明白为什么它在我运行xampp或mamp的本地机器上工作,而不是在我的web服务器上。我甚至试着在youtube视频页面上做一个卷曲,并注意到在本地,它会输出页面并播放视频,但在我的web服务器上,所有的视频呼叫都得到了404。

关于这个有什么信息吗?不管怎样,我可以抓取一个youtube视频url,这样我就可以在html5视频标签中播放youtube视频?这就是为什么keepvid和类似的网站使用该死的java applet?

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

我最近在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; 
    } 
}
?>

我希望它能帮助你。

注:由于不同isp提供的视频URL不同,需要从服务器发送。

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

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

您可以简单地从视频ID

构建一个嵌入URL

这样做的最大好处是你可以根据需要为玩家添加自己的参数。

例如iframe src

http://www.youtube.com/embed/{$ video_id} ?主题= light&播放= 1,…