Package Exports
- @choewy/yt-dlp
Readme
@choewy/yt-dlp
A Node.js/TypeScript wrapper around yt-dlp.
This package runs a platform-specific yt-dlp binary under the hood and uses ffmpeg-static as the default FFmpeg executable.
Features
- Fluent
YtDlpbuilder API - Downloads a platform-specific
yt-dlpbinary during installation - Uses bundled FFmpeg from
ffmpeg-staticby default - Supports custom FFmpeg paths
- Supports format selection, output templates, merge/recode formats, and audio extraction
- Enables
--no-playlistand--force-overwritesby default - Returns downloads as a file path,
Buffer, or Node.js stream - Optional debug mode that forwards yt-dlp output
Installation
npm install @choewy/yt-dlppnpm add @choewy/yt-dlppnpm approve-builds
This package uses install scripts in two places:
@choewy/yt-dlprunspostinstallto download the platform-specificyt-dlpbinary.ffmpeg-staticruns an install script to prepare the FFmpeg binary.
With pnpm, build/install scripts can be blocked until they are explicitly approved.
If you install this package with pnpm, run:
pnpm approve-buildsSelect both @choewy/yt-dlp and ffmpeg-static when prompted.
If the package was already installed before approval, rerun install or rebuild the packages:
pnpm installpnpm rebuild ffmpeg-staticpnpm rebuild @choewy/yt-dlpIf FFmpeg cannot be found, the wrapper throws this hint as well:
[@choewy/yt-dlp] ffmpeg not found. If you are using pnpm, run: pnpm approve-buildsRequirements
- Node.js 18 or newer. The install script uses the global
fetchAPI. - Network access during installation to download the
yt-dlprelease asset. - A supported operating system and architecture.
Supported Platforms
| Platform | Architecture | yt-dlp asset |
|---|---|---|
| macOS | x64, arm64 |
yt-dlp_macos |
| Linux | x64 |
yt-dlp_linux |
| Linux | arm64 |
yt-dlp_linux_aarch64 |
| Windows | x64, arm64 |
yt-dlp.exe |
Quick Start
import { YtDlp } from '@choewy/yt-dlp';
async function main() {
const path = await new YtDlp()
.url('https://www.youtube.com/watch?v=VIDEO_ID')
.output('./video.mp4')
.mergeFormat('mp4')
.toPath({ debug: true });
console.log(path);
}
void main();Examples
Download MP4 Video
import { YtDlp } from '@choewy/yt-dlp';
const path = await new YtDlp()
.url('https://www.youtube.com/watch?v=VIDEO_ID')
.output('./video.mp4')
.mergeFormat('mp4')
.toPath();Extract Audio
import { YtDlp } from '@choewy/yt-dlp';
const path = await new YtDlp()
.url('https://www.youtube.com/watch?v=VIDEO_ID')
.output('./audio.%(ext)s')
.audioOnly()
.audioFormat('mp3')
.toPath({ debug: true });Download as Buffer
import { YtDlp } from '@choewy/yt-dlp';
const buffer = await new YtDlp()
.url('https://www.youtube.com/watch?v=VIDEO_ID')
.mergeFormat('mp4')
.toBuffer();Download as Stream
import { createWriteStream } from 'fs';
import { YtDlp } from '@choewy/yt-dlp';
const stream = await new YtDlp()
.url('https://www.youtube.com/watch?v=VIDEO_ID')
.mergeFormat('mp4')
.toStream();
stream.pipe(createWriteStream('./video.mp4'));Use a Custom FFmpeg Binary
import { YtDlp } from '@choewy/yt-dlp';
const path = await new YtDlp()
.ffmpeg('/usr/local/bin/ffmpeg')
.url('https://www.youtube.com/watch?v=VIDEO_ID')
.output('./video.mp4')
.mergeFormat('mp4')
.toPath();API
new YtDlp()
Creates a new builder instance.
Default options:
| Option | Default |
|---|---|
| FFmpeg | ffmpeg-static path |
| Format | bv*[vcodec^=avc1][ext=mp4]+ba[ext=m4a]/b[ext=mp4]/b |
| Playlist handling | --no-playlist |
| Overwrite handling | --force-overwrites |
Methods
| Method | Description |
|---|---|
ffmpeg(path: string) |
Sets the FFmpeg executable path passed to --ffmpeg-location. |
format(format: string) |
Sets the yt-dlp format selector passed to -f. |
output(path: string) |
Sets the output path or yt-dlp output template passed to -o. Required. |
noPlaylist() |
Adds --no-playlist. This is already enabled by default. |
mergeFormat(format) |
Sets --merge-output-format. Accepts mp4, mkv, or webm. |
recodeVideo(format) |
Sets --recode-video. Accepts mp4, mkv, or webm. |
audioOnly() |
Adds -x to extract audio. |
audioFormat(format) |
Sets --audio-format. Accepts mp3, m4a, aac, or wav. |
url(url: string) |
Sets the source URL. Required. |
args() |
Builds and returns the yt-dlp argument list without executing it. |
toBuffer(options?) |
Runs yt-dlp and returns the downloaded file as a Buffer. |
toPath(options?) |
Runs yt-dlp, writes to output(path), and returns the resolved file path. |
toStream(options?) |
Runs yt-dlp and returns the downloaded file as a Node.js Readable stream. |
exec(options?) |
Alias for toPath(options?) for existing callers. |
toBuffer(), toPath(), toStream(), and exec() accept:
type YtDlpRunOptions = {
debug?: boolean;
};When debug is true, yt-dlp output is printed directly. When debug is omitted or false, output is captured and included in thrown errors.
Constraints
url()andoutput()must be set before callingargs()ortoPath().toBuffer()andtoStream()requireurl()but use a temporary file internally, sooutput()is optional for those methods.- FFmpeg must exist at the configured path.
audioOnly()andmergeFormat()cannot be used together.- The package currently exposes a library API only; it does not register a package CLI command.
Development
pnpm install
pnpm approve-builds
pnpm build
pnpm testUseful scripts:
| Script | Description |
|---|---|
pnpm build |
Builds TypeScript into dist. |
pnpm test |
Runs Jest. |
pnpm lint |
Runs ESLint with --fix. |
pnpm dev |
Runs src/main.ts with nodemon. |
Troubleshooting
ffmpeg not found
If you use pnpm, run pnpm approve-builds and approve both @choewy/yt-dlp and ffmpeg-static, then reinstall or rebuild the packages.
You can also provide your own FFmpeg executable:
new YtDlp().ffmpeg('/path/to/ffmpeg');yt-dlp download failed
The install script downloads from GitHub releases. Check your network, proxy, firewall, or GitHub availability, then reinstall the package.
Unsupported platform
Only the platform and architecture combinations listed in Supported Platforms are mapped to a bundled yt-dlp release asset.