用PHP抓取YouTube上的视频

时间:2014-01-09 17:21:47

标签: php html youtube

我正在尝试设置一个脚本来抓取youtube视频,以便在php中下载它们是我当前的脚本。

<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 :("

我不知道为什么会收到此错误,代码看起来对我有任何建议吗?

1 个答案:

答案 0 :(得分:0)

我偶然发现了这件事:

<?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();
    }
}
?>