Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grabbing youtube video URL from curl or get_video_info

Tags:

php

youtube

video

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=XXX and getting the right video file urls on my local machine.

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.

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?

like image 863
iwek Avatar asked Nov 29 '11 20:11

iwek


3 Answers

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

like image 71
Gilles Quenot Avatar answered Oct 27 '22 09:10

Gilles Quenot


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.

Here is my PHP script:

<?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.

like image 39
Licson Avatar answered Oct 27 '22 10:10

Licson


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.

like image 2
georgedyer Avatar answered Oct 27 '22 08:10

georgedyer