YBD Project fork of ytdl-core
. This fork is dedicated to developing a YouTube downloader that is fast, stable, and takes into account various use cases, with reference to LuanRT/YouTube.js and yt-dlp.
[!NOTE] If you are looking for v5 documentation for
@ybd-project/ytdl-core
, please click here.
[!CAUTION] Currently, the account that manages this ytdl-core repository has been flagged as spam, so we are accepting issues, etc. at ybd-project-ver1/ytdl-core instead. Thank you for your cooperation.
To use @ybd-project/ytdl-core
without problems, use Node.js 16 or higher. (Recommended is Node.js 18 or higher.)
[!IMPORTANT] Use with Node.js 16 is not recommended, but will be supported as much as possible.
[!IMPORTANT]
@ybd-project/ytdl-core
has not been tested in non-Node.js environments such as Deno. If you need ytdl-core optimized for these environments, please create an issue.
As usual, when using Node.js, as noted in the prerequisites, v16 or higher will work fine. If you have an example that does not work with 16 or higher versions, please create an issue.
[!NOTE] If the Node.js version is less than v16, an error will occur when creating an instance of YtdlCore. To disable it, set the option
disableVersionCheck
totrue
. (Deprecated)
When using a browser, the latest version is preferred due to the API used. However, when operating a website or other site, it is unknown which version and browser the client will use, so the following are the main browsers (Chrome, Edge, Firefox, Brave, Opera, Safari) that are currently confirmed to work.
[!NOTE] In
@ybd-project/ytdl-core
, if a browser is determined to be not in line with the following versions, an error will be raised when instantiating the YtdlCore class, stating that the version is not supported. To disable it, set the optiondisableVersionCheck
totrue
. (Deprecated)
Live demo used for testing: ytdlcore.static.jp
Browser Name | Supported Versions |
---|---|
Google Chrome | v76 - latest |
Microsoft Edge | v80 - latest |
Mozilla FireFox | v78 - latest |
Apple Safari | v14 - latest |
Brave | v1 - latest |
Opera | v63 - latest |
(Tested with BrowserStack)
We have confirmed that ytdl-core
for serverless functions works properly in the following environment.
[!TIP] We recommend deploying to Cloudflare Workers because of its simplicity and lower cost compared to other platforms.
Service Name | Remarks |
---|---|
Cloudflare Workers | With nodejs_compat compatibility flag |
Vercel Functions | Streaming doesn't work. |
npm install @ybd-project/ytdl-core@latest
Make sure you're installing the latest version of @ybd-project/ytdl-core
to keep up with the latest fixes.
For details API documentation, see the Wiki.
Only a simple example is given in the README. For a list of options and other advanced usage, please refer to the API Documentation.
import fs from 'fs';
import { YtdlCore } from '@ybd-project/ytdl-core';
// For browser: import { YtdlCore } from '@ybd-project/ytdl-core/browser';
// For serverless functions: import { YtdlCore } from '@ybd-project/ytdl-core/serverless';
// JavaScript: const { YtdlCore } = require('@ybd-project/ytdl-core');
const ytdl = new YtdlCore({
// The options specified here will be the default values when functions such as getFullInfo are executed.
});
// Download a video
ytdl.download('https://www.youtube.com/watch?v=dQw4w9WgXcQ', {
streamType: 'nodejs', // Note: If you do not set the `streamType` to `nodejs`, a pipable stream will not be returned.
}).then((stream) => stream.pipe(fs.createWriteStream('video.mp4')));
// Get video info
ytdl.getBasicInfo('https://www.youtube.com/watch?v=dQw4w9WgXcQ').then((info) => {
console.log(info.videoDetails.title);
});
@ybd-project/ytdl-core
supports OAuth2 Token.
These can be used to avoid age restrictions and bot errors. See below for instructions on how to use them.
[!IMPORTANT] Be sure to generate tokens with accounts that can be banned, as accounts may be banned. Note that OAuth2 may expire soon these days. In this case, do not use OAuth2.
[!NOTE] The specified OAuth2 token is automatically updated by ytdl-core, so you do not need to update it yourself.
import { YtdlCore } from '@ybd-project/ytdl-core';
/* Normal usage */
const ytdl = new YtdlCore({
oauth2Credentials: {
accessToken: '...',
refreshToken: '...',
expiryDate: 'yyyy-MM-ddThh-mm-ssZ',
},
});
/* If you need to specify a client ID and secret */
const ytdl = new YtdlCore({
oauth2Credentials: {
accessToken: '...',
refreshToken: '...',
expiryDate: 'yyyy-MM-ddThh-mm-ssZ',
clientData: {
clientId: '...',
clientSecret: '...',
},
},
});
/* Specify to the function if there is a need to override what was specified during class initialization. */
// This `ytdl` is already initialized as in the other examples.
ytdl.getFullInfo('https://www.youtube.com/watch?v=dQw4w9WgXcQ', {
oauth2Credentials: {
accessToken: '...',
refreshToken: '...',
expiryDate: 'yyyy-MM-ddThh-mm-ssZ',
},
});
There are two recommended methods for generating OAuth2 tokens.
[!TIP] The method of production with cobalt is very stable and is recommended. Tokens generated using cobalt can be used in the normal way.
[!IMPORTANT] If you generate it yourself, specify the client ID and secret in
clientData
. This is required to refresh the token.
To generate tokens using Cobalt, execute the following command.
git clone https://github.com/imputnet/cobalt
cd cobalt/api/src
npm install -g pnpm
pnpm install
npm run token:youtube
If you wish to generate your own, please refer to the example folder for an example.
@ybd-project/ytdl-core
supports PoToken
.
The PoToken
can be used to avoid bot errors and must be specified with VisitorData
. If you need to obtain PoToken
or VisitorData
, please use the following repository to generate them.
import { YtdlCore } from '@ybd-project/ytdl-core';
/* Basic Method */
const ytdl = new YtdlCore({ poToken: 'PO_TOKEN', visitorData: 'VISITOR_DATA' });
// PoToken, etc. specified at the time of class instantiation will be used.
// PoToken used: PO_TOKEN
ytdl.getFullInfo('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
// Specified as function arguments take precedence over those specified at the time of class instantiation.
// PoToken used: OVERRIDE_PO_TOKEN
ytdl.getFullInfo('https://www.youtube.com/watch?v=dQw4w9WgXcQ', { poToken: 'OVERRIDE_PO_TOKEN', visitorData: 'OVERRIDE_VISITOR_DATA' });
The PoToken
is automatically generated if not specified by default. To disable this, set the option disablePoTokenAutoGeneration
to true
.
import { YtdlCore } from '@ybd-project/ytdl-core';
const ytdl = new YtdlCore({ disablePoTokenAutoGeneration: true });
@ybd-project/ytdl-core
supports proxies.
[!IMPORTANT] Try PoToken or OAuth2 before using a proxy. These may have the same effect as proxies.
Starting with v6.0.0, the createProxyAgent
function and others are obsolete. Proxies must be implemented independently through the fetcher
function.
import { YtdlCore } from '@ybd-project/ytdl-core';
import { fetch, ProxyAgent } from 'undici';
const AGENT = new ProxyAgent('http://xxx.xxx.xxx.xxx:PORT'),
ytdl = new YtdlCore({
fetcher: (url, options) => {
const REQUEST_OPTIONS: RequestInit = {
...options,
dispatcher: AGENT,
};
return fetch(url, REQUEST_OPTIONS);
},
});
ytdl.getFullInfo('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
Using a proxy sold by one service may not work. In such cases, you can deploy your own proxy, e.g., to Cloudflare Workers.
See the example for a proxy server implementation.
import { YtdlCore } from '@ybd-project/ytdl-core';
const ytdl = new YtdlCore({
originalProxy: {
base: 'http://localhost:6543',
download: 'http://localhost:6543/video-download',
urlQueryName: 'apiUrl',
},
});
/* With rewriteRequest, you can specify various things. (e.g., random selection of multiple proxies) */
const ytdl = new YtdlCore({
rewriteRequest: (url, options, { isDownloadUrl }) => {
if (isDownloadUrl) {
// URL is like: https://***.googlevideo.com/playbackvideo?...
return {
url: `https://your-video-proxy.server.com/?url=${encodeURIComponent(url)}`,
options,
};
}
return {
url: `https://your-proxy.server.com/?url=${encodeURIComponent(url)}`,
options,
};
},
});
ytdl.getFullInfo('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
See the Examples folder for examples of using @ybd-project/ytdl-core
.
@ybd-project/ytdl-core
is unable to retrieve or download information from the following videos.
The URL to view the retrieved video is valid for 6 hours. (In some cases, downloading may only be possible from the same IP.)
When doing too many requests YouTube might block. This will result in your requests getting denied with HTTP-StatusCode 429. The following steps might help you:
@ybd-project/ytdl-core
to the latest versionDistributed under the MIT License.