homebridge-nest-cam-ft
Version:
Nest cam plugin for homebridge: https://homebridge.io/
108 lines • 4.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FfmpegProcess = exports.isFfmpegInstalled = exports.getDefaultEncoder = exports.getCodecsOutput = exports.doesFfmpegSupportCodec = void 0;
const execa_1 = __importDefault(require("execa"));
const ffmpeg_for_homebridge_1 = __importDefault(require("ffmpeg-for-homebridge"));
async function doesFfmpegSupportCodec(codec, ffmpegPath) {
if (!codec) {
return false;
}
if (codec === 'copy') {
return true;
}
const output = await (0, execa_1.default)(ffmpegPath, ['-codecs']);
return output.stdout.includes(codec);
}
exports.doesFfmpegSupportCodec = doesFfmpegSupportCodec;
async function getCodecsOutput(ffmpegPath) {
const output = await (0, execa_1.default)(ffmpegPath, ['-codecs']);
return output.stdout;
}
exports.getCodecsOutput = getCodecsOutput;
async function getDefaultEncoder(ffmpegPath) {
const output = await (0, execa_1.default)(ffmpegPath, ['-codecs']);
const validEncoders = ['h264_omx', 'h264_videotoolbox'];
validEncoders.forEach((encoder) => {
if (output.stdout.includes(encoder)) {
return encoder;
}
});
return 'libx264';
}
exports.getDefaultEncoder = getDefaultEncoder;
async function isFfmpegInstalled(ffmpegPath) {
try {
await (0, execa_1.default)(ffmpegPath, ['-codecs']);
return true;
}
catch (_) {
return false;
}
}
exports.isFfmpegInstalled = isFfmpegInstalled;
class FfmpegProcess {
constructor(title, command, log, delegate, sessionId, ffmpegDebugOutput, customFfmpeg, callback) {
var _a;
let started = false;
const controller = delegate.controller;
const cmdOutput = `${title} command: ffmpeg ${command}`;
ffmpegDebugOutput ? log.info(cmdOutput) : log.debug(cmdOutput);
const videoProcessor = customFfmpeg || ffmpeg_for_homebridge_1.default || 'ffmpeg';
let lastOutput = '';
try {
this.ff = (0, execa_1.default)(videoProcessor, command, { env: process.env });
(_a = this.ff.stderr) === null || _a === void 0 ? void 0 : _a.on('data', (data) => {
lastOutput = `${title}: ${String(data)}`;
ffmpegDebugOutput ? log.info(lastOutput) : log.debug(lastOutput);
if (!started && lastOutput.includes('frame=')) {
started = true;
callback && callback();
}
});
this.ff.on('exit', (code) => {
if (code && code !== 0 && callback) {
const lines = lastOutput.split('\n');
let output = '';
if (lines.length > 1) {
output = lines[lines.length - 2];
if (!output.includes('Exiting normally')) {
log.error(`${title}: ${output}`);
}
}
if (!started) {
callback(new Error(output));
}
delegate.stopStream(sessionId);
controller === null || controller === void 0 ? void 0 : controller.forceStopStreamingSession(sessionId);
}
});
}
catch (error) {
log.error(`[${title}] Failed to start stream: ` + error.message);
if (callback) {
callback(new Error('ffmpeg process creation failed!'));
delegate.stopStream(sessionId);
}
}
}
stop() {
var _a, _b, _c;
(_b = (_a = this.ff) === null || _a === void 0 ? void 0 : _a.stdin) === null || _b === void 0 ? void 0 : _b.end();
(_c = this.ff) === null || _c === void 0 ? void 0 : _c.kill('SIGTERM', {
forceKillAfterTimeout: 2000,
});
}
getStdin() {
var _a;
return (_a = this.ff) === null || _a === void 0 ? void 0 : _a.stdin;
}
getStdout() {
var _a;
return (_a = this.ff) === null || _a === void 0 ? void 0 : _a.stdout;
}
}
exports.FfmpegProcess = FfmpegProcess;
//# sourceMappingURL=ffmpeg.js.map