# 動画エディター — 開発者向けライブラリ

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/ja/app/video-editor/
