UNPKG

@omnimedia/omnitool

Version:

open source video processing tools

69 lines 1.95 kB
import { ms } from "../../../units/ms.js"; import { fps } from "../../../units/fps.js"; import { Playback } from "./parts/playback.js"; export class VideoPlayer { driver; canvas; playback; audio; #pendingSeek = null; #flushTask = null; constructor(driver, resolveMedia, timeline) { this.driver = driver; this.playback = new Playback(driver, timeline, resolveMedia); this.audio = { levels: this.playback.audioLevels }; this.canvas = driver.compositor.pixi.renderer.canvas; } async play() { await this.playback.start(); } pause() { this.playback.pause(); } seek(timeMs) { this.#pendingSeek = timeMs; return this.#flushTask ??= this.#flushSeeks().finally(() => this.#flushTask = null); } setFPS(value) { this.playback.setFps(fps(value)); } resize(width, height) { this.driver.resize(width, height); } get isSeeking() { return this.#flushTask !== null; } get isPlaying() { return this.playback.isPlaying; } get duration() { return this.playback.duration; } get currentTime() { return this.playback.currentTime; } get playbackRate() { return this.playback.playbackRate; } set playbackRate(rate) { this.playback.playbackRate = rate; } /** call this whenever your timeline state changes */ async update(timeline) { this.playback.update(timeline); if (!this.isPlaying) await this.seek(this.currentTime); } async #flushSeeks() { while (this.#pendingSeek !== null) { const next = this.#pendingSeek; this.#pendingSeek = null; const layers = await this.playback.seek(ms(next)); const frame = await this.driver.composite(layers); frame.close(); } } } //# sourceMappingURL=player.js.map