【问题标题】:Grabbing videos from YouTube in PHP用 PHP 从 YouTube 抓取视频
【发布时间】:2014-01-28 09:40:11
【问题描述】:

我正在尝试设置一个脚本来抓取 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 html youtube


【解决方案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();
    }
}
?>

【讨论】:

  • 我喜欢它,但是 URL 是从 YouTube 下载视频的输入,所以我无法识别 ID。
  • 此代码返回错误:注意:未定义索引:输入第 12 行
  • @AnassElFakir 此代码已有 5 年历史,因此 YouTube 很可能改变了某些内容。当您弄清楚如何使用它时,请随时发布我的代码的更新版本。
猜你喜欢
  • 1970-01-01
  • 2019-12-30
  • 2011-10-12
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 2020-11-05
  • 2021-02-10
  • 2016-11-14
相关资源
最近更新 更多