影片編輯器: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 授權 · 原始碼