UNPKG

tav-media

Version:

Cross platform media editing framework

181 lines (180 loc) 6.97 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()); }); }; var PAGFile_1; import { PAGModule } from './pag-module'; import { PAGComposition } from './pag-composition'; import { readFile } from './utils/common'; import { wasmAwaitRewind, wasmAsyncMethod, destroyVerify } from './utils/decorators'; import { getLayerTypeName, layer2typeLayer, proxyVector } from './utils/type-utils'; let PAGFile = PAGFile_1 = class PAGFile extends PAGComposition { constructor() { super(...arguments); this._duration = 0; } /** * Load pag file from file. */ static load(data) { return __awaiter(this, void 0, void 0, function* () { let buffer = null; if (data instanceof File) { buffer = (yield readFile(data)); } else if (data instanceof Blob) { buffer = (yield readFile(new File([data], ''))); } else if (data instanceof ArrayBuffer) { buffer = data; } if (!buffer) throw new Error('Initialize PAGFile data type error, please put check data type must to be File | Blob | ArrayBuffer!'); return PAGFile_1.loadFromBuffer(buffer); }); } /** * Load pag file from arrayBuffer */ static loadFromBuffer(buffer) { if (!buffer || !(buffer.byteLength > 0)) throw new Error('Initialize PAGFile data not be empty!'); const dataUint8Array = new Uint8Array(buffer); const numBytes = dataUint8Array.byteLength; const dataPtr = PAGModule._malloc(numBytes); const dataOnHeap = new Uint8Array(PAGModule.HEAPU8.buffer, dataPtr, numBytes); dataOnHeap.set(dataUint8Array); const wasmIns = PAGModule._PAGFile._Load(dataOnHeap.byteOffset, dataOnHeap.length); if (!wasmIns) throw new Error('Load PAGFile fail!'); const pagFile = new PAGFile_1(wasmIns); PAGModule._free(dataPtr); pagFile._duration = pagFile.wasmIns._duration(); return pagFile; } /** * The maximum tag level current SDK supports. */ static maxSupportedTagLevel() { return PAGModule._PAGFile._MaxSupportedTagLevel(); } /** * The tag level this pag file requires. */ tagLevel() { return this.wasmIns._tagLevel(); } /** * The number of replaceable texts. */ numTexts() { return this.wasmIns._numTexts(); } /** * The number of replaceable images. */ numImages() { return this.wasmIns._numImages(); } /** * The number of video compositions. */ numVideos() { return this.wasmIns._numVideos(); } /** * Get a text data of the specified index. The index ranges from 0 to numTexts - 1. * Note: It always returns the default text data. */ getTextData(editableTextIndex) { return this.wasmIns._getTextData(editableTextIndex); } /** * Replace the text data of the specified index. The index ranges from 0 to PAGFile.numTexts - 1. * Passing in null for the textData parameter will reset it to default text data. */ replaceText(editableTextIndex, textData) { this.wasmIns._replaceText(editableTextIndex, textData); } /** * Replace the image content of the specified index with a PAGImage object. The index ranges from * 0 to PAGFile.numImages - 1. Passing in null for the image parameter will reset it to default * image content. */ replaceImage(editableImageIndex, pagImage) { this.wasmIns._replaceImage(editableImageIndex, pagImage.wasmIns); } /** * Return an array of layers by specified editable index and layer type. */ getLayersByEditableIndex(editableIndex, layerType) { const wasmIns = this.wasmIns._getLayersByEditableIndex(editableIndex, layerType); if (!wasmIns) throw new Error(`Get ${getLayerTypeName(layerType)} layers by ${editableIndex} fail!`); return proxyVector(wasmIns, layer2typeLayer); } /** * Returns the indices of the editable layers in this PAGFile. * If the editableIndex of a PAGLayer is not present in the returned indices, the PAGLayer should * not be treated as editable. */ getEditableIndices(layerType) { return this.wasmIns._getEditableIndices(layerType); } /** * Indicate how to stretch the original duration to fit target duration when file's duration is * changed. The default value is PAGTimeStretchMode::Repeat. */ timeStretchMode() { return this.wasmIns._timeStretchMode(); } /** * Set the timeStretchMode of this file. */ setTimeStretchMode(value) { this.wasmIns._setTimeStretchMode(value); } /** * The duration of the layer in microseconds, indicates the length of the visible range. */ duration() { return this._duration; } /** * Set the duration of this PAGFile. Passing a value less than or equal to 0 resets the duration * to its default value. */ setDuration(duration) { this._duration = duration; this.wasmIns._setDuration(duration); } /** * Set the duration of this PAGFile. Passing a value less than or equal to 0 resets the duration * to its default value. */ copyOriginal() { const wasmIns = this.wasmIns._copyOriginal(); if (!wasmIns) throw new Error(`Copy original fail!`); return new PAGFile_1(wasmIns); } }; __decorate([ wasmAsyncMethod ], PAGFile, "load", null); PAGFile = PAGFile_1 = __decorate([ destroyVerify, wasmAwaitRewind ], PAGFile); export { PAGFile };