Skip to content

Instantly share code, notes, and snippets.

@vishnusomanus
Created December 29, 2016 10:09
Show Gist options
  • Save vishnusomanus/8c993f34c9d88008a3fcd228c8ad3ffa to your computer and use it in GitHub Desktop.
Save vishnusomanus/8c993f34c9d88008a3fcd228c8ad3ffa to your computer and use it in GitHub Desktop.
function getVideoUrlAction( $id = null,$quality='high' ){
$return = array();
$quality_index = array('high'=>0,'medium'=>0,'small'=>0);
$videoID = empty( $id ) ? ( $this->getRequest()->getParam('videoid') ) : $id ;
$xmlReqURL = 'http://www.youtube.com/get_video_info?&video_id='.$videoID.'&asv=3&el=detailpage&hl=en_US';
$response = file_get_contents( $xmlReqURL );
parse_str($response,$ttt);
if($ttt['status']=='fail')return 'Invalid video ID';
$each_quality_urls = explode(',',$ttt['url_encoded_fmt_stream_map']);
$each_quality_array = array();
foreach ($each_quality_urls as $key => $value) {
parse_str($value,$item);
if( strpos( $item['type'], 'video/mp4' ) !== false && $item['quality']=='hd720')
{
$quality_index['high']=$key;
}
else if( strpos( $item['type'], 'video/mp4' ) !== false && $item['quality']=='medium')
{
$quality_index['medium']=$key;
}
else if($item['quality']=='small')
{
$quality_index['small']=$key;
}
$item['url'] = urldecode($item['url']).'&signature=';
$item['poster'] = "https://img.youtube.com/vi/{$id}/hqdefault.jpg";
array_push($each_quality_array,$item);
}
if($quality=='high')
return $each_quality_array[$quality_index['high']];
else if($quality=='medium')
return $each_quality_array[$quality_index['medium']];
else if($quality=='small')
return $each_quality_array[$quality_index['small']];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment