Refactor Target and Writer, add single-file HLS writing mode

This commit is contained in:
Vanilagy
2026-03-27 18:07:26 +01:00
parent b8044cfa07
commit e2e8aaab78
15 changed files with 819 additions and 540 deletions
+17
View File
@@ -107,6 +107,8 @@ export class HlsSegmentedInput extends SegmentedInput {
let lastByteRangeEnd: number | null = null;
let nextByteRange: { offset: number; length: number } | null = null;
let lastProgramDateTimeSeconds: number | null = null;
let targetDuration: number | null = null;
let segmentSeen = false;
// Used for repeated parses where our job it is to only add the new segments
let prevLastSegment = last(this.segments) ?? null;
@@ -220,9 +222,19 @@ export class HlsSegmentedInput extends SegmentedInput {
if (line.startsWith('#EXTINF:')) {
if (prevLastSegment) {
segmentSeen = true;
continue;
}
if (!segmentSeen) {
if (lastProgramDateTimeSeconds === null && nextSequenceNumber > 0 && targetDuration !== null) {
// Offset the first segment's start timestamp by the following:
accumulatedTime = nextSequenceNumber * targetDuration;
}
segmentSeen = true;
}
const extinfContent = line.slice(8);
const commaIndex = extinfContent.indexOf(',');
const durationStr = commaIndex === -1 ? extinfContent : extinfContent.slice(0, commaIndex);
@@ -400,6 +412,7 @@ export class HlsSegmentedInput extends SegmentedInput {
}
this.refreshInterval = duration;
targetDuration = duration;
} else if (line === '#EXT-X-ENDLIST') {
this.streamHasEnded = true;
break; // No need to keep reading after this
@@ -411,6 +424,10 @@ export class HlsSegmentedInput extends SegmentedInput {
}
}
}
if (!headerRead) {
throw new Error('Invalid M3U8 file; no #EXTM3U header.');
}
}
async getFirstSegment() {