nodejs-ffmpeg
✨ Features
- 🚀 Auto-Download Binaries - FFmpeg binaries are automatically downloaded when needed
- 🔗 Dual API - Both fluent (chainable) and object-based APIs
- 📦 TypeScript First - Full TypeScript support with complete type definitions
- 🎬 Built-in Presets - Ready-to-use presets for common conversions
- 📊 Progress Tracking - Real-time progress with percentage and time tracking
- 🎯 FFprobe Integration - Extract metadata from any media file
- 🌐 Cross-Platform - Works on Windows, macOS, and Linux
- ⚡ ESM & CJS - Supports both module systems
📦 Installation
npm install nodejs-ffmpeg
yarn add nodejs-ffmpeg
pnpm add nodejs-ffmpeg
🚀 Quick Start
Object-Based API (Recommended)
import { convert, extractAudio, takeScreenshot } from "nodejs-ffmpeg";
await convert({
input: "input.mp4",
output: "output.webm",
videoCodec: "libvpx-vp9",
crf: 28,
onProgress: (p) => console.log(`${p.percent}%`),
});
await extractAudio({
input: "video.mp4",
output: "audio.mp3",
format: "mp3",
bitrate: "192k",
});
await takeScreenshot({
input: "video.mp4",
output: "thumbnail.jpg",
time: 5,
width: 1280,
});
Fluent API
import { FFmpeg } from "nodejs-ffmpeg";
const result = await new FFmpeg()
.input("input.mp4")
.output("output.webm")
.videoCodec("libvpx-vp9")
.audioCodec("libopus")
.on("progress", (p) => console.log(`${p.percent}%`))
.run();
Run Custom Commands
import { runCommand, runCommandString } from "nodejs-ffmpeg";
await runCommand({
args: ["-i", "input.mp4", "-vf", "scale=1280:-1", "-y", "output.mp4"],
onProgress: (p) => console.log(p.timemark),
});
await runCommandString('-i input.mp4 -vf "scale=1280:-1" -y output.mp4');
📚 API Reference
Standalone Functions
convert(options) | Generic conversion with 50+ options |
extractAudio(options) | Extract audio track (mp3, aac, opus, flac, wav) |
takeScreenshot(options) | Capture frame at specific time |
trim(options) | Cut video to time range (fast copy mode) |
compress(options) | Reduce file size with quality presets |
merge(options) | Combine video + audio files |
concat(options) | Join multiple videos |
toGif(options) | Create animated GIF |
addWatermark(options) | Overlay image with position/opacity |
changeSpeed(options) | Speed up/slow down with audio sync |
rotate(options) | Rotate 90/180/270 or flip |
runCommand(options) | Execute custom FFmpeg command |
Convert Options
await convert({
input: "input.mp4",
output: "output.mp4",
videoCodec: "libx264",
videoBitrate: "2M",
crf: 23,
preset: "fast",
fps: 30,
size: "1920x1080",
scale: "1280:-1",
audioCodec: "aac",
audioBitrate: "128k",
sampleRate: 44100,
channels: 2,
seek: 10,
duration: 60,
videoFilter: "scale=1280:-1,eq=brightness=0.1",
audioFilter: "volume=2",
noVideo: false,
noAudio: false,
overwrite: true,
threads: 4,
hwAccel: "cuda",
onProgress: (p) => {},
onStart: (cmd) => {},
});
import { FFprobe, getMetadata, getDuration } from "nodejs-ffmpeg";
const duration = await getDuration("video.mp4");
const metadata = await getMetadata("video.mp4");
const ffprobe = new FFprobe();
const info = await ffprobe.getMetadata("video.mp4");
console.log(info.format.duration);
console.log(info.format.size);
console.log(info.streams[0].codec_name);
Video Presets (Fluent API)
import { toMP4, toWebM, toHEVC, toHLS, toDASH } from "nodejs-ffmpeg";
await toMP4("input.webm", "output.mp4", { quality: "medium" }).run();
await toWebM("input.mp4", "output.webm", { quality: "high" }).run();
await toHEVC("input.mp4", "output.mp4", { crf: 28 }).run();
await toHLS("input.mp4", "./hls/", { segmentDuration: 4 }).run();
await toDASH("input.mp4", "./dash/", { segmentDuration: 4 }).run();
Binary Management
import {
downloadBinaries,
areBinariesDownloaded,
getFFmpegPath,
setFFmpegPath,
} from "nodejs-ffmpeg";
if (!areBinariesDownloaded()) {
await downloadBinaries({
onProgress: (p) => console.log(`${p.filename}: ${p.percent}%`),
});
}
setFFmpegPath("/usr/local/bin/ffmpeg");
📁 Examples
The examples/ directory contains comprehensive examples:
basic-conversion.ts | Simple video conversion |
object-api.ts | Object-based API usage |
custom-cli.ts | Custom FFmpeg commands |
probe-metadata.ts | FFprobe metadata extraction |
presets.ts | Built-in presets |
filters.ts | Video filters and effects |
effects.ts | Color, blur, rotation effects |
audio-processing.ts | Audio extraction and manipulation |
thumbnails.ts | Thumbnails and previews |
batch-processing.ts | Process multiple files |
streaming.ts | HLS/DASH streaming |
Run any example:
npx tsx examples/object-api.ts
🔧 Requirements
- Node.js 18.0.0 or higher
- FFmpeg binaries (auto-downloaded or system-installed)
📄 License
MIT © 2024
Built with ❤️ for the Node.js community