diff --git a/lib/dash.js b/lib/dash.js index 1ac581f..1593a8c 100644 --- a/lib/dash.js +++ b/lib/dash.js @@ -135,11 +135,20 @@ const parseLanguage = (representation, adaptationSet, fallbackLanguage) => { return language; }; +const identifierPattern = /\$([A-z]*)(?:(%0)([0-9]+)d)?\$/g; +const identifierReplacement = + (values) => (match, identifier, format, width) => { + if (match === '$$') return '$'; + if (typeof values[identifier] === 'undefined') return match; + const value = '' + values[identifier]; + if (identifier === 'RepresentationID') return value; + if (!format) width = 1; + else width = parseInt(width, 10); + if (value.length >= width) return value; + return value.padStart(width, '0'); + }; const buildSegmentUrl = (template, fields) => { - let result = template; - for (const [key, value] of Object.entries(fields)) - result = result.replace('$' + key + '$', value); - return result; + return template.replace(identifierPattern, identifierReplacement(fields)); }; const resolveSegmentTemplateUrls = (segmentTemplate, baseUrl, manifestUrl) => { @@ -215,10 +224,6 @@ const parseSegmentsFromTemplate = ( : DEFAULT_SEGMENTS_COUNT; const bandwidth = representation.get('bandwidth'); const id = representation.get('id'); - - const sanitizeTemplate = (template) => - template?.replace(/\$.*?(RepresentationID|Number|Time).*?\$/g, '$$$1$$'); - const segments = []; if (segmentTimeline) { segments.push( @@ -230,9 +235,8 @@ const parseSegmentsFromTemplate = ( ), ); } else { - const template = sanitizeTemplate(segmentTemplate.get('media')); for (let i = startNumber; i < startNumber + segmentsCount; i++) { - const url = buildSegmentUrl(template, { + const url = buildSegmentUrl(segmentTemplate.get('media'), { Bandwidth: bandwidth, RepresentationID: id, Number: i, @@ -241,9 +245,7 @@ const parseSegmentsFromTemplate = ( segments.push({ url }); } } - const initialization = sanitizeTemplate( - segmentTemplate.get('initialization'), - ); + const initialization = segmentTemplate.get('initialization'); if (initialization) { const url = buildSegmentUrl(initialization, { Bandwidth: bandwidth, diff --git a/test/axinom.test.js b/test/axinom.test.js index b97bf3d..4e0892e 100644 --- a/test/axinom.test.js +++ b/test/axinom.test.js @@ -13,7 +13,7 @@ test('axinom manifest parsing', async () => { const firstVideoSegment = firstVideoTrack.segments[1]; // Skip init strictEqual( firstVideoSegment.url, - 'https://media.axprod.net/TestVectors/v7-MultiDRM-SingleKey/5/1.m4s', + 'https://media.axprod.net/TestVectors/v7-MultiDRM-SingleKey/5/0001.m4s', ); const firstSubtitleTrack = manifest.tracks.subtitles[0]; diff --git a/types/dasha.d.ts b/types/dasha.d.ts index b2132da..daac1c5 100644 --- a/types/dasha.d.ts +++ b/types/dasha.d.ts @@ -145,8 +145,7 @@ export type SubtitleCodec = | 'TTML' | 'VTT' | 'STPP' - | 'fTTML' - | 'fVTT'; + | 'WVTT'; export interface SubtitleTrack extends Track { type: 'text';