# 视频编辑器 — 开发者库

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

## 安装

```sh
npm install @openlibsdev/video-editor
```

```js
import VideoEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/video-editor@1.0.0/+esm";
```

## 用法

```js
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.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`

引擎是否已就绪。

## 依赖

- @breezystack/lamejs 1.2.7 (LGPL-3.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @simonwep/pickr 1.10.2 (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)
- gifenc 1.0.3 (MIT)
- imagetracerjs 1.2.6 (Unlicense)
- mediabunny 1.66.0 (MPL-2.0)

- GitHub: https://github.com/OpenLibsDev/video-editor
- npm: https://www.npmjs.com/package/@openlibsdev/video-editor
- 打开应用: https://openlibs.dev/zh-Hans/app/video-editor/
