Xin chào các bạn hôm nay mình sẽ hướng dẫn các bạn play video youtube .Code stream :
<?php header('Content-Type: application/json'); if (isset($_GET['url']) && $_GET['url']) { $url = $_GET['url']; $data = get($url); //echo "<pre>"; print_r($data); echo json_encode($data); } function get($link,$default = ''){ $result = array(); $id = getIdYoutube($link); if($id) { $apiYoutube = 'http://www.youtube.com/get_video_info?&video_id='.$id.'&asv=3&el=detailpage&hl=en_US'; $page = curl($apiYoutube); parse_str($page, $videoInfo); if(isset($videoInfo['status']) && $videoInfo['status'] == 'ok') { $result['title'] = $videoInfo['title']; $result['image'] = 'http://i1.ytimg.com/vi/'.$id.'/0.jpg'; $result['sources'] = array(); $stream = explode(',', $videoInfo['url_encoded_fmt_stream_map']); foreach ($stream as $format) { parse_str($format, $format_info); parse_str(urldecode($format_info['url']), $url_info); $type = explode(';', $format_info['type']); $itag = $format_info['itag']; $infoItag = itagMap($itag); $var['type'] = $type[0]; $var['label'] = $infoItag['quality']; if($infoItag['quality'] == $default){ $var['default'] = 'true'; }else{ $var['default'] = 'false'; } $file = urldecode($format_info['url']); $var['file'] = preg_replace("/.*googlevideo.com/", "https://redirector.googlevideo.com", $file); $var['file'] = preg_replace("/.*google.com/", "https://redirector.googlevideo.com", $var['file']); if($var['type'] == 'video/mp4'){ array_push($result['sources'], $var); } if($result['sources']) $result['sources'] = array_reverse($result['sources']); } } } if(!$result) { $result = get($link,$default); } return $result; } function getIdYoutube($link){ $link = trim($link); preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $link, $id); if(!empty($id)) { return $id = $id[0]; }else{ return false; } } function readString($string, $findStart, $findEnd) { $start = stripos($string, $findStart); if ($start === false) return false; $length = strlen($findStart); $end = stripos(substr($string, $start + $length), $findEnd); if ($end !== false) { $rs = substr($string, $start + $length, $end); } else { $rs = substr($string, $start + $length); } return $rs ? $rs : false; } function curl($url) { $ch = @curl_init(); curl_setopt($ch, CURLOPT_URL, $url); $head[] = "Connection: keep-alive"; $head[] = "Keep-Alive: 300"; $head[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $head[] = "Accept-Language: en-us,en;q=0.5"; curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36'); curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); curl_setopt($ch, CURLOPT_HTTPHEADER, $head); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); $page = curl_exec($ch); curl_close($ch); return $page; } function itagMap($itag){ $itag = (int)$itag; switch ($itag) { case 17: $quality = 360; $type = "3gpp"; break; case 36: $quality = 480; $type = "3gpp"; break; case 5: $quality = 240; $type = "flv"; break; case 34: $quality = 360; $type = "flv"; break; case 35: $quality = 480; $type = "flv"; break; case 18: $quality = 360; $type = "mp4"; break; case 59: $quality = 480; $type = "mp4"; break; case 22: $quality = 720; $type = "mp4"; break; case 37: $quality = 1080;//1920 x 1080 $type = "mp4"; break; case 38: $quality = 1080;//2048 x 1080 $type = "mp4"; break; case 43: $quality = 360; $type = "webm"; break; case 44: $quality = 480; $type = "webm"; break; case 45: $quality = 720; $type = "webm"; break; case 46: $quality = 1080; $type = "webm"; break; default: $quality = 0; $type = ""; break; } return array("quality"=>$quality,"type"=>$type); } ?>
Code play :
<?php if (isset($_GET['url']) && $_GET['url']) { $url = $_GET['url']; } $var = file_get_contents('http://api.hbsprogram.com/getYoutube.php?url='.$url); $content = ' <div id="123">Loading the player...</div> <script src="https://content.jwplatform.com/libraries/E4QWMS3Z.js"></script> <script type="text/javascript"> jwplayer("123").setup( '.$var.' ); </script> '; echo $content; ?>
Link demo play :http://api.hbsprogram.com/playyoutube.php?url=https://www.youtube.com/watch?v=XVUZLn-PVN0
Link api get link : http://api.hbsprogram.com/getYoutube.php?url=https://www.youtube.com/watch?v=XVUZLn-PVN0