UNPKG

handbrake-ts

Version:

Handbrake with TypeScript and support for electron

163 lines 6.47 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handBrakeTs = exports.HandBrakeProcess = exports.HandBrakeTs = void 0; const rxjs_1 = require("rxjs"); const rxjs_2 = require("rxjs"); const Config_1 = require("./Config"); const Installator_1 = require("./Installator"); const Node_1 = require("./Node"); class HandBrakeTs { constructor() { this.config = Config_1.Config.instance(); this.installator = new Installator_1.Installator(this.config); } exec(args) { return new HandBrakeProcess(Node_1.childProcess, args, this.installator, this.config); } static instance() { return HandBrakeTs.singleton = HandBrakeTs.singleton == null ? new HandBrakeTs() : HandBrakeTs.singleton; } } exports.HandBrakeTs = HandBrakeTs; class HandBrakeProcess { constructor(processInicializer, args, instalator, config) { this.processInicializer = processInicializer; this.args = args; this.instalator = instalator; this.config = config; this.progressRegExp = /(?<percent>[0-9]{1,2}.[0-9]{1,2}) %|ETA (?<h>[0-9]{2})h(?<m>[0-9]{2})m(?<s>[0-9]{2})|avg (?<fps>[0-9]{2,3}.[0-9]{1,2})[ fps]{0,1}/gm; this.logStream = new rxjs_2.Subject(); this.errorStream = new rxjs_2.Subject(); this.stateChange = new rxjs_1.BehaviorSubject({ status: 'pending', hour: 0, min: 0, sec: 0, errorMessage: null, percent: 0 }); this.init(); } init() { if (this.config.autoInstall) { this.instalator.install(); } if (this.config.autoStart) { this.start(); } } isRun() { return !!this.process; } start() { return __awaiter(this, void 0, void 0, function* () { if (!(yield this.instalator.isInstalled())) { this.updateState({ status: 'error', errorMessage: "Hnadbrake not installed" }); return; } if (this.isRun()) return; this.updateState({ status: 'starting' }); const args = Node_1.toSpawnArgs(this.args); this.process = this.processInicializer.spawn(this.config.getCliPath(), args); this.process.stdout.setEncoding('utf-8'); this.process.stderr.setEncoding('utf-8'); this.process.stdout.on('data', (l) => { this.logStream.next(l); this.emitProgress(l); }); this.process.stderr.on('data', (l) => { this.errorStream.next(l); this.captureErrors(l); }); this.process.on('exit', (code, signal) => { if (this.state.status == "caneled") return; if (code == 0) { this.updateState({ status: 'done', percent: 100, min: 0, sec: 0, hour: 0 }); } else { this.updateState({ status: 'error', errorMessage: `Process failed with error code: ${code}` }); } this.process = null; }); this.process.on('error', (err) => { if (this.state.status == 'error') return; this.updateState({ status: 'error', errorMessage: err.message, percent: 0 }); this.process = null; }); }); } captureErrors(line) { if (!line) return; } get state() { return this.stateChange.getValue(); } cancel() { if (!this.process) return; this.process.kill('SIGINT'); this.process = null; this.updateState({ status: 'caneled', percent: 0, }); } emitProgress(l) { var _a; const line = l !== null && l !== void 0 ? l : ""; const match = (_a = this.progressRegExp.exec(line)) === null || _a === void 0 ? void 0 : _a.groups; // console.log(line); // console.log(match); if (!match || !line) return; const state = this.state; const status = state.status == "starting" ? 'running' : state.status; const percent = (match === null || match === void 0 ? void 0 : match.percent) ? Number(match.percent) : state.percent; this.updateState({ min: (match === null || match === void 0 ? void 0 : match.m) ? Number(match.m) : state.min, hour: (match === null || match === void 0 ? void 0 : match.h) ? Number(match.h) : state.hour, sec: (match === null || match === void 0 ? void 0 : match.s) ? Number(match.s) : state.sec, fps: (match === null || match === void 0 ? void 0 : match.fps) ? Number(match.fps) : state.fps, percent: state.percent < percent ? percent : state.percent, status: status }); } updateState(change) { var state = this.stateChange.getValue(); var newState = Object.assign(state, change); this.stateChange.next(newState); } } exports.HandBrakeProcess = HandBrakeProcess; exports.handBrakeTs = HandBrakeTs.instance(); //# sourceMappingURL=HandBrake.js.map