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
packages/ffmpeg/src/classes.ts:44
#progressEventCallbacks
• Private
#progressEventCallbacks: ProgressEventCallback
[] = []
Defined in
packages/ffmpeg/src/classes.ts:45
#rejects
• Private
#rejects: Callbacks
= {}
Defined in
packages/ffmpeg/src/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
packages/ffmpeg/src/classes.ts:41
#worker
• Private
#worker: null
| Worker
= null
Defined in
packages/ffmpeg/src/classes.ts:36
loaded
• loaded: boolean
= false
Defined in
packages/ffmpeg/src/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
Name | Type | Default value | Description |
---|---|---|---|
args | string [] | undefined | ffmpeg command line args |
timeout | number | -1 | milliseconds to wait before stopping the command execution. Default Value -1 |
__namedParameters | FFMessageOptions | {} | - |
Returns
Promise
<number
>
0
if no error, != 0
if timeout (1) or error.
Defined in
packages/ffmpeg/src/classes.ts:233
ffprobe
▸ ffprobe(args
, timeout?
, __namedParameters?
): Promise
<number
>
Execute ffprobe command.
Example
const ffmpeg = new FFmpeg();
await ffmpeg.load();
await ffmpeg.writeFile("video.avi", ...);
// Getting duration of a video in seconds: ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.avi -o output.txt
await ffmpeg.ffprobe(["-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", "video.avi", "-o", "output.txt"]);
const data = ffmpeg.readFile("output.txt");
Parameters
Name | Type | Default value | Description |
---|---|---|---|
args | string [] | undefined | ffprobe command line args |
timeout | number | -1 | milliseconds to wait before stopping the command execution. Default Value -1 |
__namedParameters | FFMessageOptions | {} | - |
Returns
Promise
<number
>
0
if no error, != 0
if timeout (1) or error.
Defined in
packages/ffmpeg/src/classes.ts:269
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
Name | Type |
---|---|
__namedParameters | FFMessageLoadConfig |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
true
if ffmpeg core is loaded for the first time.
Defined in
packages/ffmpeg/src/classes.ts:187
off
▸ off(event
, callback
): void
Unlisten to log or progress events from ffmpeg.exec()
.
Parameters
Name | Type |
---|---|
event | "log" |
callback | LogEventCallback |
Returns
void
Defined in
packages/ffmpeg/src/classes.ts:163
▸ off(event
, callback
): void
Parameters
Name | Type |
---|---|
event | "progress" |
callback | ProgressEventCallback |
Returns
void
Defined in
packages/ffmpeg/src/classes.ts:164
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
Name | Type |
---|---|
event | "log" |
callback | LogEventCallback |
Returns
void
Defined in
packages/ffmpeg/src/classes.ts:145
▸ on(event
, callback
): void
Parameters
Name | Type |
---|---|
event | "progress" |
callback | ProgressEventCallback |
Returns
void
Defined in
packages/ffmpeg/src/classes.ts:146
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
packages/ffmpeg/src/classes.ts:295
File System Methods
createDir
▸ createDir(path
, __namedParameters?
): Promise
<boolean
>
Create a directory.
Parameters
Name | Type |
---|---|
path | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
Defined in
packages/ffmpeg/src/classes.ts:440
deleteDir
▸ deleteDir(path
, __namedParameters?
): Promise
<boolean
>
Delete an empty directory.
Parameters
Name | Type |
---|---|
path | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
Defined in
packages/ffmpeg/src/classes.ts:476
deleteFile
▸ deleteFile(path
, __namedParameters?
): Promise
<boolean
>
Delete a file.
Parameters
Name | Type |
---|---|
path | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
Defined in
packages/ffmpeg/src/classes.ts:403
listDir
▸ listDir(path
, __namedParameters?
): Promise
<FSNode
[]>
List directory contents.
Parameters
Name | Type |
---|---|
path | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<FSNode
[]>
Defined in
packages/ffmpeg/src/classes.ts:458
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
Name | Type | Default value | Description |
---|---|---|---|
path | string | undefined | - |
encoding | string | "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 |
__namedParameters | FFMessageOptions | {} | - |
Returns
Promise
<FileData
>
Defined in
packages/ffmpeg/src/classes.ts:377
rename
▸ rename(oldPath
, newPath
, __namedParameters?
): Promise
<boolean
>
Rename a file or directory.
Parameters
Name | Type |
---|---|
oldPath | string |
newPath | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
Defined in
packages/ffmpeg/src/classes.ts:421
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
Name | Type |
---|---|
path | string |
data | FileData |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
Defined in
packages/ffmpeg/src/classes.ts:324
Other Methods
#registerHandlers
▸ Private
#registerHandlers(): void
register worker message event handlers.
Returns
void
Defined in
packages/ffmpeg/src/classes.ts:52
#send
▸ Private
#send(__namedParameters
, trans?
, signal?
): Promise
<CallbackData
>
Generic function to send messages to web worker.
Parameters
Name | Type | Default value |
---|---|---|
__namedParameters | Message | undefined |
trans | Transferable [] | [] |
signal? | AbortSignal | undefined |
Returns
Promise
<CallbackData
>
Defined in
packages/ffmpeg/src/classes.ts:96
mount
▸ mount(fsType
, options
, mountPoint
): Promise
<boolean
>
Parameters
Name | Type |
---|---|
fsType | FFFSType |
options | WorkerFSMountData |
mountPoint | string |
Returns
Promise
<boolean
>
Defined in
packages/ffmpeg/src/classes.ts:343
unmount
▸ unmount(mountPoint
): Promise
<boolean
>
Parameters
Name | Type |
---|---|
mountPoint | string |
Returns
Promise
<boolean
>