Complete API reference for ffmpeg-oneclick
npm install @ffmpeg-oneclick/core @ffmpeg-oneclick/binimport { ffmpeg } from '@ffmpeg-oneclick/core';
await ffmpeg('input.mp4')
.output('output.mp4')
.run();Main entry point for creating FFmpeg operations.
function ffmpeg(input?: InputType): ChainableFFmpegParameters:
input(optional): Input file path or stream. Can be string, Buffer, or Stream.
Returns: ChainableFFmpeg instance
Example:
// From file
const command = ffmpeg('input.mp4');
// From stream
const command = ffmpeg(readStream);
// Multiple inputs
const command = ffmpeg()
.input('video.mp4')
.input('audio.mp3');Add an input file or stream.
.input(input: InputType): thisParameters:
input: Input file path, Buffer, or Stream
Returns: this (chainable)
Example:
await ffmpeg()
.input('video.mp4')
.output('output.mp4')
.run();Set the output file path or stream.
.output(output: OutputType): thisParameters:
output: Output file path or writable stream
Returns: this (chainable)
Set video codec.
.videoCodec(codec: string): thisParameters:
codec: Video codec name (e.g., 'libx264', 'libx265', 'vp9')
Common Codecs:
libx264- H.264/AVClibx265- H.265/HEVCvp9- VP9av1- AV1
Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.videoCodec('libx265')
.run();Set video resolution.
.size(resolution: string): thisParameters:
resolution: Resolution string or preset
Presets:
'4k'or'2160p'- 3840x2160'1080p'- 1920x1080'720p'- 1280x720'480p'- 854x480- Custom:
'1280x720'
Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.size('1080p')
.run();Set frame rate.
.fps(frameRate: number): thisParameters:
frameRate: Frames per second
Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.fps(30)
.run();Set video bitrate.
.videoBitrate(bitrate: string | number): thisParameters:
bitrate: Bitrate value (e.g., '1M', '1500k', 1500000)
Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.videoBitrate('2M')
.run();Set audio codec.
.audioCodec(codec: string): thisParameters:
codec: Audio codec name (e.g., 'aac', 'mp3', 'opus')
Common Codecs:
aac- AACmp3- MP3opus- Opusflac- FLAC
Set audio bitrate.
.audioBitrate(bitrate: string | number): thisParameters:
bitrate: Bitrate value (e.g., '128k', '192k', 192000)
Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.audioBitrate('192k')
.run();Adjust audio volume.
.volume(volume: number): thisParameters:
volume: Volume multiplier (1.0 = 100%, 0.5 = 50%, 2.0 = 200%)
Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.volume(1.5) // 150% volume
.run();Trim video to specified time range.
.trim(startTime: number, endTime?: number): thisParameters:
startTime: Start time in secondsendTime(optional): End time in seconds. If omitted, trims to end of video
Example:
// Trim 10-20 seconds
await ffmpeg('input.mp4')
.output('output.mp4')
.trim(10, 20)
.run();
// Trim from 5 seconds to end
await ffmpeg('input.mp4')
.output('output.mp4')
.trim(5)
.run();Rotate video.
.rotate(angle: number): thisParameters:
angle: Rotation angle in degrees (90, 180, 270)
Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.rotate(90)
.run();Flip video.
.flip(direction: 'horizontal' | 'vertical'): thisParameters:
direction: 'horizontal' or 'vertical'
Add image watermark.
.watermark(image: string, options?: WatermarkOptions): thisParameters:
image: Path to watermark imageoptions: Watermark configuration
WatermarkOptions:
interface WatermarkOptions {
position?: Position | { x: number; y: number };
opacity?: number; // 0-1
scale?: number; // 0-1
}Position Presets:
'topLeft','topCenter','topRight''centerLeft','center','centerRight''bottomLeft','bottomCenter','bottomRight'
Example:
await ffmpeg('video.mp4')
.watermark('logo.png', {
position: 'bottomRight',
opacity: 0.8,
scale: 0.2
})
.output('output.mp4')
.run();Add text watermark.
.textWatermark(text: string, options?: TextWatermarkOptions): thisParameters:
text: Watermark textoptions: Text watermark configuration
TextWatermarkOptions:
interface TextWatermarkOptions {
fontSize?: number;
fontColor?: string;
fontFamily?: string;
position?: Position | { x: number; y: number };
opacity?: number;
box?: number;
boxColor?: string;
}Example:
await ffmpeg('video.mp4')
.textWatermark('© 2024 My Brand', {
fontSize: 24,
fontColor: 'white',
position: 'bottomLeft',
opacity: 0.7
})
.output('output.mp4')
.run();Take a single screenshot.
.screenshot(timestamp: number, output: string): thisParameters:
timestamp: Time in secondsoutput: Output file path
Example:
await ffmpeg('video.mp4')
.screenshot(5, 'frame.jpg')
.run();Take multiple screenshots.
.screenshots(options: ScreenshotOptions): thisScreenshotOptions:
interface ScreenshotOptions {
timestamps: number[];
filenameTemplate?: string;
size?: string;
}Example:
await ffmpeg('video.mp4')
.screenshots({
timestamps: [1, 5, 10, 15],
filenameTemplate: 'shot_%d.jpg',
size: '1280x720'
})
.run();Convert to HLS format.
.toHLS(output: string, options?: HLSOptions): Promise<FFmpegResult>HLSOptions:
interface HLSOptions {
segmentDuration?: number; // Default: 10
playlistName?: string;
segmentName?: string;
fmp4?: boolean;
}Example:
await ffmpeg('input.mp4')
.toHLS('playlist.m3u8', {
segmentDuration: 10
});Convert to DASH format.
.toDASH(output: string, options?: DASHOptions): Promise<FFmpegResult>Example:
await ffmpeg('input.mp4')
.toDASH('manifest.mpd', {
segmentDuration: 10
});Mix multiple audio tracks.
.mix(tracks: AudioTrack[]): thisAudioTrack:
interface AudioTrack {
input: string;
volume?: number;
delay?: number; // milliseconds
}Example:
await ffmpeg('video.mp4')
.output('output.mp4')
.mix([
{ input: 'video.mp4', volume: 1.0 },
{ input: 'music.mp3', volume: 0.3 }
])
.run();Add or modify metadata.
.metadata(key: string, value: string): thisExample:
await ffmpeg('input.mp4')
.output('output.mp4')
.metadata('title', 'My Video')
.metadata('artist', 'My Name')
.run();Remove all metadata.
.noMetadata(): thisEnable hardware acceleration.
.hardwareAccelerate(type: 'auto' | 'nvenc' | 'qsv' | 'vce' | 'videotoolbox'): thisParameters:
type: Acceleration type or 'auto' for auto-detection
Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.hardwareAccelerate('auto')
.run();Register event handler.
.on(event: string, handler: Function): thisEvents:
'progress'- Progress updates'end'- Conversion complete'error'- Error occurred'start'- FFmpeg started
Progress Object:
interface Progress {
percent: number;
eta: number; // seconds
currentFrame: number;
totalFrames: number;
fps: number;
speed: number;
bitrate: string;
}Example:
await ffmpeg('input.mp4')
.output('output.mp4')
.on('progress', (progress) => {
console.log(`${progress.percent.toFixed(1)}%`);
})
.on('end', (result) => {
console.log('Complete!');
})
.run();Execute the FFmpeg command.
.run(): Promise<FFmpegResult>Returns: Promise that resolves with result
FFmpegResult:
interface FFmpegResult {
output: string;
duration: number; // milliseconds
size: number; // bytes
command: string;
logs: string;
}Example:
const result = await ffmpeg('input.mp4')
.output('output.mp4')
.run();
console.log(`Output: ${result.output}`);
console.log(`Duration: ${result.duration}ms`);
console.log(`Size: ${result.size} bytes`);Get the FFmpeg command without executing.
.getCommand(): stringReturns: FFmpeg command string
Example:
const cmd = ffmpeg('input.mp4')
.output('output.mp4')
.size('720p')
.getCommand();
console.log(cmd); // "ffmpeg -i input.mp4 -s 1280x720 output.mp4"Compress video with quality preset.
presets.compressVideo(
input: string,
output: string,
quality: 'high' | 'medium' | 'low'
): Promise<FFmpegResult>Example:
await presets.compressVideo('input.mp4', 'output.mp4', 'high');Convert video to GIF.
presets.toGif(
input: string,
output: string,
options?: {
startTime?: number;
duration?: number;
fps?: number;
size?: string;
}
): Promise<FFmpegResult>Example:
await presets.toGif('input.mp4', 'output.gif', {
startTime: 5,
duration: 3,
fps: 15,
size: '480x270'
});Extract audio from video.
presets.extractAudio(input: string, output: string): Promise<FFmpegResult>Example:
await presets.extractAudio('video.mp4', 'audio.mp3');Get video metadata.
getMetadata(input: string): Promise<MetadataInfo>Returns:
interface MetadataInfo {
duration: number;
width: number;
height: number;
fps: number;
videoCodec: string;
audioCodec: string;
bitrate: number;
size: number;
}Example:
const meta = await getMetadata('video.mp4');
console.log(`Duration: ${meta.duration}s`);
console.log(`Resolution: ${meta.width}x${meta.height}`);Install a plugin.
usePlugin(plugin: Plugin): Promise<void>Plugin Interface:
interface Plugin {
name: string;
version: string;
install: (context: PluginContext) => void | Promise<void>;
uninstall?: () => void | Promise<void>;
}Example:
const myPlugin = {
name: 'my-plugin',
version: '1.0.0',
install: (context) => {
context.registerPreset('my:preset', {
name: 'My Preset',
videoCodec: 'libx264'
});
}
};
await usePlugin(myPlugin);type InputType = string | Buffer | ReadableStream;type OutputType = string | WritableStream;type Position =
| 'topLeft' | 'topCenter' | 'topRight'
| 'centerLeft' | 'center' | 'centerRight'
| 'bottomLeft' | 'bottomCenter' | 'bottomRight';Always wrap in try-catch:
try {
const result = await ffmpeg('input.mp4')
.output('output.mp4')
.run();
console.log('Success:', result);
} catch (error) {
console.error('Error:', error.message);
console.error('Code:', error.code);
console.error('Suggestion:', error.suggestion);
}Use finally for cleanup:
try {
await ffmpeg('input.mp4')
.output('output.mp4')
.run();
} catch (error) {
console.error(error);
} finally {
// Cleanup temporary files
}Monitor long operations:
await ffmpeg('input.mp4')
.output('output.mp4')
.on('progress', (progress) => {
console.log(`${progress.percent}% - ETA: ${progress.eta}s`);
})
.run();