# Video editor — library for developers

Video editing in the browser on WebCodecs: trim, convert to MP4, WebM or MKV, make a GIF, extract the sound, change speed, size, rotation, apply filters and add captions. The file never leaves the computer and no codec is downloaded.

## Installation

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

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

## Usage

```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()`

Creates the processing engine; see its methods below.

### `VideoEditor.normalize(job, duration)`

Checks a job and fills in defaults: {op, start, end, format, speed, …}; the times are clamped to the video length.

### `VideoEditor.buildJob(job, info)`

Turns a job into a plan: what is decoded, changed and encoded, and the name and type of the result.

### `VideoEditor.OPS`

The operations: trim, convert, gif, audio, speed, resize, rotate, filter, text.

### `VideoEditor.DEFAULTS`

Default values of every job field.

### `VideoEditor.FORMATS`

Output video containers.

### `VideoEditor.AUDIO_FORMATS`

Output sound formats.

### `VideoEditor.FILTERS`

Picture filters: grayscale, sepia, brighter, contrast, invert, blur, sharpen, vignette.

### `VideoEditor.VIDEO_CODECS`

Video codecs tried in order of preference for each container.

### `VideoEditor.AUDIO_CODECS`

Audio codecs tried in order of preference for each container.

### `VideoEditor.applyFilter(img, name)`

Applies a filter to an ImageData frame.

### `VideoEditor.stretchAudio(channels, sampleRate, speed)`

Changes the speed of sound without changing its pitch (WSOLA).

### `VideoEditor.timecode(sec)`

Seconds → "h:mm:ss.s".

### `VideoEditor.parseTimecode(text)`

"1:02.5" or "62.5" → seconds.

### `VideoEditor.captionFamilies()`

Font families available for captions.

### `VideoEditor.sizeText(bytes, units)`

File size as text ("12.4 MB").

### `VideoEditor.APP`

Name, version and release date of the library: {name, version, created}.

### `VideoEditor.GIF_FPS`

Frame rates offered for a GIF.

### `VideoEditor.GIF_WIDTHS`

Widths offered for a GIF, in pixels.

### `VideoEditor.HEIGHTS`

Frame heights offered for resizing, in pixels (the video is never enlarged).

### `VideoEditor.MIME`

MIME type of each output extension.

### `VideoEditor.ROTATIONS`

Rotations and flips: cw, ccw, 180, hflip, vflip.

### `VideoEditor.TEXT_POSITIONS`

Positions of a caption: top, center, bottom.

### `VideoEditor.baseName(name)`

File name without its extension, with characters not allowed in file names replaced.

### `VideoEditor.captionTextFontData`

Data for the caption's text and font block (font-forge's, limited to fonts usable for captions).

### `VideoEditor.extOf(name)`

Extension of a file name in lower case ("mp4" when there is none).

### `VideoEditor.findNamedColor(text, lang)`

Finds a named color by its name in the given language ("dark blue" → #00008b).

### `VideoEditor.fontFor(family, bold, italic)`

Browser font name of a family and style, loading it first.

### `VideoEditor.listNamedColors(lang)`

Lists the named colors with their names in the given language.

### `VideoEditor.namedColorTexts(lang)`

Labels of the color field in the given language (the texts of the color picker), English for a missing language.

### `VideoEditor.textFontData`

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

### `engine.probe(file, opts)`

Reads a file: duration, size, codecs and whether it has sound and picture.

### `engine.run(file, job, opts)`

Processes a file by a job and returns {blob, name, mime, ext, plan, codecs}. Options: hasAudio, frameHeight, onProgress, onLog, signal.

### `engine.load()`

Prepares the engine in advance (otherwise the first call does it).

### `engine.terminate()`

Stops the current job.

### `engine.loaded`

Whether the engine is ready.

## Dependencies

- @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
- Open the app: https://openlibs.dev/en/app/video-editor/
