Fix HLS demuxer parsing bug, extract HLS mime type out, add TargetRequest.mimeType

This commit is contained in:
Vanilagy
2026-04-16 10:53:36 +02:00
parent d68c9c4fb1
commit 2d0aac3208
8 changed files with 87 additions and 13 deletions
+12 -4
View File
@@ -20,7 +20,7 @@ import { TrackType } from '../output';
import { assert, joinPaths, MaybePromise, Rotation, UNDETERMINED_LANGUAGE } from '../misc';
import { EncodedPacket } from '../packet';
import { readAllLines } from '../reader';
import { AttributeList, canIgnoreLine } from './hls-misc';
import { AttributeList, canIgnoreLine, HLS_MIME_TYPE } from './hls-misc';
import { HlsSegmentedInput } from './hls-segmented-input';
import { PathedSource } from '../source';
import { SegmentedInputTrackDeclaration } from '../segmented-input';
@@ -105,7 +105,7 @@ export class HlsDemuxer extends Demuxer {
if (bandwidth === null) {
throw new Error(
'Invalid M3U8 file; #EXT-X-STREAM-INF tag requires a BANDWIDTH attribute with a valid'
+ ' number value.',
+ ' numerical value.',
);
}
@@ -116,7 +116,7 @@ export class HlsDemuxer extends Demuxer {
hasOnlyKeyPackets: false,
});
} else if (line.startsWith('#EXT-X-I-FRAME-STREAM-INF:')) {
const attributes = new AttributeList(line.slice(18));
const attributes = new AttributeList(line.slice(26));
const playlistPath = attributes.get('uri');
if (playlistPath === null) {
@@ -125,6 +125,14 @@ export class HlsDemuxer extends Demuxer {
);
}
const bandwidth = attributes.getAsNumber('bandwidth');
if (bandwidth === null) {
throw new Error(
'Invalid M3U8 file; #EXT-X-I-FRAME-STREAM-INF tag requires a BANDWIDTH attribute with a'
+ ' valid numerical value.',
);
}
const fullPath = joinPaths(source.rootPath, playlistPath);
variantStreams.push({
@@ -610,7 +618,7 @@ export class HlsDemuxer extends Demuxer {
}
async getMimeType(): Promise<string> {
return 'application/vnd.apple.mpegurl';
return HLS_MIME_TYPE;
}
override dispose(): void {
+2
View File
@@ -6,6 +6,8 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
export const HLS_MIME_TYPE = 'application/vnd.apple.mpegurl';
export const canIgnoreLine = (line: string) => line.length === 0 || (line.startsWith('#') && !line.startsWith('#EXT'));
export class AttributeList {
+18 -4
View File
@@ -29,6 +29,7 @@ import { Writer } from '../writer';
import { EncodedPacket } from '../packet';
import { SubtitleCue, SubtitleMetadata } from '../subtitles';
import { NullTarget, PathedTarget, Target, TargetRequest } from '../target';
import { HLS_MIME_TYPE } from './hls-misc';
type HlsTrackData = {
track: OutputTrack;
@@ -499,7 +500,7 @@ export class HlsMuxer extends Muxer {
}
async getMimeType(): Promise<string> {
return 'application/vnd.apple.mpegurl';
return HLS_MIME_TYPE;
}
private allTracksAreKnown(playlist: Playlist) {
@@ -829,7 +830,11 @@ export class HlsMuxer extends Muxer {
relativeSegmentPath,
);
const target = await this.output._getTarget({ path: fullSegmentPath, isRoot: false });
const target = await this.output._getTarget({
path: fullSegmentPath,
isRoot: false,
mimeType: playlist.segmentFormat.mimeType,
});
target._start();
playlist.singleFile = {
@@ -926,6 +931,7 @@ export class HlsMuxer extends Muxer {
const target = await this.output._getTarget({
path: initPath,
isRoot: false,
mimeType: playlist.segmentFormat.mimeType,
});
target.on('write', ({ end }) => {
playlist.initSegment!.byteSize = Math.max(playlist.initSegment!.byteSize, end);
@@ -1179,7 +1185,11 @@ export class HlsMuxer extends Muxer {
this.format._options.onPlaylist?.(playlistText, toPlaylistInfo(playlist));
const target = await this.output._getTarget({ path: playlistPath, isRoot: false });
const target = await this.output._getTarget({
path: playlistPath,
isRoot: false,
mimeType: HLS_MIME_TYPE,
});
const writer = new Writer(target);
writer.start();
writer.write(textEncoder.encode(playlistText));
@@ -1382,7 +1392,11 @@ export class HlsMuxer extends Muxer {
} else {
// For subsequent master playlist writes, we *must* obtain a different target in order to overwrite
// the file.
const target = await this.output._getTarget({ path: pathedTarget.rootPath, isRoot: true });
const target = await this.output._getTarget({
path: pathedTarget.rootPath,
isRoot: true,
mimeType: HLS_MIME_TYPE,
});
writer = new Writer(target);
writer.start();
}