2014-01-09 48 views
0

我正在嘗試設置一個腳本,用於抓取YouTube視頻以在PHP中下載它們,這裏是我當前的腳本。從PHP中抓取YouTube上的視頻

<html> 
<?php 

if(!empty($_POST['name'])) { 

$location = htmlspecialchars($_POST['name']); 
try { 
    $handle = fopen($location, "r"); 
if($handle) { 
    $contents = ''; 
while (!feof($handle)) { 
    $contents .= fread($handle, 8192); 
} 

fclose($handle); 
$result1 = preg_match("/&t=([\w]*)&/",$contents,$tickets); 
$result2 = preg_match("/v=(\w*)/",$location,$video_id); 

if($result1) { 
    echo "<a href = \"http://www.youtube.com/get_video?video_id="; 
    echo $video_id[1]; 
    echo "&t="; 
    echo $tickets[1]; 
    echo "\">Download link.</a>"; 
    echo "<br>"; 
    echo "Click and Save"; 
} 
    else echo "Damn! Click Back and try again..Sorry :("; 
} 
    else echo "\nYou liked that? Ha?"; 
} 
    catch(Exception $e) {echo "I made an error. So what? COME AT ME BRO!";} 
} 

else echo "Empty input! YOU'RE SO STUPID!"; 
?> 
<br><br> 
<a href="face.html">Back</a> 
</hmtl> 

我也有一個簡單的HTML文件,提供的地方YouTube網址粘貼到一個領域,它運行上面的腳本。但是我收到迴音

Damn! Click Back and try again..Sorry :("

我不知道爲什麼我收到此錯誤,代碼看起來固體給我什麼建議?

+0

'的var_dump($內容)'? – jeroen

+0

看看:https://github.com/jeckman/YouTube-Downloader –

回答

1

我偶然發現了這一段時間回來:

<?php 
$id = 'O99Sqpxd0hE'; 
$format = 'video/mp4'; 
parse_str(file_get_contents("http://www.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); 
    if(stripos($data['type'], $format) !== false) { 
     $video = fopen($data['url'] . '&signature=' . $data['sig'], 'r'); 
     $file = fopen($_GET['id'] . '.mp4', 'w'); 
     stream_copy_to_stream($video, $file); 
     fclose($video); 
     fclose($file); 
     echo '<a href="./'.$_GET['id'].'.mp4">Download</a>'; 
     die(); 
    } 
} 
?> 
+0

我喜歡它,但URL是從YouTube下載視頻的輸入,所以我無法識別已識別的ID。 – mongobongo