【问题标题】:How can I download youtube videos php?如何下载 youtube 视频 php?
【发布时间】:2011-07-18 12:09:38
【问题描述】:

无论如何,我正在寻找从 youtube 下载视频... 当然我用过所有的代码和脚本,都试过了,都失败了……

因此我想要对我有帮助的新代码...

【问题讨论】:

  • 贴出你试过的代码,我们从那里开始
  • 大多数人只是使用浏览器观看视频,然后将 .flv 文件下载到他们的计算机上……这属于您指定的“任何方式”。

标签: php video download youtube


【解决方案1】:

为此使用外部工具。 youtube-dl 浮现在脑海中,并且很容易在 PHP 中使用:

$url = escapeshellcmd($url);
exec("youtube-dl $url -O /tmp/video.flv");

【讨论】:

  • 如果你愿意,然后用 PHP 重写它。
  • 我尝试在服务器中安装 youtube-dl ...然后我测试了这个 root@ns1 [~]# youtube-dl www.youtube.com/watch?v=62h6Wr7heXk [youtube] Setting language [youtube] 62h6Wr7heXk: Downloading video info webpage [youtube] 62h6Wr7heXk: Extracting video information ERROR: format not available for video
  • 您必须将其更新到最新版本。在命令行上使用sudo youtube-dl -Uubuntugeek.com/…
  • @mario 我有最新版本...我重新安装它但它仍然无法正常工作...我的操作是:CentOs 5
  • 但是当我这样做时的问题:root@ns1 [~]# sudo youtube-dl -U usage: Usage: youtube-dl [options] url... youtube-dl: error: no such option: -U
【解决方案2】:
<?php
 // The YouTube ID
 $key = "Ct5KJKBI9kc";
 // Get all info for video
 $output = file_get_contents('http://www.youtube.com/get_video_info?&video_id='.$key);
 // Parse data to eg.(&id=var)
 parse_str($output);
 // Get Quality map of video an set array
if(isset($url_encoded_fmt_stream_map)) {
 $my_formats_array = explode(',',$url_encoded_fmt_stream_map);
} 
 // Set Array & Vars
 $avail_formats[] = '';
 $i = 0;
 // Break up array to create download links to quality
 foreach($my_formats_array as $format) {
 parse_str($format);
echo "<a download='". $title .".mp4' href='".$avail_formats[$i]['url'] = urldecode($url) . '&signature=' . $sig."'>". $title .".mp4</a> Quality - ".$quality."<br>"; $i++; } ?>

【讨论】:

  • 只需将 $key 值更改为您的 youtube 视频 ID 并运行。
  • 关于parse_str 在这段代码中的使用,php 手册说:警告: 不建议使用没有第二个参数的函数。在函数范围内动态设置变量与 register_globals 存在完全相同的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 2020-06-21
  • 2011-12-26
  • 2010-11-08
相关资源
最近更新 更多