UNPKG

framo

Version:

Glorious media processing right in your browser with Typescript support

99 lines 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FfmpegService = void 0; const tslib_1 = require("tslib"); const rxjs_1 = require("rxjs"); const constants_1 = require("../constants/constants"); const scripts_1 = require("../stores/scripts"); const script_loader_service_1 = require("./script-loader.service"); const scripService = new script_loader_service_1.ScriptService(); class FfmpegService extends EventTarget { constructor() { super(...arguments); this.isReady = false; this.progress = new rxjs_1.Subject(); this.ready = new rxjs_1.Subject(); this.initializeFramo = () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield this.initializeFfmpeg(); yield this.initializeMediaInfo(); this.isReady = true; this.emitReadyState(); }); this.initializeFfmpeg = () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield scripService.loadScript(scripts_1.ScriptNames.FFMPEG); this.ffmpeg = exports.FFmpeg.createFFmpeg({ log: true, }); yield this.ffmpeg.load(); this.initializeProgressForwarding(); }); this.initializeMediaInfo = () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield scripService.loadScript(scripts_1.ScriptNames.MEDIAINFO); const opts = { chunkSize: 256 * 1024, coverData: false, format: 'object' }; this.mediainfo = yield new exports.MediaInfo(opts); } catch (error) { throw new Error(`${constants_1.ERROR_MESSAGES.MEDIAINFO_COULD_NOT_LOAD}: ${error}`); } }); this.fetchFile = (filename, file) => tslib_1.__awaiter(this, void 0, void 0, function* () { const fileAsUint8Array = yield exports.FFmpeg.fetchFile(file); yield this.ffmpeg.FS("writeFile", filename, fileAsUint8Array); return new Blob([fileAsUint8Array]); }); this.fetchFiles = (files, filenames) => { const filePromises = []; files.forEach((file, index) => { filePromises.push(this.fetchFile(filenames[index], file)); }); return filePromises; }; this.emitReadyState = () => { this.ready.next(); }; this.getMediaInfo = (fileBlob) => tslib_1.__awaiter(this, void 0, void 0, function* () { const getSize = () => fileBlob.size; const readChunk = (chunkSize, offset) => new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = (event) => { var _a; if ((_a = event.target) === null || _a === void 0 ? void 0 : _a.error) { reject(event.target.error); return; } resolve(new Uint8Array((event.target.result))); }; reader.readAsArrayBuffer(fileBlob.slice(offset, offset + chunkSize)); }); try { const result = yield this.mediainfo .analyzeData(getSize, readChunk); return result; } catch (error) { throw new Error(constants_1.ERROR_MESSAGES.MEDIAINFO_COULD_NOT_ANALYZE_VIDEO); } }); this.getMediaInfos = (fileBlobs) => { const mediaInfos = []; fileBlobs.forEach((blob) => { mediaInfos.push(this.getMediaInfo(blob)); }); return mediaInfos; }; } static getInstance() { if (!FfmpegService.instance) { FfmpegService.instance = new FfmpegService(); } return FfmpegService.instance; } initializeProgressForwarding() { this.ffmpeg.setProgress((progress) => { this.progress.next(progress); }); } } exports.FfmpegService = FfmpegService; //# sourceMappingURL=ffmpeg.service.js.map