使用 PHP 下载 Youtube 视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3687859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading Youtube videos with PHP
提问by Will
I am searching for a way to download Youtube videos using PHP. I have searched how to do this for hours but unfortunately all the Google results I find are years old and do not work anymore.
我正在寻找一种使用 PHP 下载 Youtube 视频的方法。我已经搜索了几个小时如何做到这一点,但不幸的是,我找到的所有谷歌结果都已经有几年了,不再有效。
I would appreciate it if someone could explain how to do this, or give a link to an up-to-date article that explains it in detail.
如果有人可以解释如何做到这一点,或者提供指向详细解释它的最新文章的链接,我将不胜感激。
Thanks very much.
非常感谢。
采纳答案by Brad
The first thing you should do is get a tool like Fiddlerand visit a YouTube video page. In Fiddler, you will see all of the files that make up that page, including the FLV itself. Now, you know that the video isn't one of the CSS files, nor is it the image files. You can ignore those. Look for a big file. If you look at the URL, it begins with /videoplayback
.
您应该做的第一件事是获取类似Fiddler的工具并访问 YouTube 视频页面。在 Fiddler 中,您将看到构成该页面的所有文件,包括 FLV 本身。现在,您知道视频不是 CSS 文件之一,也不是图像文件。你可以忽略这些。寻找一个大文件。如果您查看 URL,它以/videoplayback
.
Now, once you've found it, figure out how the browser knew to get that file. Do a search through the sessions (Ctrl+F) and look for "videoplayback". You will see a hit on the first page you went to, like http://www.youtube.com/watch?v=123asdf. If you dig through that file, you'll see a DIV tag with the ID of "watch-player". Within that there is a script tag to setup the flash player, and within that are all of the flash parameters. Within those is the URL to the video.
现在,一旦您找到它,请弄清楚浏览器是如何知道获取该文件的。搜索会话 (Ctrl+F) 并查找“videoplayback”。您会在访问的第一页上看到一个点击,例如http://www.youtube.com/watch?v=123asdf。如果你翻阅那个文件,你会看到一个带有“watch-player”ID 的 DIV 标签。其中有一个用于设置 Flash 播放器的脚本标签,其中包含所有 Flash 参数。其中是视频的 URL。
So now you know how to use your tools to figure out how the browser got to it. How do you duplicate this behavior in PHP?
所以现在你知道如何使用你的工具来弄清楚浏览器是如何做到的。你如何在 PHP 中复制这种行为?
Do a file_get_contents()
on the page that references the video. Ignore everything not in that watch-player div. Parse through the code until you find that variable that contains the URL. From there you will probably have to unescape that URL. Once you have it, you can do a file_get_contents()
(or some other download method, depending on what you are trying to do) to get the URL. it is that simple. Your HTML parsing code will be the most complex.
做一个file_get_contents()
页面引用视频。忽略不在那个 watch-player div 中的所有内容。解析代码,直到找到包含 URL 的变量。从那里您可能必须取消该 URL 的转义。一旦你有了它,你可以做一个file_get_contents()
(或其他一些下载方法,取决于你要做什么)来获取 URL。就是这么简单。您的 HTML 解析代码将是最复杂的。
Finally, keep in mind what you are about to do may be illegal. Check the EULA.
最后,请记住您将要做的事情可能是非法的。检查 EULA。
回答by mario
Nobody writes manuals/howtos that become outdated every four weeks. The closest you can get is inspecting the actual extraction methods in a contemporary implementation. Quite readable:
没有人编写每四个星期就会过时的手册/方法。您可以获得的最接近的是检查当代实现中的实际提取方法。相当可读:
http://bitbucket.org/rg3/youtube-dl/raw/2010.08.04/youtube-dl
http://bitbucket.org/rg3/youtube-dl/raw/2010.08.04/youtube-dl
If you don't want to read through/reimplement it, it's obviously not simple, you could just run it as-is from PHP:
如果您不想通读/重新实现它,这显然不简单,您可以按原样从 PHP 运行它:
system("youtube-dl '$url'");
回答by Rajendra
last time i was working on fixing one of the brocken chrome extension to download youtube video. I fixed it by altering the script part. (Javascript)
上次我正在修复一个 Broken chrome 扩展程序以下载 youtube 视频。我通过更改脚本部分来修复它。(Javascript)
var links = new String();
var downlink = new String();
var has22 = new Boolean();
has22 = false;
var Marked=false;
var FMT_DATA = fmt_url_map;//This is html text that you have to grab. In case of extension it was readily available through:document.getElementsByTagName('script');
var StrSplitter1='%2C', StrSplitter2='%26', StrSplitter3='%3D';
if (FMT_DATA.indexOf(',')>-1) { //Found ,
StrSplitter1=',';
StrSplitter2=(FMT_DATA.indexOf('&')>-1)?'&':'\u0026';
StrSplitter3='=';
}
var videoURL=new Array();
var FMT_DATA_PACKET=new Array();
var FMT_DATA_PACKET=FMT_DATA.split(StrSplitter1);
for (var i=0;i<FMT_DATA_PACKET.length;i++){
var FMT_DATA_FRAME=FMT_DATA_PACKET[i].split(StrSplitter2);
var FMT_DATA_DUEO=new Array();
for (var j=0;j<FMT_DATA_FRAME.length;j++){
var pair=FMT_DATA_FRAME[j].split(StrSplitter3);
if (pair.length==2) {
FMT_DATA_DUEO[pair[0]]=pair[1];
}
}
var url=(FMT_DATA_DUEO['url'])?FMT_DATA_DUEO['url']:null;
if (url==null) continue;
url=unescape(unescape(url)).replace(/\\//g,'/').replace(/\u0026/g,'&');
var itag=(FMT_DATA_DUEO['itag'])?FMT_DATA_DUEO['itag']:null;
var itag=(FMT_DATA_DUEO['itag'])?FMT_DATA_DUEO['itag']:null;
if (itag==null) continue;
var signature=(FMT_DATA_DUEO['sig'])?FMT_DATA_DUEO['sig']:null;
if (signature!=null) {
url=url+"&signature="+signature;
}
if (url.toLowerCase().indexOf('http')==0) { // validate URL
if (itag == '5') {
links += '<a href="' + url + '&title=' + username + title + quality240 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v240p">FLV (240p)</span></a>';
}
if (itag == '18') {
links += '<a href="' + url + '&title=' + username + title + quality360 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v360p">MP4 (360p)</span></a>';
}
if (itag == '35') {
links += '<a href="' + url + '&title=' + username + title + quality480 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v480p">FLV (480p)</span></a>';
}
if (itag == '22') {
links += '<a href="' + url + '&title=' + username + title + quality720 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v720p">MP4 HD (720p)</span></a>';
}
if (itag == '37') {
links += ' <a href="' + url + '&title=' + username + title + quality1080 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v1080p">MP4 HD (1080p)</span></a>';
}
if (itag == '38') {
links += '<a href="' + url + '&title=' + username + title + quality4k + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v4k">MP4 HD (4K)</span></a>';
}
FavVideo();
videoURL[itag]=url;
console.log(itag);
}
}
You can get separate video link from videoURL[itag] array. Above logic can be converted to PHP easily
您可以从 videoURL[itag] 数组中获取单独的视频链接。以上逻辑可以轻松转换为PHP
The extension can be downloaded from location http://www.figmentsol.com/chrome/ytdw/
该扩展程序可以从位置http://www.figmentsol.com/chrome/ytdw/下载
I hope this would help someone. This is working solution (date:06-04-2013)
我希望这会帮助某人。这是有效的解决方案(日期:06-04-2013)