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
#progressEventCallbacks
• Private
#progressEventCallbacks: ProgressEventCallback
[] = []
Defined in
#rejects
• Private
#rejects: Callbacks
= {}
Defined in
#resolves
• Private
#resolves: Callbacks
= {}
#resolves and #rejects tracks Promise resolves and rejects to be called when we receive message from web worker.
Defined in
#worker
• Private
#worker: null
| Worker
= null
Defined in
loaded
• loaded: boolean
= false
Defined in
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
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
off
▸ off(event
, callback
): void
Unlisten to log or prgress events from ffmpeg.exec()
.
Parameters
Name | Type |
---|---|
event | "log" |
callback | LogEventCallback |
Returns
void
Defined in
▸ off(event
, callback
): void
Parameters
Name | Type |
---|---|
event | "progress" |
callback | ProgressEventCallback |
Returns
void
Defined in
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
▸ on(event
, callback
): void
Parameters
Name | Type |
---|---|
event | "progress" |
callback | ProgressEventCallback |
Returns
void
Defined in
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
File System Methods
createDir
▸ createDir(path
, __namedParameters?
): Promise
<boolean
>
Create a directory.
Parameters
Name | Type |
---|---|
path | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
Defined in
deleteDir
▸ deleteDir(path
, __namedParameters?
): Promise
<boolean
>
Delete an empty directory.
Parameters
Name | Type |
---|---|
path | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
Defined in
deleteFile
▸ deleteFile(path
, __namedParameters?
): Promise
<boolean
>
Delete a file.
Parameters
Name | Type |
---|---|
path | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<boolean
>
Defined in
listDir
▸ listDir(path
, __namedParameters?
): Promise
<FSNode
[]>
List directory contents.
Parameters
Name | Type |
---|---|
path | string |
__namedParameters | FFMessageOptions |
Returns
Promise
<FSNode
[]>
Defined in
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
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
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
Other Methods
#registerHandlers
▸ Private
#registerHandlers(): void
register worker message event handlers.
Returns
void
Defined in
#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
mount
▸ mount(fsType
, options
, mountPoint
): Promise
<boolean
>
Parameters
Name | Type |
---|---|
fsType | FFFSType |
options | WorkerFSMountData |
mountPoint | string |
Returns
Promise
<boolean
>
Defined in
unmount
▸ unmount(mountPoint
): Promise
<boolean
>
Parameters
Name | Type |
---|---|
mountPoint | string |
Returns
Promise
<boolean
>