Important Note: Version 2 is finally here! 🎉 This is still in beta, so please feel free to submit feature requests. Found any bug? Please open an issue on our GitHub repository.
This Node.js module is a wrapper for yt-dlp, a powerful video downloader, that allows you to download, stream, and fetch metadata for videos from various websites. The wrapper automatically downloads the yt-dlp binary and provides a simple interface for using its features directly within a Node.js environment.
To install the yt-dlp Node.js wrapper, run:
npm i ytdlp-nodejsThis package recommends installing FFmpeg. You can manually download it from here, or you can use the downloadFFmpeg() function to automate the process.
import { YtDlp } from 'ytdlp-nodejs';
const ytdlp = new YtDlp();async function downloadVideo() {
try {
const output = await ytdlp.downloadAsync(
'https://www.youtube.com/watch?v=_AL4IwHuHlY',
{
onProgress: (progress) => {
console.log(progress);
},
// others args
}
);
console.log('Download completed:', output);
} catch (error) {
console.error('Error:', error);
}
}
downloadVideo();import { createWriteStream } from 'fs';
async function streamVideo() {
try {
const st = createWriteStream('video.mp4');
const ytdlpStream = ytdlp.stream(
'https://www.youtube.com/watch?v=_AL4IwHuHlY',
{
onProgress: (progress) => {
console.log(progress);
},
// others args
}
);
await ytdlpStream.pipeAsync(st);
console.log('Download completed');
} catch (error) {
console.error('Error:', error);
}
}
streamVideo();The constructor initializes the YtDlp object.
opt(optional): Options to configure the paths foryt-dlpandffmpeg.binaryPath: Path to theyt-dlpbinary (optional).ffmpegPath: Path to theffmpegbinary (optional).
const ytDlp = new YtDlp({
binaryPath: 'path-to-yt-dlp',
ffmpegPath: 'path-to-ffmpeg',
});Asynchronously checks if both yt-dlp and optionally ffmpeg binaries are installed and available.
options(optional): An object to specify ifffmpegshould also be checked.ffmpeg: If set totrue, it checks ifffmpegis installed.
Promise<boolean>: Resolves totrueif bothyt-dlpandffmpegare installed (if required), otherwisefalse.
const isInstalled = await ytDlp.checkInstallationAsync({ ffmpeg: true });Synchronously checks if both yt-dlp and optionally ffmpeg binaries are installed and available.
options(optional): An object to specify ifffmpegshould also be checked.ffmpeg: If set totrue, it checks ifffmpegis installed.
boolean:trueif bothyt-dlpandffmpegare installed (if required), otherwisefalse.
const isInstalled = ytDlp.checkInstallation({ ffmpeg: true });Asynchronously executes yt-dlp with the provided URL and options.
url: The URL of the video to download or stream.options(optional): Additional options to pass toyt-dlp:onData: A callback that is triggered when data is received fromyt-dlp.onProgress: An callback function to track progess of downloading.
Promise<string>: Resolves to the output of theyt-dlpcommand.
const result = await ytDlp.execAsync(
'https://www.youtube.com/watch?v=exampleVideoID'
);Synchronously executes yt-dlp with the provided URL and options.
url: The URL of the video to download or stream.options(optional): Additional options to pass toyt-dlp.
ChildProcess: The spawned child process runningyt-dlp.on('progress'): An event to track progess of downloading.
const ytDlpProcess = ytDlp.exec(
'https://www.youtube.com/watch?v=exampleVideoID'
);Downloads a video from the given URL.
url: The URL of the video to download.options(optional): Additional options for downloading, such as video format.format: String | Format Options.
ChildProcess: The spawned child process runningyt-dlp.on('progress'): An event to track progess of downloading.
ytDlp.download('https://www.youtube.com/watch?v=exampleVideoID', {
format: 'bestvideo+bestaudio',
});Asynchronously downloads a video from the given URL.
url: The URL of the video to download.options(optional): Additional options for downloading, such as video format and a progress callback.format: String | Format Options.onProgress: An callback function to track progess of downloading.output: String | Custom output path and filename template. Uses yt-dlp output template syntax. For example:"./downloads/%(title)s.%(ext)s".
Promise<string>: Resolves to the output of theyt-dlpcommand.
const result = await ytDlp.downloadAsync(
'https://www.youtube.com/watch?v=exampleVideoID',
{
format: 'bestvideo+bestaudio',
}
);Streams a video from the given URL.
url: The URL of the video to stream.options(optional): Additional options for streaming, such as video format and a progress callback.format: String | Format Options.onProgress: An callback function to track progess of downloading.
pipe: A function that pipes the stream to a writable stream.pipeAsync: A function that pipes the stream asynchronously to a writable stream.
const ytdlpStream = ytDlp.stream(
'https://www.youtube.com/watch?v=exampleVideoID'
);
ytdlpStream.pipe(destinationStream);Fetches detailed information about a video asynchronously.
-
url: The URL of the video. -
options(optional): InfoOptions Additional options to control the fetching behavior:-
flatPlaylist?:boolean(default:true) | Iftrue, returns a flat list with limited information for playlist items. Iffalse, fetches full information for each video in the playlist. -
cookies?:string| A raw cookie header string to be used for authenticated requests. -
cookiesFromBrowser?:string| Uses cookies retrieved from the specified browser profile. -
noCookiesFromBrowser?:boolean| If true, disables automatically retrieving cookies from the browser. -
noCookies?:boolean| If true, disables the use of all cookies entirely (overrides other cookie options).
-
Promise<VideoInfo | PlaylistInfo>: Resolves to aVideoInfoorPlaylistInfoobject containing metadata about the video.
const info = await ytDlp.getInfoAsync('url');
if (info._type == 'video') {
console.log(info); // VideoInfo
}
if (info._type == 'playlist') {
console.log(info); // PlaylistInfo
}Fetches all available thumbnails for a video asynchronously.
url: The URL of the video.
Promise<VideoThumbnail[]>: Resolves to an array ofVideoThumbnailobjects.
const thumbnails = await ytDlp.getThumbnailsAsync(
'https://www.youtube.com/watch?v=exampleVideoID'
);Fetche title for a video asynchronously.
url: The URL of the video.
Promise<string>: Resolves to a string.
const title = await ytDlp.getTitleAsync(
'https://www.youtube.com/watch?v=exampleVideoID'
);Returns a File object containing the video/audio data without saving it to disk.
url: The URL of the video.options(optional): Additional options for getting the file:format: String | Format Optionsfilename: Custom filename for the resulting filemetadata: Custom metadata for the file:name: File nametype: MIME typesize: File size in bytes
onProgress: A callback function to track progress of downloading
Promise<File>: Resolves to aFileobject containing the video/audio data.
const file = await ytdlp.getFileAsync(
'https://www.youtube.com/watch?v=exampleVideoID',
{
format: {
filter: 'audioandvideo',
type: 'mp4',
quality: 'highest',
},
filename: 'custom-video.mp4',
onProgress: (progress) => {
console.log(progress);
},
}
);Downloads ffmpeg using a predefined method.
Promise<void>: Resolves once the download is complete.
await ytDlp.downloadFFmpeg();filter: "videoonly" | "audioonly" | "audioandvideo" | "mergevideo"
-
filter: "videoonly"quality:"2160p" | "1440p" | "1080p" | "720p" | "480p" | "360p" | "240p" | "144p" | "highest" | "lowest" (default: 'highest')type:"mp4" | "webm" (default:'mp4')
-
filter: "audioonly"quality:"highest" | "lowest" (default:'highest')
-
filter: "audioandvideo"quality:"highest" | "lowest" (default:'highest')type:"mp4" | "webm" (default:'mp4')
-
filter: "audioonly"quality:0 to 10 (default:5)type:"aac" | "flac" | "mp3" | "m4a" | "opus" | "vorbis" | "wav" | "alac" (default:'mp3')
-
filter: "mergevideo"quality:"2160p" | "1440p" | "1080p" | "720p" | "480p" | "360p" | "240p" | "144p" | "highest" | "lowest" (default: 'highest')format:"mkv" | "mp4" | "ogg" | "webm" | "flv" (default:'mp4')
Contributions are welcome! Feel free to submit a pull request or open an issue on GitHub.