# 影片編輯器 — 開發人員程式庫

基於 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-Hant/app/video-editor/
