用PHP下载Youtube视频 [英] Downloading Youtube videos with PHP

查看:370
本文介绍了用PHP下载Youtube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用PHP下载Youtube视频的方法。我已经搜索了几个小时,但遗憾的是,我发现的所有Google搜索结果都是多年的,不再工作。

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.

如果有人可以解释如何要做到这一点,或链接到最新的文章,详细解释。

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.

非常感谢。

推荐答案

你应该做的第一件事是得到一个工具,如 Fiddler ,并访问YouTube视频页面。在Fiddler中,您将看到组成该页面的所有文件,包括FLV本身。现在,你知道视频不是CSS文件之一,也不是图像文件。你可以忽略那些。寻找一个大文件。如果您查看该URL,则以 / videoplayback 开头。

The first thing you should do is get a tool like Fiddler and 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.

现在,一旦找到它,弄清楚浏览器如何知道获取该文件。通过会话进行搜索(Ctrl + F)并查找videoplayback。您将看到您所访问的第一页上的点击,例如 http://www.youtube.com /手表?ν= 123asdf 。如果您浏览该文件,您将看到一个ID为watch-player的DIV标签。在那里有一个脚本标签来设置Flash播放器,并且在这些都是闪存参数。其中包括视频的URL。

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.

所以现在你知道如何使用你的工具来弄清楚浏览器是如何得到的。您如何在PHP中复制此行为?

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?

在引用该视频的页面上执行 file_get_contents()。忽略所有不在那个手表架上的一切。解析代码,直到找到包含该URL的变量。从那里你可能不得不解压缩该URL。一旦你拥有它,你可以做一个 file_get_contents()(或其他一些下载方法,取决于你想做什么)获取URL。这很简单您的HTML解析代码将是最复杂的。

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.

最后,请记住您要做的是非法的。检查EULA。

Finally, keep in mind what you are about to do may be illegal. Check the EULA.

这篇关于用PHP下载Youtube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆