UNPKG

8.83 kBMarkdownView Raw
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
6Yet another youtube downloading module. Written with only Javascript and a node-friendly streaming interface.
7
8# Support
9You can contact us for support on our [chat server](https://discord.gg/V3vSCs7)
10
11# Usage
12
13```js
14const fs = require('fs');
15const 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
20ytdl('http://www.youtube.com/watch?v=A02s8omM_hI')
21 .pipe(fs.createWriteStream('video.flv'));
22```
23
24
25# API
26### ytdl(url, [options])
27
28Attempts 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`.
31* `filter` - Used to decide what format to download. 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.
32* `format` - Primarily used to download specific video or audio streams. This can be a specific `format` object returned from `getInfo`.
33 * Supplying this option will ignore the `filter` and `quality` options since the format is explicitly provided.
34* `range` - A byte range in the form `{start: INT, end: INT}` that specifies part of the file to download, ie {start: 10355705, end: 12452856}.
35 * This downloads a portion of the file, and not a separately spliced video.
36* `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`. For live videos, this also accepts a unix timestamp or Date, and defaults to `Date.now()`.
37 * This option is not very reliable, see [#129](https://github.com/fent/node-ytdl-core/issues/129), [#219](https://github.com/fent/node-ytdl-core/issues/219).
38* `liveBuffer` - How much time buffer to use for live videos in milliseconds. Default is `20000`.
39* `requestOptions` - Anything to merge into the request options which [miniget](https://github.com/fent/node-miniget) is called with, such as headers.
40* `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.
41* `lang` - The 2 character symbol of a language. Default is `en`.
42
43```js
44// Example with `filter` option.
45ytdl(url, { filter: (format) => format.container === 'mp4' })
46 .pipe(fs.createWriteStream('video.mp4'));
47```
48
49#### Event: info
50* [`ytdl.videoInfo`](example/info.json) - Info.
51* [`ytdl.videoFormat`](typings/index.d.ts#L22) - Video Format.
52
53Emitted when the a video's `info` hash is fetched, along with the chosen format metadata to download. `format.url` might be different if `start` was given.
54
55#### Event: response
56* [`http.ServerResponse`](https://nodejs.org/api/http.html#http_class_http_serverresponse) - Response.
57
58Emitted when the video response has been found and has started downloading or after any successful reconnects. Can be used to get the size of the download.
59
60#### Event: progress
61* `number` - Chunk byte length.
62* `number` - Total bytes or segments downloaded.
63* `number` - Total bytes or segments.
64
65Emitted whenever a new chunk is received. Passes values describing the download progress.
66
67### Stream#destroy()
68
69Call to abort and stop downloading a video.
70
71### ytdl.getBasicInfo(url, [options], [callback(err, info)])
72
73Use this if you only want to get metainfo from a video. If `callback` isn't given, returns a promise.
74
75### ytdl.getInfo(url, [options], [callback(err, info)])
76
77Gets metainfo from a video. Includes additional formats, and ready to download deciphered URL. This is what the `ytdl()` function uses internally. If `callback` isn't given, returns a promise.
78
79### ytdl.downloadFromInfo(info, options)
80
81Once you have received metadata from a video with the `ytdl.getInfo` function, you may pass that information along with other options to this function.
82
83### ytdl.chooseFormat(formats, options)
84
85Can be used if you'd like to choose a format yourself with the [options above](#ytdlurl-options).
86
87```js
88// Example of choosing a video format.
89ytdl.getInfo(videoID, (err, info) => {
90 if (err) throw err;
91 let format = ytdl.chooseFormat(info.formats, { quality: '134' });
92 if (format) {
93 console.log('Format found!');
94 }
95});
96```
97
98### ytdl.filterFormats(formats, filter)
99
100If you'd like to work with only some formats, you can use the [`filter` option above](#ytdlurl-options).
101
102```js
103// Example of filtering the formats to audio only.
104ytdl.getInfo(videoID, (err, info) => {
105 if (err) throw err;
106 let audioFormats = ytdl.filterFormats(info.formats, 'audioonly');
107 console.log('Formats with only audio: ' + audioFormats.length);
108});
109```
110
111### ytdl.validateID(id)
112
113Returns true if the given string satisfies YouTube's ID format.
114
115### ytdl.validateURL(url)
116
117Returns true if able to parse out a valid video ID.
118
119### ytdl.getURLVideoID(url)
120
121Returns a video ID from a YouTube URL.
122
123### ytdl.getVideoID(str)
124
125Same 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.
126
127## Limitations
128
129ytdl cannot download videos that fall into the following
130* Regionally restricted (requires a [proxy](example/proxy.js))
131* Private
132* Rentals
133
134YouTube intentionally rate limits downloads, particularly audio only formats, likely to prevent bandwidth abuse. The download rate is still faster than a media player can play the video, even on 2x. See [#294](https://github.com/fent/node-ytdl-core/issues/294).
135
136Generated download links are valid for 6 hours, for the same IP address.
137
138## Handling Separate Streams
139
140Typically 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.
141
142## What if it stops working?
143
144Youtube 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.
145
146If you'd like to help fix the issue, look at the type of error first. The most common one is
147
148 Could not extract signature deciphering actions
149
150Run the tests at `test/irl-test.js` just to make sure that this is actually an issue with ytdl-core.
151
152 mocha test/irl-test.js
153
154These tests are not mocked, and they actually try to start downloading a few videos. If these fail, then it's time to debug.
155
156For 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).
157
158
159# Install
160
161```bash
162npm install ytdl-core@latest
163```
164
165Or for Yarn users:
166```bash
167yarn add ytdl-core@latest
168```
169
170If you're using a bot or app that uses ytdl-core, it may be dependent on an older version. Make sure you're installing the latest version of ytdl-core to keep up with the latest fixes.
171
172# Related Projects
173
174- [ytdl](https://github.com/fent/node-ytdl) - A cli wrapper of this.
175- [pully](https://github.com/JimmyBoh/pully) - Another cli wrapper of this aimed at high quality formats.
176- [ytsr](https://github.com/TimeForANinja/node-ytsr) - YouTube video search results.
177- [ytpl](https://github.com/TimeForANinja/node-ytpl) - YouTube playlist and channel resolver.
178
179
180# Tests
181Tests are written with [mocha](https://mochajs.org)
182
183```bash
184npm test
185```