视频编辑器:JavaScript 库

基于 WebCodecs 的浏览器视频编辑:剪辑、转换为 MP4、WebM 或 MKV、制作 GIF、提取声音、改变速度、尺寸和旋转、滤镜和字幕。文件不会离开电脑,也不下载任何编解码器。

  • @openlibsdev/video-editor
  • v1.0.0
  • ES module
  • MIT

安装

使用打包工具或 Node 风格的导入:

npm install @openlibsdev/video-editor
import VideoEditor from "@openlibsdev/video-editor";

或直接在页面中使用,无需构建(jsDelivr 会解析依赖):

<script type="module">
import VideoEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/video-editor@1.0.0/+esm";
</script>

用法

import VideoEditor from "@openlibsdev/video-editor";

const file = document.querySelector("input[type=file]").files[0];
const engine = VideoEditor.createEngine();
const info = await engine.probe(file);      // { duration, width, height, hasAudio, ... }
const job = VideoEditor.normalize({ op: "trim", start: 1, end: 3 }, info.duration);
const result = await engine.run(file, job, {
  hasAudio: info.hasAudio,
  onProgress: p => console.log(Math.round(p * 100) + "%")
});
console.log(result.name, result.blob.size);   // a new file, e.g. clip_trim.mp4

API

VideoEditor.createEngine()

创建处理引擎;其方法见下文。

VideoEditor.normalize(job, duration)

检查任务并补全默认值:{op, start, end, format, speed, …};时间会限制在视频时长内。

VideoEditor.buildJob(job, info)

将任务转换为计划:解码、修改和编码哪些内容,以及结果的名称和类型。

VideoEditor.OPS

操作:trim、convert、gif、audio、speed、resize、rotate、filter、text。

VideoEditor.DEFAULTS

任务所有字段的默认值。

VideoEditor.FORMATS

输出视频容器。

VideoEditor.AUDIO_FORMATS

输出音频格式。

VideoEditor.FILTERS

图像滤镜:灰度、棕褐色、提亮、对比度、反相、模糊、锐化、暗角。

VideoEditor.VIDEO_CODECS

每种容器按优先顺序尝试的视频编解码器。

VideoEditor.AUDIO_CODECS

每种容器按优先顺序尝试的音频编解码器。

VideoEditor.applyFilter(img, name)

对 ImageData 帧应用滤镜。

VideoEditor.stretchAudio(channels, sampleRate, speed)

改变声音速度而不改变音高(WSOLA)。

VideoEditor.timecode(sec)

秒 → “h:mm:ss.s”。

VideoEditor.parseTimecode(text)

“1:02.5” 或 “62.5” → 秒。

VideoEditor.captionFamilies()

可用于字幕的字体家族。

VideoEditor.sizeText(bytes, units)

以文字表示文件大小(“12.4 MB”)。

VideoEditor.APP

库的名称、版本和发布日期:{name, version, created}。

VideoEditor.GIF_FPS

GIF 可选的帧率。

VideoEditor.GIF_WIDTHS

GIF 可选的宽度(像素)。

VideoEditor.HEIGHTS

调整大小时可选的帧高度(像素,视频不会被放大)。

VideoEditor.MIME

每种输出扩展名的 MIME 类型。

VideoEditor.ROTATIONS

旋转和翻转:cw, ccw, 180, hflip, vflip。

VideoEditor.TEXT_POSITIONS

字幕的位置:顶部、中间、底部。

VideoEditor.baseName(name)

不含扩展名的文件名,文件名中不允许的字符会被替换。

VideoEditor.captionTextFontData

字幕文本和字体区块的数据(来自 font-forge,仅限可用于字幕的字体)。

VideoEditor.extOf(name)

文件名的小写扩展名(没有时为“mp4”)。

VideoEditor.findNamedColor(text, lang)

按指定语言的颜色名称查找命名颜色(“深蓝色”→ #00008b)。

VideoEditor.fontFor(family, bold, italic)

某字体族和样式在浏览器中的字体名,会先加载该字体。

VideoEditor.listNamedColors(lang)

列出命名颜色及其在指定语言中的名称。

VideoEditor.namedColorTexts(lang)

指定语言的颜色字段标签(取色器的文本),缺少该语言时为英文。

VideoEditor.textFontData

文本和字体区块的数据:字体列表、加载字形样式、添加字体文件、某种语言的区块标签以及默认值。

实例:engine = VideoEditor.createEngine()

engine.probe(file, opts)

读取文件:时长、尺寸、编解码器,以及是否有声音和画面。

engine.run(file, job, opts)

按任务处理文件并返回 {blob, name, mime, ext, plan, codecs}。选项:hasAudio、frameHeight、onProgress、onLog、signal。

engine.load()

提前准备引擎(否则由首次调用完成)。

engine.terminate()

停止当前任务。

engine.loaded

引擎是否已就绪。

依赖

库加载的内容及其许可证(全部列在“许可证”页面)。

名称版本许可证文件
@openlibsdev/font-forge1.0.0MITGitHub
@openlibsdev/image-forge1.0.0MITGitHub
@breezystack/lamejs1.2.7LGPL-3.0 许可证 · 源代码
@imagemagick/magick-wasm0.0.35Apache-2.0 许可证 · 源代码
@imagemagick/magick-wasm0.0.35Apache-2.0 许可证 · 源代码
@simonwep/pickr1.10.2MIT 许可证 · 源代码
@simonwep/pickr1.10.2MIT 许可证 · 源代码
dejavu-fonts-ttf2.37.3Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain 许可证 · 源代码
gifenc1.0.3MIT 许可证 · 源代码
imagetracerjs1.2.6Unlicense 许可证 · 源代码
mediabunny1.66.0MPL-2.0 许可证 · 源代码