1 | # node-ytdl-core
|
2 | [![Dependency Status](https://david-dm.org/fent/node-ytdl-core.svg)](https://david-dm.org/fent/node-ytdl-core)
|
3 | [![codecov](https://codecov.io/gh/fent/node-ytdl-core/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/node-ytdl-core)
|
4 | [![Discord](https://img.shields.io/discord/484464227067887645.svg)](https://discord.gg/V3vSCs7)
|
5 |
|
6 | Yet another youtube downloading module. Written with only Javascript and a node-friendly streaming interface.
|
7 |
|
8 | # Support
|
9 | You can contact us for support on our [chat server](https://discord.gg/V3vSCs7)
|
10 |
|
11 | # Usage
|
12 |
|
13 | ```js
|
14 | const fs = require('fs');
|
15 | const ytdl = require('ytdl-core');
|
16 | // TypeScript: import ytdl from 'ytdl-core'; with --esModuleInterop
|
17 | // TypeScript: import * as ytdl from 'ytdl-core'; with --allowSyntheticDefaultImports
|
18 | // TypeScript: import ytdl = require('ytdl-core'); with neither of the above
|
19 |
|
20 | ytdl('http://www.youtube.com/watch?v=A02s8omM_hI')
|
21 | .pipe(fs.createWriteStream('video.flv'));
|
22 | ```
|
23 |
|
24 |
|
25 | # API
|
26 | ### ytdl(url, [options])
|
27 |
|
28 | Attempts to download a video from the given url. Returns a [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable). `options` can have the following keys
|
29 |
|
30 | * `quality` - Video quality to download. Can be an [itag value](http://en.wikipedia.org/wiki/YouTube#Quality_and_formats), a list of itag values, or `highest`/`lowest`/`highestaudio`/`lowestaudio`/`highestvideo`/`lowestvideo`. `highestaudio`/`lowestaudio`/`highestvideo`/`lowestvideo` all prefer audio/video only respectively. Defaults to `highest`, which prefers formats with both video and audio.
|
31 |
|
32 | A typical video's formats will be sorted in the following way using `quality: 'highest'`
|
33 | ```
|
34 | itag container quality codecs bitrate audio bitrate
|
35 | 18 mp4 360p avc1.42001E, mp4a.40.2 696.66KB 96KB
|
36 | 137 mp4 1080p avc1.640028 4.53MB
|
37 | 248 webm 1080p vp9 2.52MB
|
38 | 136 mp4 720p avc1.4d4016 2.2MB
|
39 | 247 webm 720p vp9 1.44MB
|
40 | 135 mp4 480p avc1.4d4014 1.1MB
|
41 | 134 mp4 360p avc1.4d401e 593.26KB
|
42 | 140 mp4 mp4a.40.2 128KB
|
43 | ```
|
44 | format 18 at 360p will be chosen first since it's the highest quality format with both video and audio.
|
45 | * `filter` - Used to filter the list of formats to choose from. Can be `audioandvideo` to filter formats that contain both video and audio, `video` to filter for formats that contain video, or `videoonly` for formats that contain video and no additional audio track. Can also be `audio` or `audioonly`. You can give a filtering function that gets called with each format available. This function is given the `format` object as its first argument, and should return true if the format is preferable.
|
46 | ```js
|
47 | // Example with custom function.
|
48 | ytdl(url, { filter: format => format.container === 'mp4' })
|
49 | ```
|
50 | * `format` - Primarily used to download specific video or audio streams. This can be a specific `format` object returned from `getInfo`.
|
51 | * Supplying this option will ignore the `filter` and `quality` options since the format is explicitly provided.
|
52 | * `range` - A byte range in the form `{start: INT, end: INT}` that specifies part of the file to download, ie {start: 10355705, end: 12452856}. Not supported on segmented (DASH MPD, m3u8) formats.
|
53 | * This downloads a portion of the file, and not a separately spliced video.
|
54 | * `begin` - What time in the video to begin. Supports formats `00:00:00.000`, `0ms, 0s, 0m, 0h`, or number of milliseconds. Example: `1:30`, `05:10.123`, `10m30s`.
|
55 | * For live videos, this also accepts a unix timestamp or Date object, and defaults to `Date.now()`.
|
56 | * This option is not very reliable for non-live videos, see [#129](https://github.com/fent/node-ytdl-core/issues/129), [#219](https://github.com/fent/node-ytdl-core/issues/219).
|
57 | * `liveBuffer` - How much time buffer to use for live videos in milliseconds. Default is `20000`.
|
58 | * `requestOptions` - Anything to merge into the request options which [miniget](https://github.com/fent/node-miniget) is called with, such as `headers`.
|
59 | * `highWaterMark` - How much of the video download to buffer into memory. See [node's docs](https://nodejs.org/api/stream.html#stream_constructor_new_stream_writable_options) for more. Defaults to 512KB.
|
60 | * `dlChunkSize` - The size of the download chunk in bytes. When the chosen format is video only or audio only, the download in this case is separated into multiple chunks to avoid throttling. Defaults to 10MB.
|
61 | * `lang` - The 2 character symbol of a language. Default is `en`.
|
62 |
|
63 | #### Event: info
|
64 | * [`ytdl.videoInfo`](example/info.json) - Info.
|
65 | * [`ytdl.videoFormat`](typings/index.d.ts#L22) - Video Format.
|
66 |
|
67 | Emitted when the video's `info` is fetched, along with the chosen format to download.
|
68 |
|
69 | #### Event: progress
|
70 | * `number` - Chunk byte length.
|
71 | * `number` - Total bytes or segments downloaded.
|
72 | * `number` - Total bytes or segments.
|
73 |
|
74 | Emitted whenever a new chunk is received. Passes values describing the download progress.
|
75 |
|
76 | #### miniget events
|
77 |
|
78 | All [miniget events](https://github.com/fent/node-miniget#event-redirect) are forwarded and can be listened to from the returned stream.
|
79 |
|
80 | ### Stream#destroy()
|
81 |
|
82 | Call to abort and stop downloading a video.
|
83 |
|
84 | ### async ytdl.getBasicInfo(url, [options])
|
85 |
|
86 | Use this if you only want to get metainfo from a video.
|
87 |
|
88 | ### async ytdl.getInfo(url, [options])
|
89 |
|
90 | Gets metainfo from a video. Includes additional formats, and ready to download deciphered URL. This is what the `ytdl()` function uses internally.
|
91 |
|
92 | ### ytdl.downloadFromInfo(info, options)
|
93 |
|
94 | Once you have received metadata from a video with the `ytdl.getInfo` function, you may pass that information along with other options to this function.
|
95 |
|
96 | ### ytdl.chooseFormat(formats, options)
|
97 |
|
98 | Can be used if you'd like to choose a format yourself with the [options above](#ytdlurl-options).
|
99 | Throws an Error if it fails to find any matching format.
|
100 |
|
101 | ```js
|
102 | // Example of choosing a video format.
|
103 | let info = await ytdl.getInfo(videoID);
|
104 | let format = ytdl.chooseFormat(info.formats, { quality: '134' });
|
105 | console.log('Format found!', format);
|
106 | ```
|
107 |
|
108 | ### ytdl.filterFormats(formats, filter)
|
109 |
|
110 | If you'd like to work with only some formats, you can use the [`filter` option above](#ytdlurl-options).
|
111 |
|
112 | ```js
|
113 | // Example of filtering the formats to audio only.
|
114 | let info = await ytdl.getInfo(videoID);
|
115 | let audioFormats = ytdl.filterFormats(info.formats, 'audioonly');
|
116 | console.log('Formats with only audio: ' + audioFormats.length);
|
117 | ```
|
118 |
|
119 | ### ytdl.validateID(id)
|
120 |
|
121 | Returns true if the given string satisfies YouTube's ID format.
|
122 |
|
123 | ### ytdl.validateURL(url)
|
124 |
|
125 | Returns true if able to parse out a valid video ID.
|
126 |
|
127 | ### ytdl.getURLVideoID(url)
|
128 |
|
129 | Returns a video ID from a YouTube URL.
|
130 | Throws an Error if it fails to parse an ID.
|
131 |
|
132 | ### ytdl.getVideoID(str)
|
133 |
|
134 | Same as the above `ytdl.getURLVideoID()`, but can be called with the video ID directly, in which case it returns it. This is what ytdl uses internally.
|
135 | Throws an Error if it fails to parse an ID.
|
136 |
|
137 | ## Limitations
|
138 |
|
139 | ytdl cannot download videos that fall into the following
|
140 | * Regionally restricted (requires a [proxy](example/proxy.js))
|
141 | * Private (if you have access, requires [cookies](example/cookies.js))
|
142 | * Rentals (if you have access, requires [cookies](example/cookies.js))
|
143 |
|
144 | Generated download links are valid for 6 hours, for the same IP address.
|
145 |
|
146 | ## Handling Separate Streams
|
147 |
|
148 | Typically 1080p or better video does not have audio encoded with it. The audio must be downloaded separately and merged via an appropriate encoding library. `ffmpeg` is the most widely used tool, with many [Node.js modules available](https://www.npmjs.com/search?q=ffmpeg). Use the `format` objects returned from `ytdl.getInfo` to download specific streams to combine to fit your needs. Look at [example/ffmpeg.js](example/ffmpeg.js) for an example on doing this.
|
149 |
|
150 | ## What if it stops working?
|
151 |
|
152 | Youtube updates their website all the time, it's not that rare for this to stop working. If it doesn't work for you and you're using the latest version, feel free to open up an issue. Make sure to check if there isn't one already with the same error.
|
153 |
|
154 | If you'd like to help fix the issue, look at the type of error first. If you're getting the following error
|
155 |
|
156 | Could not extract signature deciphering actions
|
157 |
|
158 | Run the tests at `test/irl-test.js` just to make sure that this is actually an issue with ytdl-core.
|
159 |
|
160 | mocha test/irl-test.js
|
161 |
|
162 | These tests are not mocked, and they try to start downloading a few videos. If these fail, then it's time to debug.
|
163 |
|
164 | For getting started with that, you can look at the `extractActions()` function in [`/lib/sig.js`](https://github.com/fent/node-ytdl-core/blob/master/lib/sig.js).
|
165 |
|
166 |
|
167 | # Install
|
168 |
|
169 | ```bash
|
170 | npm install ytdl-core@latest
|
171 | ```
|
172 |
|
173 | Or for Yarn users:
|
174 | ```bash
|
175 | yarn add ytdl-core@latest
|
176 | ```
|
177 |
|
178 | Make sure you're installing the latest version of ytdl-core to keep up with the latest fixes.
|
179 |
|
180 | If you're using a bot or app that uses ytdl-core such as [ytdl-core-discord](https://github.com/amishshah/ytdl-core-discord) or [discord-player](https://github.com/Androz2091/discord-player), it may be dependent on an older version. To update its ytdl-core version, you'll have to fork the project and update its `package.json` file, you can't simply change the version on your project's `package.json`, the app will still use its own older version of ytdl-core.
|
181 |
|
182 | You can then submit a pull request to their project and point to your fork temporarily. You can also check their pull request and check if there's one open already, and point to that instead. To point to a github's repo's branch in your `package.json`, you can do
|
183 |
|
184 | ```json
|
185 | "ytdl-core-discord": "amishshah/ytdl-core-discord#dependabot/npm_and_yarn/ytdl-core-2.0.1"
|
186 | ```
|
187 |
|
188 | # Related Projects
|
189 |
|
190 | - [ytdl](https://github.com/fent/node-ytdl) - A cli wrapper of this.
|
191 | - [pully](https://github.com/JimmyBoh/pully) - Another cli wrapper of this aimed at high quality formats.
|
192 | - [ytsr](https://github.com/TimeForANinja/node-ytsr) - YouTube video search results.
|
193 | - [ytpl](https://github.com/TimeForANinja/node-ytpl) - YouTube playlist and channel resolver.
|
194 |
|
195 |
|
196 | # Tests
|
197 | Tests are written with [mocha](https://mochajs.org)
|
198 |
|
199 | ```bash
|
200 | npm test
|
201 | ```
|