2017-01-15 151 views
1

我有Youtube視頻下載腳本它給視頻網址,但不下載,它在瀏覽器中打開視頻和播放。php腳本強制下載YouTube視頻

$id = 'jNQXAC9IVRw';//$_GET['v']; 
    parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=".$id),$info); 
    $streams = $info['url_encoded_fmt_stream_map']; 
    $streams = explode(',',$streams); 

    foreach($streams as $stream){ 
     parse_str($stream,$data); 
     echo '<a href="'.$data['url'].'" class="btn btn-success">'.$data['quality'].'</a> '; 
     /*echo $data['url']; 
     *returns url like this 
     *http://r2---sn-2uja-3ipd.googlevideo.com/videoplayback?mime=video%2Fwebm&upn=wdfTtFFSMNA&source=youtube&mt=1484446843&ms=au&expire=1484468585&initcwndbps=187500&ipbits=0&pl=24&itag=43&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Csource%2Cupn%2Cexpire&id=o-AGqBaImYFicQvH1J3Tmt4QGqt817uD_Pw-vF9MF8asMy&dur=0.000&mm=31&key=yt6&mn=sn-2uja-3ipd&signature=C38C1D21BC1B7F4658BCEF30DFC94E1ACBE1FC6D.356305D4012AE391A24283CA46950BBCDE7EB7CE&lmt=1418224509434980&mv=m&ratebypass=yes&ip=119.154.139.153 
     */ 
    } 

我試過很多方法,但沒有一個人擔任我想要的,我想要顯示的按鈕,其中用戶可以下載他所選擇的視頻格式,如MP4,3GP他的電腦上。
我寫了一個正在服務器上下載0KB文件的腳本。
這也可以,如果由jQuery完成。

回答

0

創建另一個文件download.php發送URL,file_format該文件

讓你的鏈接= download.php?video_url=https://that_lengthy_url&format=mp4-OR-3gp

的download.php

$format = $_GET['format']; 
$filename = 'video.'.$format; 
$url = $_GET['video_url']; 

header('Content-Type: application/octet-stream'); 
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$filename."\""); 

readfile($url); 
exit;