fent / node-ytdl-core
- понедельник, 26 октября 2020 г. в 00:23:49
HTML
YouTube video downloader in javascript.
Yet another youtube downloading module. Written with only Javascript and a node-friendly streaming interface.
You can contact us for support on our chat server
const fs = require('fs');
const ytdl = require('ytdl-core');
// TypeScript: import ytdl from 'ytdl-core'; with --esModuleInterop
// TypeScript: import * as ytdl from 'ytdl-core'; with --allowSyntheticDefaultImports
// TypeScript: import ytdl = require('ytdl-core'); with neither of the above
ytdl('http://www.youtube.com/watch?v=aqz-KE-bpKQ')
.pipe(fs.createWriteStream('video.mp4'));
Attempts to download a video from the given url. Returns a readable stream. options
can have the following
quality
- Video quality to download. Can be an itag value, 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.
A typical video's formats will be sorted in the following way using quality: 'highest'
itag container quality codecs bitrate audio bitrate
18 mp4 360p avc1.42001E, mp4a.40.2 696.66KB 96KB
137 mp4 1080p avc1.640028 4.53MB
248 webm 1080p vp9 2.52MB
136 mp4 720p avc1.4d4016 2.2MB
247 webm 720p vp9 1.44MB
135 mp4 480p avc1.4d4014 1.1MB
134 mp4 360p avc1.4d401e 593.26KB
140 mp4 mp4a.40.2 128KB
format 18 at 360p will be chosen first since it's the highest quality format with both video and audio. If you'd like a higher quality format with both video and audio, see the section on handling separate streams.
filter
- Used to filter the list of formats to choose from. Can be audioandvideo
or videoandaudio
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.
// Example with custom function.
ytdl(url, { filter: format => format.container === 'mp4' })
format
- Primarily used to download specific video or audio streams. This can be a specific format
object returned from getInfo
.
filter
and quality
options since the format is explicitly provided.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.
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
.
liveBuffer
- How much time buffer to use for live videos in milliseconds. Default is 20000
.
highWaterMark
- How much of the video download to buffer into memory. See node's docs for more. Defaults to 512KB.
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. Setting it to 0 disables chunking. Defaults to 10MB.
In addition, any getInfo()
option can be given.
ytdl.videoInfo
- Info.ytdl.videoFormat
- Video Format.Emitted when the video's info
is fetched, along with the chosen format to download.
number
- Chunk length in bytes or segment number.number
- Total bytes or segments downloaded.number
- Total bytes or segments.Emitted whenever a new chunk is received. Passes values describing the download progress.
All miniget events are forwarded and can be listened to from the returned stream.
Call to abort and stop downloading a video.
Use this if you only want to get metainfo from a video.
Gets metainfo from a video. Includes additional formats, and ready to download deciphered URL. This is what the ytdl()
function uses internally.
options
can have the following
requestOptions
- Anything to merge into the request options which miniget is called with, such as headers
.lang
- The 2 character symbol of a language. Default is en
.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.
Can be used if you'd like to choose a format yourself with the options above. Throws an Error if it fails to find any matching format.
// Example of choosing a video format.
let info = await ytdl.getInfo(videoID);
let format = ytdl.chooseFormat(info.formats, { quality: '134' });
console.log('Format found!', format);
If you'd like to work with only some formats, you can use the filter
option above.
// Example of filtering the formats to audio only.
let info = await ytdl.getInfo(videoID);
let audioFormats = ytdl.filterFormats(info.formats, 'audioonly');
console.log('Formats with only audio: ' + audioFormats.length);
Returns true if the given string satisfies YouTube's ID format.
Returns true if able to parse out a valid video ID.
Returns a video ID from a YouTube URL. Throws an Error if it fails to parse an ID.
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.
Throws an Error if it fails to parse an ID.
ytdl cannot download videos that fall into the following
Generated download links are valid for 6 hours, and may only be downloadable from the same IP address.
Typically 1080p or better videos do not have audio encoded with it. The audio must be downloaded separately and merged via an encoding library. ffmpeg
is the most widely used tool, with many Node.js modules available. Use the format
objects returned from ytdl.getInfo
to download specific streams to combine to fit your needs. Look at example/ffmpeg.js for an example on doing this.
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.
Run the tests at test/irl-test.js
to make sure this is really an issue with ytdl-core.
npm run test:irl
These tests are not mocked, they try to start downloading a few videos. If these fail, then it's time to debug. If the error you're getting is signature deciphering, check lib/sig.js
. Otherwise, the error is likely within lib/info.js
.
npm install ytdl-core@latest
Or for Yarn users:
yarn add ytdl-core@latest
Make sure you're installing the latest version of ytdl-core to keep up with the latest fixes.
If you're using a bot or app that uses ytdl-core such as ytdl-core-discord or discord-player, it may be dependent on an older version. To update its ytdl-core version, that library has to 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.
Look in their repo to see if they already have an active pull request that updates ytdl-core. If they don't, open an issue asking them to update ytdl-core, or better yet, fork the project and submit a pull request with the updated version.
While you wait for the pull reques to merge, you can point to its branch in your package.json
"ytdl-core-discord": "amishshah/ytdl-core-discord#dependabot/npm_and_yarn/ytdl-core-2.0.1"
Tests are written with mocha
npm test