3.8 KiB
API
createWorker(options): Worker
createWorker is a factory function that creates a ffmpeg worker, a worker is basically a Web Worker in browser and Child Process in Node.
Arguments:
options
an object of customized optionscorePath
path for ffmpeg-core.js scriptworkerPath
path for downloading worker scriptworkerBlobURL
a boolean to define whether to use Blob URL for worker script, default: truelogger
a function to log the progress, a quick example ism => console.log(m)
Examples:
const { createWorker } = FFmpeg;
const worker = createWorker({
corePath: './node_modules/@ffmpeg/core/ffmpeg-core.js',
logger: m => console.log(m),
});
Worker.load(jobId): Promise
Worker.load() loads ffmpeg-core.js script (download from remote if not presented), it makes Web Worker/Child Process ready for next action.
Arguments:
jobId
jobId is generated by ffmpeg.js to identify each job, but you can put your own when calling the function.
Examples:
(async () => {
await worker.load();
})();
Worker.write(path, data, jobId): Promise
Worker.write() writes data to specific path in Emscripten file system, it is an essential step before doing any other tasks.
Arguments:
path
path to write data to file systemdata
data to write, can be Uint8Array, URL or base64 formatjobId
check Worker.load()
Examples:
(async () => {
await worker.write('flame.avi', 'http://localhost:3000/tests/assets/flame.avi');
})();
Worker.read(path, jobId): Promise
Worker.read() reads data from file system, often used to get output data after specific task.
Arguments:
path
path to read data from file systemjobId
check Worker.load()
Examples:
(async () => {
const { data } = await worker.read('output.mp4');
})();
Worker.mkdir(path, jobId): Promise
Worker.mkdir() creates a directory in file system, useful when you need to group files in a directory.
Arguments:
path
path to create directoryjobId
check Worker.load()
Examples:
(async () => {
await worker.mkdir('/video-clips');
})();
Worker.remove(path, jobId): Promise
Worker.remove() removes files in file system, it will be better to delete unused files if you need to run ffmpeg.js multiple times.
Arguments:
path
path for file to deletejobId
check Worker.load()
Examples:
(async () => {
await worker.remove('output.mp4');
})();
Worker.transcode(inputPath, outputPath, options, jobId): Promise
Worker.transcode() transcode a video file to another format.
Arguments:
inputPath
input file path, the input file should be written through Worker.write()outputPath
output file path, can be read with Worker.read() lateroptions
a string to add extra arguments to ffmpegjobId
check Worker.load()
Examples:
(async () => {
await worker.transcode('flame.avi', 'output.mp4', '-s 1920x1080');
})();
Worker.run(args, jobId): Promise
Worker.run() is similar to FFmpeg cli tool, aims to provide maximum flexiblity for users.
Arguments:
args
a string to represent argumentsjobId
check Worker.load()
Examples:
(async () => {
await worker.run('-i flame.avi -s 1920x1080 output.mp4');
})();