ffmpeg.wasm
repository·main·Indexed 12 days ago
https://github.com/ffmpegwasm/ffmpeg.wasmA WebAssembly and JavaScript port of FFmpeg that enables client-side video and audio recording, conversion, and streaming directly in the browser. Version 0.12.0 introduces a TypeScript rewrite, monorepo structure, and official support for Web Workers and multi-threading.
What's inside ffmpeg.wasm
- ffmpeg.wasm is a pure WebAssembly and JavaScript port of FFmpeg. It allows developers to perform video and audio recording, conversion, and streaming directly within a web browser without requiring a backend server for media processing.
Handle TypeScript type support for .vue files
mainBecause standardtsccannot process.vuefiles, this project usesvue-tscfor type checking. This ensures that type information from Vue components is correctly validated during the build process.Understand ffmpeg.wasm performance expectations
mainffmpeg.wasm uses transpiled FFmpeg C source code compiled to WebAssembly. Users should expect lower performance compared to native FFmpeg, as the WebAssembly implementation is not yet fully optimized. Even when using the multi-threaded version (core-mt), performance will be significantly slower than native execution. Use this knowledge to decide if ffmpeg.wasm is appropriate for your specific use case (e.g., background processing vs. real-time requirements).Typical ffmpeg.wasm Workflow
mainWhen using ffmpeg.wasm in your own applications, you should follow this standard lifecycle:
- Load Assets: Download and initialize the required JavaScript and WebAssembly assets. You can choose between the standard version or the multithreading version (
@ffmpeg/core-mt). - Prepare the File System: Upload your source files (media or text) into the ffmpeg.wasm in-memory File System. Files must be present in this virtual environment before the ffmpeg APIs can process them.
- Execute Commands: Pass the desired FFmpeg arguments to the API to process the files in the virtual file system.
- Retrieve Output: Access the resulting files from the in-memory file system for download or further processing.
- Load Assets: Download and initialize the required JavaScript and WebAssembly assets. You can choose between the standard version or the multithreading version (
Use WORKERFS for file system operations
mainThe
WORKERFSfeature allows for specialized file system handling within the worker environment.Requirements:
@ffmpeg/ffmpeg@0.12.10or higher@ffmpeg/core@0.12.4or higher
How ffmpeg.wasm architecture works
mainffmpeg.wasm offloads resource-intensive multimedia transcoding tasks to a web worker (
ffmpeg.worker) by default to avoid blocking the browser's main thread.Key Workflow:
- Worker Initialization: The
ffmpeg.workerdownloads the WebAssemblyffmpeg-corefrom a CDN and initializes it within theWorkerGlobalScope. - File System Management: To process a video, you must first populate the
ffmpeg-coreFile System with your input files. After processing, you must read the resulting files from theffmpeg-coreFile System. - Asynchronous API: Because tasks run in a worker, almost all function calls in ffmpeg.wasm are asynchronous. It is highly recommended to use
async/awaitsyntax. - Threading:
- Using the single-thread core (
@ffmpeg/core) runs in one worker. - Using the multi-thread core (
@ffmpeg/core-mt) causesffmpeg-coreto spawn additional web workers inside the primaryffmpeg.worker.
- Using the single-thread core (
- Worker Initialization: The
Understanding ffmpeg.wasm cores
mainIn ffmpeg.wasm, the
coreacts as the engine and is a swappable component. You can choose between different core implementations depending on your performance and complexity needs:@ffmpeg/core: The single-thread version of the ffmpeg.wasm core.@ffmpeg/core-mt: The multi-thread version of the ffmpeg.wasm core.
Developers can also build custom cores (for example, a core containing only the
x264library to minimize the.wasmfile size) using the build scripts provided in the repository.Migrate from 0.11.x to 0.12+ API
mainVersion 0.12+ of
@ffmpeg/ffmpegis not backward compatible with 0.11.x. When upgrading, you must update your imports, instantiation logic, and method calls. Key changes include moving from a factory function (createFFmpeg) to a class constructor (new FFmpeg), renaming execution and file system methods, and switching from setter methods for logging and progress to an event-based listener pattern (ffmpeg.on).// 0.11.x import { createFFmpeg } from '@ffmpeg/ffmpeg'; const ffmpeg = createFFmpeg(); await ffmpeg.load(); await ffmpeg.run('-i', 'input.mp4'); await ffmpeg.FS.writeFile('test.txt', data); await ffmpeg.FS.readFile('test.txt'); ffmpeg.setLogger(({ message }) => console.log(message)); ffmpeg.setProgress(({ progress }) => console.log(progress)); import { fetchFile } from '@ffmpeg/ffmpeg'; // 0.12+ import { FFmpeg } from '@ffmpeg/ffmpeg'; import { fetchFile } from '@ffmpeg/util'; const ffmpeg = new FFmpeg(); // Note: arguments previously passed to createFFmpeg() are now passed to ffmpeg.load() await ffmpeg.load(); await ffmpeg.exec(['-i', 'input.mp4']); await ffmpeg.writeFile('test.txt', data); await ffmpeg.readFile('test.txt'); ffmpeg.on("log", ({ message }) => console.log(message)); ffmpeg.on("progress", ({ progress }) => console.log(progress));Build the AngularApp project
mainTo create a production build of the application, runng build. The resulting build artifacts will be located in thedist/directory.ng buildBuild a SolidStart application with presets
mainSolid apps use presets to optimize projects for specific deployment environments.
- By default,
npm run buildgenerates a Node.js application that can be executed withnpm start. - To use a different deployment target, add the corresponding preset to your
devDependenciesinpackage.jsonand configure it withinapp.config.js.
npm run build # After building a default Node app, run it with: npm start- By default,
Install the ffmpeg.wasm website
mainTo set up the website locally, first ensure you have
git-lfsinstalled to handle large files. If you have already cloned the repository withoutgit-lfs, rungit lfs pullto download the necessary assets. Then, install the dependencies using npm.# If git-lfs was not installed during clone git lfs pull # Install dependencies npm installPublish @ffmpeg/util to npm
mainTo publish the
@ffmpeg/utilpackage to the npm registry, navigate to the package directory and run the standard publish command.cd packages/util npm publish