mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-01 12:53:49 +02:00
Refactor Target and Writer, add single-file HLS writing mode
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user