@eleven-am/transcoder
Version:
High-performance HLS transcoding library with hardware acceleration, intelligent client management, and distributed processing support for Node.js
130 lines • 5.28 kB
JavaScript
;
/*
* @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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaSource = void 0;
const path = __importStar(require("path"));
const fp_1 = require("@eleven-am/fp");
class MediaSource {
constructor(filePath, storage) {
this.filePath = filePath;
this.storage = storage;
}
/**
* Retrieves the path of the media file
*/
getFileId() {
return this.storage.generateFileId(this.filePath);
}
/**
* Deletes the temporary files associated with the media source generated during transcoding
*/
deleteTempFiles() {
return this.getFileId()
.chain((fileId) => this.storage.deleteFilesWithPrefix(this.storage.getBasePath(fileId)));
}
/**
* Retrieves the absolute path of the media file
*/
getFilePath() {
return this.filePath;
}
/**
* Checks if a segment exists in the storage
* @param streamType The type of stream (video/audio)
* @param streamIndex The index of the stream
* @param quality The quality of the segment
* @param segmentNumber The number of the segment
*/
segmentExist(streamType, streamIndex, quality, segmentNumber) {
return this.getSegmentPath(streamType, streamIndex, quality, segmentNumber)
.chain((segmentPath) => this.storage.exists(segmentPath));
}
/**
* Retrieves the directory path for a specific stream type, index and quality
* @param streamType The type of stream (video/audio)
* @param streamIndex The index of the stream
* @param quality The quality of the stream
*/
getStreamDirectory(streamType, streamIndex, quality) {
return this.getFileId()
.map((fileId) => path.join(this.storage.getBasePath(fileId), `${streamType}${streamIndex}`, quality))
.chain((path) => this.storage.ensureDirectoryExists(path));
}
/**
* Retrieves the input stream of a segment
* @param streamType The type of stream (video/audio)
* @param streamIndex The index of the stream
* @param quality The quality of the segment
* @param segmentNumber The number of the segment
*/
getSegmentStream(streamType, streamIndex, quality, segmentNumber) {
return this.getSegmentPath(streamType, streamIndex, quality, segmentNumber)
.chain((segmentPath) => fp_1.TaskEither.fromBind({
stream: this.storage.getFileStream(segmentPath),
size: this.storage.getFileSize(segmentPath),
}));
}
/**
* Retrieves the path of a segment in the storage
* @param streamType The type of stream (video/audio)
* @param streamIndex The index of the stream
* @param quality The quality of the segment
* @param segmentNumber The number of the segment
*/
getSegmentPath(streamType, streamIndex, quality, segmentNumber) {
return this.getFileId()
.map((fileId) => path.join(this.storage.getBasePath(fileId), `${streamType}${streamIndex}`, quality, `segment-${segmentNumber}.ts`))
.map((segmentPath) => path.normalize(segmentPath));
}
}
exports.MediaSource = MediaSource;
//# sourceMappingURL=mediaSource.js.map