Skip to main content

Class: FFmpeg

Provides APIs to interact with ffmpeg web worker.

Example

const ffmpeg = new FFmpeg();

Constructors

constructor

new FFmpeg()

Properties

#logEventCallbacks

Private #logEventCallbacks: LogEventCallback[] = []

Defined in

classes.ts:44


#progressEventCallbacks

Private #progressEventCallbacks: ProgressEventCallback[] = []

Defined in

classes.ts:45


#rejects

Private #rejects: Callbacks = {}

Defined in

classes.ts:42


#resolves

Private #resolves: Callbacks = {}

#resolves and #rejects tracks Promise resolves and rejects to be called when we receive message from web worker.

Defined in

classes.ts:41


#worker

Private #worker: null | Worker = null

Defined in

classes.ts:36


loaded

loaded: boolean = false

Defined in

classes.ts:47

FFmpeg Methods

exec

exec(args, timeout?, __namedParameters?): Promise<number>

Execute ffmpeg command.

Remarks

To avoid common I/O issues, ["-nostdin", "-y"] are prepended to the args by default.

Example

const ffmpeg = new FFmpeg();
await ffmpeg.load();
await ffmpeg.writeFile("video.avi", ...);
// ffmpeg -i video.avi video.mp4
await ffmpeg.exec(["-i", "video.avi", "video.mp4"]);
const data = ffmpeg.readFile("video.mp4");

Parameters

NameTypeDefault valueDescription
argsstring[]undefinedffmpeg command line args
timeoutnumber-1milliseconds to wait before stopping the command execution. Default Value -1
__namedParametersFFMessageOptions{}-

Returns

Promise<number>

0 if no error, != 0 if timeout (1) or error.

Defined in

classes.ts:232


load

load(__namedParameters?, __namedParameters?): Promise<boolean>

Loads ffmpeg-core inside web worker. It is required to call this method first as it initializes WebAssembly and other essential variables.

Parameters

NameType
__namedParametersFFMessageLoadConfig
__namedParametersFFMessageOptions

Returns

Promise<boolean>

true if ffmpeg core is loaded for the first time.

Defined in

classes.ts:186


off

off(event, callback): void

Unlisten to log or prgress events from ffmpeg.exec().

Parameters

NameType
event"log"
callbackLogEventCallback

Returns

void

Defined in

classes.ts:162

off(event, callback): void

Parameters

NameType
event"progress"
callbackProgressEventCallback

Returns

void

Defined in

classes.ts:163


on

on(event, callback): void

Listen to log or prgress events from ffmpeg.exec().

Example

ffmpeg.on("log", ({ type, message }) => {
// ...
})

Example

ffmpeg.on("progress", ({ progress, time }) => {
// ...
})

Remarks

  • log includes output to stdout and stderr.
  • The progress events are accurate only when the length of input and output video/audio file are the same.

Parameters

NameType
event"log"
callbackLogEventCallback

Returns

void

Defined in

classes.ts:144

on(event, callback): void

Parameters

NameType
event"progress"
callbackProgressEventCallback

Returns

void

Defined in

classes.ts:145


terminate

terminate(): void

Terminate all ongoing API calls and terminate web worker. FFmpeg.load() must be called again before calling any other APIs.

Returns

void

Defined in

classes.ts:258


File System Methods

createDir

createDir(path, __namedParameters?): Promise<boolean>

Create a directory.

Parameters

NameType
pathstring
__namedParametersFFMessageOptions

Returns

Promise<boolean>

Defined in

classes.ts:403


deleteDir

deleteDir(path, __namedParameters?): Promise<boolean>

Delete an empty directory.

Parameters

NameType
pathstring
__namedParametersFFMessageOptions

Returns

Promise<boolean>

Defined in

classes.ts:439


deleteFile

deleteFile(path, __namedParameters?): Promise<boolean>

Delete a file.

Parameters

NameType
pathstring
__namedParametersFFMessageOptions

Returns

Promise<boolean>

Defined in

classes.ts:366


listDir

listDir(path, __namedParameters?): Promise<FSNode[]>

List directory contents.

Parameters

NameType
pathstring
__namedParametersFFMessageOptions

Returns

Promise<FSNode[]>

Defined in

classes.ts:421


readFile

readFile(path, encoding?, __namedParameters?): Promise<FileData>

Read data from ffmpeg.wasm.

Example

const ffmpeg = new FFmpeg();
await ffmpeg.load();
const data = await ffmpeg.readFile("video.mp4");

Parameters

NameTypeDefault valueDescription
pathstringundefined-
encodingstring"binary"File content encoding, supports two encodings: - utf8: read file as text file, return data in string type. - binary: read file as binary file, return data in Uint8Array type. Default Value binary
__namedParametersFFMessageOptions{}-

Returns

Promise<FileData>

Defined in

classes.ts:340


rename

rename(oldPath, newPath, __namedParameters?): Promise<boolean>

Rename a file or directory.

Parameters

NameType
oldPathstring
newPathstring
__namedParametersFFMessageOptions

Returns

Promise<boolean>

Defined in

classes.ts:384


writeFile

writeFile(path, data, __namedParameters?): Promise<boolean>

Write data to ffmpeg.wasm.

Example

const ffmpeg = new FFmpeg();
await ffmpeg.load();
await ffmpeg.writeFile("video.avi", await fetchFile("../video.avi"));
await ffmpeg.writeFile("text.txt", "hello world");

Parameters

NameType
pathstring
dataFileData
__namedParametersFFMessageOptions

Returns

Promise<boolean>

Defined in

classes.ts:287


Other Methods

#registerHandlers

Private #registerHandlers(): void

register worker message event handlers.

Returns

void

Defined in

classes.ts:52


#send

Private #send(__namedParameters, trans?, signal?): Promise<CallbackData>

Generic function to send messages to web worker.

Parameters

NameTypeDefault value
__namedParametersMessageundefined
transTransferable[][]
signal?AbortSignalundefined

Returns

Promise<CallbackData>

Defined in

classes.ts:95


mount

mount(fsType, options, mountPoint): Promise<boolean>

Parameters

NameType
fsTypeFFFSType
optionsWorkerFSMountData
mountPointstring

Returns

Promise<boolean>

Defined in

classes.ts:306


unmount

unmount(mountPoint): Promise<boolean>

Parameters

NameType
mountPointstring

Returns

Promise<boolean>

Defined in

classes.ts:317