After the release of v0.60 the path in worker.run from /data/filename to filename https://github.com/ffmpegjs/ffmpeg.js/blob/master/src/createWorker.js#L72
5.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:
optionsan object of customized optionscorePathpath for ffmpeg-core.js scriptworkerPathpath for downloading worker scriptworkerBlobURLa boolean to define whether to use Blob URL for worker script, default: trueloggera function to log the progress, a quick example ism => console.log(m)progressa function to trace the progress, a quick example isp => console.log(p)
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:
jobIdjobId 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.
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 formatjobId@see Worker.load()
Examples:
(async () => {
await worker.write(
"flame.avi",
"http://localhost:3000/tests/assets/flame.avi"
);
})();
Worker.writeText(path, text, jobId): Promise
Worker.write() writes text data to specific path in Emscripten file system.
Arguments:
pathpath to write data to file systemtextstring to write to filejobId@see Worker.load()
Examples:
(async () => {
await worker.write("sub.srt", "...");
})();
Worker.read(path, jobId): Promise
Worker.read() reads data from file system, often used to get output data after specific task.
Arguments:
pathpath to read data from file systemjobId@see Worker.load()
Examples:
(async () => {
const { data } = await worker.read("output.mp4");
})();
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:
pathpath for file to deletejobId@see Worker.load()
Examples:
(async () => {
await worker.remove("output.mp4");
})();
Worker.transcode(input, output, options, jobId): Promise
Worker.transcode() transcode a video file to another format.
Arguments:
inputinput file path, the input file should be written through Worker.write()outputoutput file path, can be read with Worker.read() lateroptionsa string to add extra arguments to ffmpegjobId@see Worker.load()
Examples:
(async () => {
await worker.transcode("flame.avi", "output.mp4", "-s 1920x1080");
})();
Worker.trim(input, output, from, to, options, jobId): Promise
Worker.trim() trims video to specific interval.
Arguments:
inputPathinput file path, the input file should be written through Worker.write()outputPathoutput file path, can be read with Worker.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 ffmpegjobId@see Worker.load()
Examples:
(async () => {
await worker.trim("flame.avi", "output.mp4", 1, 2);
})();
Worker.concatDemuxer(input, output, options, jobId): Promise
Worker.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 Worker.write()outputoutput file path, can be read with Worker.read() lateroptionsa string to add extra arguments to ffmpegjobIdcheck Worker.load()
Examples:
(async () => {
await worker.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 worker.concatDemuxer(["flame-1.mp4", "flame-2.mp4"], "output.mp4", "-c copy");
})();
Worker.run(args, jobId): Promise
Worker.run() is similar to FFmpeg cli tool, aims to provide maximum flexiblity for users.
Arguments:
argsa string to represent argumentsjobIdcheck Worker.load()
Examples:
(async () => {
await worker.run("-i flame.avi -s 1920x1080 output.mp4");
})();