diff --git a/fixtures/content/content.json b/fixtures/content/content.json index 3d2a33a..4ff2826 100644 --- a/fixtures/content/content.json +++ b/fixtures/content/content.json @@ -15,6 +15,7 @@ "width": 180, "height": 320, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v1/init-0.m4s", "initBytes": 826, "segments": [ @@ -47,6 +48,7 @@ "width": 270, "height": 480, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v1/init-1.m4s", "initBytes": 826, "segments": [ @@ -79,6 +81,7 @@ "width": 360, "height": 640, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v1/init-2.m4s", "initBytes": 828, "segments": [ @@ -154,6 +157,7 @@ "width": 180, "height": 320, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v2/init-0.m4s", "initBytes": 826, "segments": [ @@ -186,6 +190,7 @@ "width": 270, "height": 480, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v2/init-1.m4s", "initBytes": 826, "segments": [ @@ -218,6 +223,7 @@ "width": 360, "height": 640, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v2/init-2.m4s", "initBytes": 828, "segments": [ @@ -293,6 +299,7 @@ "width": 180, "height": 320, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v3/init-0.m4s", "initBytes": 826, "segments": [ @@ -325,6 +332,7 @@ "width": 270, "height": 480, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v3/init-1.m4s", "initBytes": 826, "segments": [ @@ -357,6 +365,7 @@ "width": 360, "height": 640, "codec": "avc1", + "frameRateFps": 30, "initUrl": "/media/v3/init-2.m4s", "initBytes": 828, "segments": [ diff --git a/src/dashjs/SFVDashAssetResolver.js b/src/dashjs/SFVDashAssetResolver.js index 4f6df8e..3b2e575 100644 --- a/src/dashjs/SFVDashAssetResolver.js +++ b/src/dashjs/SFVDashAssetResolver.js @@ -29,6 +29,13 @@ function formatTemplate(template, representationId, number, bandwidth, time) { } ); } +function frameRateFpsOf(frameRate) { + if (frameRate == null) return undefined; + const [numerator, denominator = 1] = String(frameRate).split('/').map(Number); + const fps = numerator / denominator; + return Number.isFinite(fps) && fps > 0 ? fps : undefined; +} + function segmentsOf(template, durationS) { if (!template || !template.SegmentTimeline?.S?.length) throw new Error('SegmentTimeline required'); @@ -108,6 +115,7 @@ export class SFVDashAssetResolver { width: rep.width, height: rep.height, codec: rep.codecs ?? set.codecs, + frameRateFps: frameRateFpsOf(rep.frameRate ?? set.frameRate), mimeType: rep.mimeType ?? set.mimeType ?? `${type}/mp4`, initUrl: url(template.initialization), segments: timeline.map(s => ({ diff --git a/src/media/SFVMediaCatalog.js b/src/media/SFVMediaCatalog.js index 8869179..7865265 100644 --- a/src/media/SFVMediaCatalog.js +++ b/src/media/SFVMediaCatalog.js @@ -31,12 +31,13 @@ export class SFVMediaCatalog { getRepresentations(videoId, mediaType = 'video') { this.#assertAvailable(videoId); const reps = this.#video(videoId).representations[mediaType] ?? []; - return reps.map(({ id, bitrateKbps, width, height, codec }) => ({ + return reps.map(({ id, bitrateKbps, width, height, codec, frameRateFps }) => ({ id, bitrateKbps, width, height, - codec + codec, + frameRateFps })); } getSegments(videoId, mediaType = 'video', representationId) { @@ -97,6 +98,7 @@ export class SFVMediaCatalog { width: r.width, height: r.height, codec: r.codec, + frameRateFps: r.frameRateFps, segments: r.segments.map(s => ({ index: s.index, startS: s.startS, diff --git a/src/output/SFVPlaybackMetricsCalculator.js b/src/output/SFVPlaybackMetricsCalculator.js index d31deee..5c15b46 100644 --- a/src/output/SFVPlaybackMetricsCalculator.js +++ b/src/output/SFVPlaybackMetricsCalculator.js @@ -11,9 +11,15 @@ function knownSegments(video) { return video.representations?.video?.[0]?.segments ?? []; } -function bitrateFor(video, representationId) { +function representationProperties(video, representationId) { const rep = video.representations?.video?.find(r => String(r.id) === String(representationId)); - return finite(rep?.bitrateKbps) ? rep.bitrateKbps : null; + return { + bitrate_kbps: finite(rep?.bitrateKbps) ? rep.bitrateKbps : null, + width: finite(rep?.width) ? rep.width : null, + height: finite(rep?.height) ? rep.height : null, + codec: rep?.codec ?? null, + frame_rate_fps: finite(rep?.frameRateFps) ? rep.frameRateFps : null + }; } function appendMap(allEvents) { @@ -87,7 +93,7 @@ function progressForVideo(video, playbackEvents, assignments) { intervals.push({ video_id: video.videoId, representation_id: representationId, - bitrate_kbps: bitrateFor(video, representationId), + ...representationProperties(video, representationId), segment_index: seg.index, start_at_s: startTime, end_at_s: endTime, diff --git a/test/manifest-base-uri.test.js b/test/manifest-base-uri.test.js index 4bdc7cf..97af013 100644 --- a/test/manifest-base-uri.test.js +++ b/test/manifest-base-uri.test.js @@ -56,3 +56,21 @@ test('manifest directory rather than page origin is the base URI', () => { const result = resolver.parse('v42', new TextEncoder().encode(''), url); assert.equal(result.dashManifest.baseUri, 'https://cdn.example.test/feed/item42/'); }); + +test('frame rate comes from the adaptation set or representation', () => { + const url = 'https://cdn.example.test/feed/item42/index.mpd'; + const parseWithFrameRate = (setFrameRate, representationFrameRate) => { + const mpd = fakeDashParser(); + const set = mpd.Period[0].AdaptationSet[0]; + set.frameRate = setFrameRate; + set.Representation[0].frameRate = representationFrameRate; + const resolver = new SFVDashAssetResolver({ parseDashManifest: () => mpd }); + const result = resolver.parse('v42', new TextEncoder().encode(''), url); + return result.video.representations.video[0].frameRateFps; + }; + assert.equal(parseWithFrameRate('30/1'), 30); + assert.equal(parseWithFrameRate(30), 30); + assert.equal(parseWithFrameRate('30/1', '25'), 25); + assert.ok(Math.abs(parseWithFrameRate('30000/1001') - 29.97) < 0.01); + assert.equal(parseWithFrameRate(), undefined); +}); diff --git a/test/playback-metrics.test.js b/test/playback-metrics.test.js index 5ed2ce7..e586ad0 100644 --- a/test/playback-metrics.test.js +++ b/test/playback-metrics.test.js @@ -235,3 +235,18 @@ test('prefetched and appended but never played segments contribute zero played d assert.equal(m.per_video.v2.representation_intervals.length, 0); close(m.per_video.v1.representation_played_duration_s['0'], 1); }); + +test('representation intervals hold the bitrate, resolution, codec and frame rate', () => { + const m = calculatePlaybackMetrics({ + dataset, + allEvents: [appended('v1', 0, '2', 0)], + playbackEvents: [started('v1', 0), sample('v1', 1, 1)], + endTimeS: 1 + }); + const interval = m.per_video.v1.representation_intervals[0]; + assert.equal(interval.bitrate_kbps, 650); + assert.equal(interval.width, 360); + assert.equal(interval.height, 640); + assert.equal(interval.codec, 'avc1'); + assert.equal(interval.frame_rate_fps, 30); +});