FFmpeg.wasm, a pure WebAssembly / JavaScript port of FFmpeg

17 篇文章 0 订阅
6 篇文章 0 订阅

Official Website: https://ffmpegwasm.github.io/

FFmpeg is a famous framework/tool when it comes to video/audio processing, now with ffmpeg.wasm you can use FFmpeg right inside your browser without installation or upload your file to the server. Although ffmpeg.wasm is not as fast as FFmpeg, but it might come in handy for certain use cases.

In this post, I would like to share how to install and use ffmpeg.wasm and also technical details behind to support your usage.

Install ffmpeg.wasm

To install and use ffmpeg.wasm, you only need to use npm/yarn:

$ npm install @ffmpeg/ffmpeg
# or
$ yarn add @ffmpeg/ffmpeg

To use ffmpeg.wasm in node environment, you also need to install @ffmpeg/core .

$ npm install @ffmpeg/core
# or
$ yarn add @ffmpeg/core

Or you can use a CDN

<script src='https://unpkg.com/@ffmpeg/ffmpeg@0.9.3/dist/ffmpeg.min.js'></script>

Use ffmpeg.wasm

To use ffmpeg.wasm, only few lines of code are required:

const fs = require('fs');
const { createFFmpeg, fetchFile } = require('@ffmpeg/ffmpeg');
const ffmpeg = createFFmpeg({ log: true });
(async () => {
  await ffmpeg.load();
  ffmpeg.FS('writeFile', 'test.avi', await fetchFile('./test.avi'));
  await ffmpeg.run('-i', 'test.avi', 'test.mp4');
  fs.writeFileSync('./test.mp4', ffmpeg.FS('readFile', 'test.mp4'));
  process.exit(0);
})();

Don’t forget to add --experimental-wasm-threads and --experimental-wasm-bulk-memory when executing the script in node.

Also you can try the live demo session in the official website: https://ffmpegwasm.github.io/#demo

Behind the scene

To fully utilize/understand the power of ffmpeg.wasm (also for most WebAssembly libraries), few technical details you might want to know:

Inside ffmpeg.load()

ffmpeg.load() is a required API to call before you call others. What happened inside this API is:

  • Download ffmpeg-core.js from remote server (which is around 25MB)
  • Instantiate ffmpeg.wasm wasm code

Depending on your network speed and host machine hardware, this operation can take up to minutes to complete.

File System (FS)

When you check the API of ffmpeg.wasm, you may find an API called ffmpeg.FS() to handle File System operations. (In ffmpeg.wasm we use MEMFS / Memory File System) You can imagine this FS just like a hard disk where you can put the input file and pull the output file from ffmpeg.wasm command. It is essential as we want to keep minimal changes to FFmpeg source code and reserve its maxium similarity to original command line interface. Some command operations you might use:

  1. ffmpeg.FS('writeFile', 'filename', data) : write file to MEMFS as input of ffmpeg.wasm
  2. ffmpeg.FS('readFile', 'filename') : read file from MEMFS as output of ffmpeg.wasm
  3. ffmpeg.FS('unlink', 'filename') : delete a file in MEMFS
  4. ffmpeg.FS('readdir', '/') : list files inside specific path

For full list of APIs, you can check: File System API — Emscripten 3.1.45-git (dev) documentation

SharedArrayBuffer

SharedArrayBuffer is a pretty new data type in JavaScript, right now most of the browsers still lack of fully support due to security issues. But in ffmpeg.wasm, to enable pthread / mutli-threading support to speed up, it is a required data type to use. 

Web Workers

When you run ffmpeg.run() , you might find tons of web workers spawned. It is a normal situation as web workers are simulating threads in FFmpeg and it is good as we don’t want ffmpeg.wasm to block our main thread.

Leverage CPU capabilities

For most of libraries work with ffmpeg (ex. x264), they use assembly language like x86 to speed up the process. But sadly, WebAssembly cannot directly use these x86 assembly code as it is not compatible or certain instructions are not supported. Thus it takes time to port x86 assembly to SIMD assembly to make it work.


It is still a long way to go as ffmpeg.wasm is still in an early stage (right now it is only v0.9), although it is still very slow comparing to original version, but with the growth and evolution of WebAssembly, I believe it will become more and more useful. 😃

Feel free to try and comments, issues or/and PRs are highly welcome!

Github: GitHub - ffmpegwasm/ffmpeg.wasm: FFmpeg for browser, powered by WebAssembly

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在项目中安装ffmpeg.wasm和@ffmpeg/ffmpeg,你可以按照以下步骤进行操作: 1. 首先,使用命令`yarn add @ffmpeg/ffmpeg @ffmpeg/core`来将ffmpeg.wasm和@ffmpeg/ffmpeg安装到你的项目中。 2. 接下来,你需要创建一个ffmpeg实例。你可以使用`import FFmpeg from 'ffmpeg/@ffmpeg/dist/ffmpeg.min.js'`来导入ffmpeg的核心文件。然后,使用`FFmpeg.createFFmpeg()`方法来创建一个ffmpeg实例,并传入一些配置参数,例如核心文件的路径和日志选项。 3. 然后,你需要等待ffmpeg加载完成。可以使用`await ffmpeg.load()`来等待加载完成。这个过程可能需要一些时间,所以确保在等待期间不会执行其他操作。 综上所述,你可以按照上述步骤在项目中安装ffmpeg.wasm和@ffmpeg/ffmpeg。希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [ffmpeg.wasm的使用教程](https://blog.csdn.net/qq_41535611/article/details/121907623)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [ffmpeg.wasm:用于Web浏览器和节点的FFmpeg,由WebAssembly支持](https://download.csdn.net/download/weixin_42125867/18577288)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值