Skip to content

Instantly share code, notes, and snippets.

@anatox
Created November 27, 2015 11:50
Show Gist options
  • Select an option

  • Save anatox/712f197169c130bc2ad7 to your computer and use it in GitHub Desktop.

Select an option

Save anatox/712f197169c130bc2ad7 to your computer and use it in GitHub Desktop.
<?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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment