FFMPEG实践

ffmpeg实践

基础命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// -s 1280x720 指定分辨率
// -fs 15MB 指定文件大小,会截取视频
// -r 20:表示帧率设置为 20fps
// 降低码率 -b:v :指定视频的码率 -b:a : 指定音频的码率
await ffmpeg.run(
'-i',
'cache.mp4',
'-vf',
'scale=1280:-1',
'-b:v',
'0.8M',
'-c:v',
'libx264',
'-r',
'20',
'-preset',
'veryslow',
'-an',
'output.mp4',
);

fluent-ffmpeg

https://www.npmjs.com/package/fluent-ffmpeg

需要提前安装ffmpeg,可以配合二进制ffmpeg包使用

This library abstracts the complex command-line usage of ffmpeg into a fluent, easy to use node.js module. In order to be able to use this module, make sure you have ffmpeg installed on your system (including all necessary encoding libraries like libmp3lame or libx264).

@ffmpeg-installer/ffmpeg

https://www.npmjs.com/package/@ffmpeg-installer/ffmpeg

Platform independent binary installer of FFmpeg for node projects. Useful for tools that should “just work” on multiple environments.

1
2
3
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);

注意Electron平台打包-安装后路径

1
2
3
// Wrong path under Electron with Asar enabled
// It's a known issue that Asar breaks native paths. As a workaround, if you use Asar, you can do something like this:
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path.replace('app.asar', 'app.asar.unpacked');

ffmpeg-static

https://www.npmjs.com/package/ffmpeg-static

ffmpeg static binaries for Mac OSX, Linux, Windows.

1
2
var pathToFfmpeg = require('ffmpeg-static');
console.log(pathToFfmpeg);

注意Electron平台打包-安装后路径

Electron & other cross-platform packaging tools. Because ffmpeg-static will download a binary specific to the OS/platform, you need to purge node_modules before (re-)packaging your app for a different OS/platform (read more in #35).

ffmpeg.wasm

https://www.npmjs.com/package/@ffmpeg/ffmpeg

不建议生产使用。多线程兼容性不好,报内存相关错误。单线程性能较差

ffmpeg.wasm is a pure Webassembly / Javascript port of FFmpeg.(Webassembly)

ffmpeg.js

https://www.npmjs.com/package/ffmpeg.js

不建议生产使用,同样存在性能问题

This library provides FFmpeg builds ported to JavaScript using Emscripten project.(Webassembly)


FFMPEG实践
http://example.com/20230224-ffmpeg实践/
作者
csorz
发布于
2023年2月24日
许可协议