kclauhk/youtube-downloader
Composer 安装命令:
composer require kclauhk/youtube-downloader
包简介
PHP powered alternative for youtube-dl
README 文档
README
This is forked from Athlon1600/youtube-downloader
YouTube Downloader
This project was inspired by a very popular youtube-dl python package:
https://github.com/ytdl-org/youtube-dl
Yes, there are multiple other PHP-based youtube downloaders on the Internet, but most of them haven't been updated in years, or they depend on youtube-dl itself.
Pure PHP-based YouTube downloaders that work, and are kept-up-to date just do not exist.
For v4.1.x,
the script uses no JavaScript interpreters, no calls to shell... nothing but pure PHP with no heavy dependencies either.
That's all there is to it!
⚠️ Legal Disclaimer
This program is for personal use only. Downloading copyrighted material without permission is against YouTube's terms of services. By using this program, you are solely responsible for any copyright violations. We are not responsible for people who attempt to use this program in any way that breaks YouTube's terms of services.
Installation
Recommended way of installing this is via Composer:
composer require kclauhk/youtube-downloader
For pure PHP version:
composer require kclauhk/youtube-downloader "~4.1.2"
Changes in this fork
translated video details (if available) are supported
- To specify the desired language (by YouTube language code)
$downloadOptions = $youtube->getDownloadLinks($url, ['lang'=>'fr']);
- To specify the desired language and player client
$downloadOptions = $youtube->getDownloadLinks($url, ['client'=>'android', 'lang'=>'fr']);
(The old way to specify the player client(s) remains valid)
DASH/HLS manifest (available in "visionos", "web" *cookies required, and live streams)
To get the URL of the HLS manifest
$manifestUrl = $downloadOptions->getHlsManifestUrl();
To get the URL of the DASH manifest of live stream with android_vr
$manifestUrl = $downloadOptions->getDashManifestUrl();
(For private/age-restricted videos, fetching the manifest (.m3u8) and MPEG-TS segments requires the same set of cookies)
n/sig deciphering is supported
Deno (an open-source JavaScript runtime) and the PHP exec() function are required. To use this project with Deno, you can either:
- put the Deno binary executable into the "vendor/kclauhk/youtube-downloader/src" directory; or
- specify the directory containing the binary executable by
$youtube->getJsrt()->setPath('path of the directory');
You may use other JS runtimes. Here are some examples.
Hence, "TVHTML5", which requires n deciphering, is added (client ID: tv) and is set as default client if a JS runtime is available.
player client can be added/modified
The currently available clients are android_vr, tv, visionos, web (Safari user agent) and web_embedded.
You can add additional clients/modify the built-in clients by:
$youtube->getApiClients()->setClient($clientId, $context);
$clientId- ID of the player client$context- data in an array of key-value pairs which must contain "clientName" and "clientVersion",
for example, adding "MWEB":$client = array( 'context' => [ 'client' => [ 'clientName' => 'MWEB', 'clientVersion' => '2.20260115.01.00', 'userAgent' => 'Mozilla/5.0 (iPad; CPU OS 16_7_10 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1', ], ], 'client_name' => 2, 'supports_cookies' => true, ); $youtube->getApiClients()->setClient('mweb', $client); $downloadOptions = $youtube->getDownloadLinks($url, 'mweb'); // use 'mweb'
(client which requires a PO token is not supported)
Changes since v4.1.0
- Two YouTube clients (client ID: "android_vr"
and "ios") are built into YouTubeDownloader- To specify a player client
$downloadOptions = $youtube->getDownloadLinks($url, $clientId);
$downloadOptions = $youtube->getDownloadLinks($url);will use the default client
- To specify a player client
StreamFormatobject now containsaudioTrack,indexRangeandisDrcproperties- YouTubeStreamer accepts custom request headers (this can be used for streaming media from sources that require specific headers)
$headers = array("origin: $origin", "referer: $referer"); $youtube->stream($url, $headers);
Example usage
use YouTube\YouTubeDownloader; use YouTube\Exception\YouTubeException; $youtube = new YouTubeDownloader(); try { $downloadOptions = $youtube->getDownloadLinks("https://www.youtube.com/watch?v=aqz-KE-bpKQ"); if ($downloadOptions->getAllFormats()) { echo $downloadOptions->getFirstCombinedFormat()->url; } else { echo 'No links found'; } } catch (YouTubeException $e) { echo 'Something went wrong: ' . $e->getMessage(); }
getDownloadLinks method returns a DownloadOptions type object, which holds an array of stream links - some that are audio-only, and some that are both audio and video combined into one.
For typical usage, you are probably interested in dealing with combined streams, for that case, there is the getCombinedFormats method.
Other Features
- Stream YouTube videos directly from your server:
$streamer = new \YouTube\YouTubeStreamer(); $streamer->stream('https://r4---sn-n4v7knll.googlevideo.com/videoplayback?...');
- Pass in your own cookies to YouTubeStreamer
Cookies are needed for accessing stream links of private/age-restricted videos obtained through the web/web_embedded client. Set cookie file before streaming:
$streamer->setCookieFile('./your_cookies.txt');
- Pass in your own cookies/user-agent
If you try downloading private/age-restricted videos, YouTube will ask you to login. The only way to make this work, is to login to your YouTube account in your own web-browser, export those newly set cookies from your browser into a file, and then pass it all to youtube-downloader for use.
$youtube = new YouTubeDownloader(); $youtube->getBrowser()->setCookieFile('./your_cookies.txt'); $youtube->getBrowser()->setUserAgent('Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0');
See also:
https://github.com/ytdl-org/youtube-dl/blob/master/README.md#how-do-i-pass-cookies-to-youtube-dl
- Before you continue to YouTube...
Depending on your region, you might be force redirected to a page that asks you to agree to Google's cookie policy.
You can programmatically agree to those terms, and bypass that warning permanently via consentCookies method on your Browser instance. Example:
$youtube = new YouTubeDownloader(); $youtube->getBrowser()->consentCookies();
How does it work
A more detailed explanation on how to download videos from YouTube will be written soon. For now, there is this:
Miscellaneous Links
- https://gitlab.futo.org/videostreaming/plugins/youtube
- https://tyrrrz.me/blog/reverse-engineering-youtube-revisited
- https://github.com/TeamNewPipe/NewPipeExtractor/blob/d83787a5ca308c4ca4e86e63a8b63c5e7c4708d6/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java
- https://github.com/ytdl-org/youtube-dl/blob/master/youtube_dl/extractor/youtube.py
- https://github.com/yt-dlp/yt-dlp
To-do list
kclauhk/youtube-downloader 适用场景与选型建议
kclauhk/youtube-downloader 是一款 基于 HTML 开发的 Composer 扩展包,目前已累计 1.97k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2025 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「youtube downloader」 「download youtube」 「download youtube videos」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kclauhk/youtube-downloader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kclauhk/youtube-downloader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kclauhk/youtube-downloader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
The NzoFileDownloaderBundle is a Symfony Bundle used to Download all types of files from servers and Web applications safely and with ease. You can also read/show the file content in the Web Browser.
Adds a download link next to assets
PHP powered alternative for youtube-dl
Utility for downloading Microsoft Edge Driver binary with browser version detection
Downloader Library
统计信息
- 总下载量: 1.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-02