FFmpeg API Documentation
JavaScript API
This page documents the FFmpeg WebAssembly API used by this project. The integration uses the FFmpeg class from /vendor/ffmpeg/index.js.
Setup
import { FFmpeg } from '/vendor/ffmpeg/index.js';
import { fetchFile } from '/vendor/util/index.js';
const ffmpeg = new FFmpeg();
await ffmpeg.load({
coreURL: '/ffmpeg/core/esm/ffmpeg-core.js',
wasmURL: '/ffmpeg/core/esm/ffmpeg-core.wasm'
});
Core Methods
new FFmpeg()
Creates a new FFmpeg WebAssembly client instance.
await ffmpeg.load(config?)
Loads FFmpeg core assets and initializes the WASM runtime. Returns a boolean indicating first load.
ffmpeg.on('log' | 'progress', callback)
Subscribes to stderr/stdout log events and progress updates during execution.
ffmpeg.off('log' | 'progress', callback)
Unsubscribes an event callback previously attached with on().
await ffmpeg.exec(args, timeout?)
Runs an FFmpeg command array. Returns process exit code (0 means success).
ffmpeg.terminate()
Terminates the worker and clears loaded runtime state. load() must be called again afterward.
File System Methods
await ffmpeg.writeFile(path, data)
Writes input data into FFmpeg's virtual filesystem.
await ffmpeg.readFile(path, encoding?)
Reads output data from FFmpeg's virtual filesystem as bytes or text.
await ffmpeg.deleteFile(path)
Removes a file from FFmpeg's virtual filesystem.
await ffmpeg.rename(oldPath, newPath)
Renames a file or directory.
await ffmpeg.createDir(path)
Creates a directory.
await ffmpeg.listDir(path)
Lists directory entries with basic metadata.
await ffmpeg.deleteDir(path)
Deletes an empty directory.
Runtime Properties
ffmpeg.loaded; // boolean
// Local core configuration used by this project:
{
coreURL: '/ffmpeg/core/esm/ffmpeg-core.js',
wasmURL: '/ffmpeg/core/esm/ffmpeg-core.wasm'
}
Example Usage
import { FFmpeg } from '/vendor/ffmpeg/index.js';
import { fetchFile } from '/vendor/util/index.js';
const ffmpeg = new FFmpeg();
ffmpeg.on('log', ({ type, message }) => {
if (type === 'stderr') console.debug(message);
});
await ffmpeg.load({
coreURL: '/ffmpeg/core/esm/ffmpeg-core.js',
wasmURL: '/ffmpeg/core/esm/ffmpeg-core.wasm'
});
await ffmpeg.writeFile('input.flac', await fetchFile(file));
const exitCode = await ffmpeg.exec([
'-i', 'input.flac',
'-map', '0:a:0',
'-c:a', 'aac',
'-b:a', '192k',
'output.m4a'
]);
if (exitCode !== 0) {
throw new Error(`FFmpeg exited with code ${exitCode}`);
}
const output = await ffmpeg.readFile('output.m4a');
await ffmpeg.deleteFile('input.flac');
await ffmpeg.deleteFile('output.m4a');
Explore Zero Audio
Dive into the AI Enhancer, see what’s included on the Features page, or browse answers in the FAQ. Need help? Visit Support.
References: Web Audio API, WebAssembly