From cbbac3c25cd7bf0c6303edd0da2ff19cf4c3ed32 Mon Sep 17 00:00:00 2001 From: Fredrik Burmester Date: Mon, 5 Jan 2026 21:28:00 +0100 Subject: [PATCH] fix(hls): parse M3U8 attributes respecting quoted strings The previous implementation split on commas without respecting quoted values, causing subtitles with commas in names (e.g., "English, Forced") or URIs with special characters to be incorrectly parsed. Uses regex to properly match key=value pairs where values can be either quoted (containing any characters except quotes) or unquoted (no commas). --- utils/hls/parseM3U8ForSubtitles.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/utils/hls/parseM3U8ForSubtitles.ts b/utils/hls/parseM3U8ForSubtitles.ts index f4d25458..e5d35fcf 100644 --- a/utils/hls/parseM3U8ForSubtitles.ts +++ b/utils/hls/parseM3U8ForSubtitles.ts @@ -44,12 +44,14 @@ export async function parseM3U8ForSubtitles( function parseAttributes(line: string): { [key: string]: string } { const attributes: { [key: string]: string } = {}; - const parts = line.split(","); - parts.forEach((part) => { - const [key, value] = part.split("="); - if (key && value) { - attributes[key.trim()] = value.replace(/"/g, "").trim(); - } - }); + const regex = /([A-Z-]+)=(?:"([^"]*)"|([^,]*))/g; + let match: RegExpExecArray | null; + + while ((match = regex.exec(line)) !== null) { + const key = match[1]; + const value = match[2] ?? match[3]; // quoted or unquoted + attributes[key] = value; + } + return attributes; }