2011-01-05 71 views

回答

16

最簡單的方法是從http://youtube.com/get_video_info?video_id=XXXXXXXX

我將使用PHP作爲處理這個數據的例子得到源。使用PHP的parse_str()您可以將字符串的所有變量分配到的視頻信息一個很好的數組:

$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$id); 
parse_str($content, $ytarr); 

然後將所有信息存儲到$ ytarr。感興趣的變量將是「fmt_list」和「fmt_map」(它似乎包含相同的事物afaik)。此變量包含可用的視頻格式,根據該圖在這裏的列表:http://en.wikipedia.org/wiki/Youtube#Quality_and_codecs

假設你知道那些你想下載的格式,然後你可以使用「fmt_url_map」變量(或$ ytarr ['fmt_url_map ']在這個例子中)來訪問所需格式的鏈接列表。

您可以使用這些鏈接下載任何格式的視頻,但它們受限於YouTube的有限帶寬(youtube用於節省帶寬的最佳流媒體速度),因此下載速度不會像您想象的那麼快。這使我相信這些鏈接最好用於流式傳輸而不是下載,但我不知道有其他選擇。

4

youtube更改了用於獲取flv文件的請求。 現在看起來

http://v2.lscache3.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0dYSVZMTl9FSkNNOF9ORlJF&fexp=903816&fexp=903813%2C908400&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&expire=1294610400&key=yt1&signature=9C4D194A7F85F0171771486714D3F061ED2924F6.98D10BB190123CADCA3CDF993174D47F8932895F&factor=1.25&id=10af5a6280e42a28

你可以輕鬆抓取除了超頻參數的所有參數即從響應%2Coc%3AU0dYSVZMTl9FSkNNOF9ORlJF ,仍然在尋找一種方式來獲得它。

youtube鎖定瀏覽器的鏈接,所以你不能在服務器上使用它。 如果你看看youtube頁面的源代碼,你會發現像上面的地址,但沒有oc參數,如果你從Firebug網絡面板或鉻資源中複製它,它會下載flv電影作爲「videoplayback」。搶在YouTube視頻信息

+0

感謝您的答覆。只要你得到任何解決方案,請把它發佈在這裏。 – 2011-01-12 12:09:50

6

互聯網充滿了沒有工作的例子,所以這裏是一個腳本,任何需要的人。 FLV文件有問題。他們的鏈接提供的.txt文件,但它與webm和mp4很好。我需要爲pr0n網站太呵呵呵

<?php 

//$debug = true; 

if(empty($debug)) 
    $debug = false; 

if(empty($_GET['id'])) 
    die('no id'); 
if(!preg_match('/^[A-Z0-9-_]+$/i', $_GET['id'])) 
    die('invalid character in id'); 
else 
    $id = $_GET['id']; 

if(empty($_GET['type'])) 
    $type = 'mp4'; 
else 
if(!preg_match('/^[A-Z]+$/i', $_GET['type'])) 
    die('invalid character in type'); 
else 
    $type = strtolower($_GET['type']); 


$url = 'http://youtube.com/get_video_info?video_id='; 
$key = 'url_encoded_fmt_stream_map'; 
$content = file_get_contents($url.$id); 
parse_str($content, $result); 

if($debug) 
    { 
    echo $url.$id.'<br/>'; 
    echo $key.'<br/>'; 
    echo $type.'<br/>'; 
    echo '<pre>'; 
    print_r($result); 
    echo '</pre>'; 
    } 
else 
    { 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="videofile.'.$type.'"'); 
    } 

$type = 'type=video/'.$type; 
$files = explode(',url=', $result[$key]); 
$files[0] = substr($files[0], 4); 

for($i=0; $i<count($files); $i++) 
    { 
    $file = urldecode($files[$i]); 
    $found = strpos($file, $type) > -1; 

    if ($debug) 
     { 
     if ($found) 
      echo '[THIS]'; 
     echo '<a href="'.$file.'">'.$file.'</a><br/><br/>'; 
     } 
    else 
     { 
     if ($found) 
      { 
      $file = explode('; codecs=', $file); 
      @readfile($file[0]); 
      break; 
      } 
     } 
    } 
?> 
+0

如何使用此僅獲取使用視頻ID的mp4文件版本? – m3tsys 2012-03-03 21:34:29

1

嘗試:

$id = "111_fGEdIvs"; //the youtube video ID 
//$id = $_GET['id']; 

//$format = $_GET['fmt']; //the MIME type of the video. e.g. video/mp4, video/webm, etc. 
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=".$id),$info);  //decode the data 
$streams = $info['url_encoded_fmt_stream_map']; //the video's location info 
$streams = explode(',',$streams); 
parse_str(urldecode($streams[0]),$data); //In this example I am downloading only the first video 

$url=$data['url']; 
$sig=$data['signature']; 
unset($data['type']); 
unset($data['url']); 
unset($data['sig']); 
$urlPlay= str_replace('%2C', ',' ,$url.'&'.http_build_query($data).'&signature='.$sig); 

echo '<iframe class="youtube-player" type="text/html" width="640" height="385" src="'.$urlPlay.'" allowfullscreen frameborder="0" ></iframe>';