使用 PHP 下载 Youtube 视频

2022-01-06 00:00:00 download youtube php video

我正在寻找一种使用 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) 并查找视频播放".您会在访问的第一页上看到一个点击,例如 http://www.youtube.com/watch?v=123asdf.如果你翻阅那个文件,你会看到一个带有watch-player"ID 的 DIV 标签.其中有一个脚本标签来设置 flash 播放器,里面有所有的 flash 参数.其中是视频的网址.

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().忽略不在那个 watch-player div 中的所有内容.解析代码,直到找到包含 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.

相关文章