php - 用PHP抓取YouTube上的视频

标签 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 URL粘贴到字段中并在其中运行上述脚本的地方。但是我得到回声
Damn! Click Back and try again..Sorry :("
我不知道为什么会收到此错误,该代码对我来说有什么建议?

最佳答案

不久前,我偶然发现了这一点:

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

关于php - 用PHP抓取YouTube上的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21026981/

相关文章:

javascript - HTML Canvas 仅在浏览器调整大小后触发?

Youtube V3 - LiveChatMessages.list 最多仅返回 75 个请求

ios - youtube-mp3.org api 无法正常工作,仅下载缓存的视频

php - 使用 php 和 html 传递数据

php - 获取统计数据并在图表中显示 - PHP/HighCharts

php - Roundcube 登录 "Tabbing"

PHP/MySQL : SQL Statement from different columns

html - 左边是 Bootstrap 轮播,右边是动态文本

html - Sendgrid 发送不带嵌入代码的 html 电子邮件

javascript - Youtube 嵌入开始时间错误 &lt;iframe&gt;<javascript>