5.4 KiB
API
createFFmpeg(options): ffmpeg
createFFmpeg is a factory function that creates a ffmpeg instance.
Arguments:
optionsan object of customized optionscorePathpath for ffmpeg-core.js scriptloga boolean to turn on all logs, default isfalseloggera function to get log messages, a quick example is({ message }) => console.log(message)progressa function to trace the progress, a quick example isp => console.log(p)
Examples:
const { createFFmpeg } = FFmpeg;
const ffmpeg = createFFmpeg({
corePath: "./node_modules/@ffmpeg/core/ffmpeg-core.js",
log: true,
});
ffmpeg.load(): Promise
ffmpeg.load() loads ffmpeg-core.js script (download from remote if not presented), it makes WebAssembly code ready to use.
Examples:
(async () => {
await ffmpeg.load();
})();
ffmpeg.write(path, data): Promise
ffmpeg.write() writes data to specific path in Emscripten file system, it is an essential step before doing any other tasks.
Currently we found an issue that you should not have parallel Worker.write() as it may cause unexpected behavior, please do it in sequential for-loop like HERE
Arguments:
pathpath to write data to file systemdatadata to write, can be Uint8Array, URL or base64 format
Examples:
(async () => {
await ffmpeg.write(
"flame.avi",
"http://localhost:3000/tests/assets/flame.avi"
);
})();
ffmpeg.writeText(path, text): undefined
ffmpeg.write() writes text data to specific path in Emscripten file system.
Arguments:
pathpath to write data to file systemtextstring to write to file
Examples:
(async () => {
ffmpeg.writeText("sub.srt", "...");
})();
ffmpeg.read(path): Uint8Array
ffmpeg.read() reads data from file system, often used to get output data after specific task.
Arguments:
pathpath to read data from file system
Examples:
(async () => {
const data = ffmpeg.read("output.mp4");
})();
ffmpeg.remove(path): Promise
ffmpeg.remove() removes files in file system, it will be better to delete unused files if you need to run ffmpeg.js multiple times.
Arguments:
pathpath for file to delete
Examples:
(async () => {
ffmpeg.remove("output.mp4");
})();
ffmpeg.ls(path): Promise
ffmpeg.ls() lists all files in specific path.
Arguments:
pathpath to list
Examples:
(async () => {
const dirs = ffmpeg.ls("/");
})();
ffmpeg.transcode(input, output, options): Promise
ffmpeg.transcode() transcode a video file to another format.
Arguments:
inputinput file path, the input file should be written through ffmpeg.write()outputoutput file path, can be read with ffmpeg.read() lateroptionsa string to add extra arguments to ffmpeg
Examples:
(async () => {
await ffmpeg.transcode("flame.avi", "output.mp4", "-s 1920x1080");
})();
ffmpeg.trim(input, output, from, to, options): Promise
ffmpeg.trim() trims video to specific interval.
Arguments:
inputPathinput file path, the input file should be written through ffmpeg.write()outputPathoutput file path, can be read with ffmpeg.read() laterfromstart time, can be in time stamp (00:00:12.000) or seconds (12)toend time, rule same as aboveoptionsa string to add extra arguments to ffmpeg
Examples:
(async () => {
await ffmpeg.trim("flame.avi", "output.mp4", 1, 2);
})();
ffmpeg.concatDemuxer(input, output, options): Promise
ffmpeg.concatDemuxer() concatenates multiple videos using concatDemuxer. This method won't encode the videos again. But it has its limitations. See Concat demuxer Wiki
Arguments:
inputinput file paths as an Array, the input files should be written through ffmpeg.write()outputoutput file path, can be read with ffmpeg.read() lateroptionsa string to add extra arguments to ffmpeg
Examples:
(async () => {
await ffmpeg.concatDemuxer(["flame-1.avi", "flame-2.avi"], "output.mp4");
})();
If the input video files are the same as the output video file, you can pass an extra option to speed up the process
(async () => {
await ffmpeg.concatDemuxer(["flame-1.mp4", "flame-2.mp4"], "output.mp4", "-c copy");
})();
ffmpeg.run(args): Promise
ffmpeg.run() is similar to FFmpeg cli tool, aims to provide maximum flexiblity for users.
Arguments:
argsa string to represent arguments
Examples:
(async () => {
await ffmpeg.run("-i flame.avi -s 1920x1080 output.mp4");
})();