UNPKG

@eleven-am/transcoder

Version:

High-performance HLS transcoding library with hardware acceleration, intelligent client management, and distributed processing support for Node.js

169 lines 6.4 kB
"use strict"; /* * @eleven-am/transcoder * Copyright (C) 2025 Roy OSSAI * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalSegmentProcessor = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const ffmpeg_1 = __importDefault(require("../ffmpeg")); /** * Local segment processor - processes segments on the current machine * This is the default processor that maintains backward compatibility */ class LocalSegmentProcessor { constructor(workerId) { this.disposed = false; this.workerId = workerId || process.env.HOSTNAME || `local-${Date.now()}`; } async processSegment(data) { const startTime = Date.now(); try { // Check if segment already exists if (await this.segmentExists(data.outputPath)) { return { success: true, segmentIndex: data.segmentIndex, outputPath: data.outputPath, cached: true, processingTime: Date.now() - startTime, }; } // Process the segment using FFmpeg await this.runFFmpeg(data); return { success: true, segmentIndex: data.segmentIndex, outputPath: data.outputPath, processingTime: Date.now() - startTime, }; } catch (error) { return { success: false, segmentIndex: data.segmentIndex, outputPath: data.outputPath, processingTime: Date.now() - startTime, error: error, }; } } async isHealthy() { // Local processor is always healthy return !this.disposed; } getMode() { return 'local'; } async dispose() { this.disposed = true; } async runFFmpeg(data) { const { inputOptions, outputOptions, videoFilters } = data.ffmpegOptions; // Ensure output directory exists (async) const outputDir = path.dirname(data.outputPath); await fs.promises.mkdir(outputDir, { recursive: true }); // Create temp file path with worker ID to avoid collisions const tempPath = `${data.outputPath}.${this.workerId}.tmp`; return new Promise((resolve, reject) => { const command = (0, ffmpeg_1.default)(data.sourceFilePath) .inputOptions(inputOptions) .outputOptions(outputOptions) .output(tempPath); if (videoFilters) { command.videoFilters(videoFilters); } command.on('end', async () => { try { // Atomic rename from temp to final path await fs.promises.rename(tempPath, data.outputPath); resolve(); } catch (renameError) { // If rename fails due to file already existing, that's ok if (renameError.code === 'EEXIST') { console.log(`Segment ${data.segmentIndex} already exists (lost race to another worker)`); // Clean up our temp file await fs.promises.unlink(tempPath).catch(() => { }); resolve(); } else { reject(renameError); } } }); command.on('error', async (err) => { // Attempt to clean up temp file on error try { await fs.promises.unlink(tempPath); } catch { // Ignore unlink errors - file may not exist } reject(err); }); command.run(); }); } async segmentExists(filePath) { try { await fs.promises.access(filePath); return true; } catch { return false; } } } exports.LocalSegmentProcessor = LocalSegmentProcessor; //# sourceMappingURL=localSegmentProcessor.js.map