UNPKG

tav-media

Version:

Cross platform media editing framework

135 lines (134 loc) 5.79 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { tav } from '../tav'; import { Asset } from './tav-asset'; import { allowCallNativeAnytime } from '../types/tav-object'; import { makeClipAssetPath, parseClipAssetPath } from '../utils/clip-asset-path'; /** * MovieAsset is an asset that can be used to play video file. * @hideconstructor * @category Assets */ export class MovieAsset extends Asset { constructor() { super(...arguments); /** * Returns `'MovieAsset'` */ this.type = 'MovieAsset'; this.clipId = ''; } /** * Creates a TAVMedia Movie object from a specified range of a video file, return null if the file does * not exist or it's not a valid video file. * @param path The path of the video file. * @param options The options of the video file. * * The MovieAsset will skip metadata loading if you * set the width, height, duration at the same time. */ static MakeFromPath(path, options) { return __awaiter(this, void 0, void 0, function* () { const { assetPath } = parseClipAssetPath(path); this.MovieOptions[assetPath] = options; const movieAsset = new MovieAsset(null, path); movieAsset.options = options; yield movieAsset.prepare(); const asset = yield movieAsset.build(); if (!asset) { movieAsset.release(); return null; } return movieAsset; }); } /** * Preload videos * @param urls The urls of the video files you want to preload. */ static preload(urls) { return __awaiter(this, void 0, void 0, function* () { return yield Promise.all(urls.map(url => tav.TAVVideoFrameReader.preload(url))); }); } /** * @ignore */ $getCopyOrSelfWillBeUsedByClip(clip) { return __awaiter(this, void 0, void 0, function* () { if (!this.clipId) { this.clipId = clip.id; return this; } const clipPath = makeClipAssetPath(clip.id, this.path); const assetClone = yield MovieAsset.MakeFromPath(clipPath); yield assetClone.build(); return assetClone; }); } doPrepare() { const _super = Object.create(null, { doPrepare: { get: () => super.doPrepare } }); var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { yield _super.doPrepare.call(this); if (tav.PLATFORM === 'wx' || tav.PLATFORM === 'browser') { try { if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.height) && ((_b = this.options) === null || _b === void 0 ? void 0 : _b.width) && ((_c = this.options) === null || _c === void 0 ? void 0 : _c.duration)) { this.rawVideo = { videoWidth: this.options.width, videoHeight: this.options.height, duration: this.options.duration, }; } const videoTask = tav.TAVVideoFrameReader.preload(this.path); const audioTask = tav.TAVMovieAudioReader.preload(this.path); if (!this.rawVideo) { this.rawVideo = yield videoTask; yield audioTask; } } catch (e) { this.rawVideo = null; console.error('video load failed', this.path); } } }); } createAsset() { return __awaiter(this, void 0, void 0, function* () { if (!this.path) return undefined; if (tav.PLATFORM === 'wx' || tav.PLATFORM === 'browser') { if (!this.rawVideo) return undefined; return yield tav.MovieAsset.MakeFromUrl(this.path, this.rawVideo.videoWidth, this.rawVideo.videoHeight, this.rawVideo.duration * 1000000); } return yield tav.MovieAsset.MakeFromPath(this.path); }); } /** * Returns the frameRate of this asset. */ get frameRate() { var _a; return ((_a = this.nativeAsset) === null || _a === void 0 ? void 0 : _a.frameRate()) || 0; } } MovieAsset.MovieOptions = {}; __decorate([ allowCallNativeAnytime ], MovieAsset.prototype, "frameRate", null);