From 6faebd723d22f04ebf7dae2ad56f86aeb64c5c33 Mon Sep 17 00:00:00 2001 From: Joris Bertomeu Date: Tue, 26 Aug 2025 11:45:31 +0200 Subject: [PATCH] Add duration --- index.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 391e05b..d0bd38a 100644 --- a/index.js +++ b/index.js @@ -353,6 +353,29 @@ const extractPercentage = (line) => { return Math.max(...percentages.filter((p) => p < 100)); } +const formatDuration = (seconds) => { + if (isNaN(seconds) || seconds < 0) { + return 'Invalid input'; + } + + const hours = Math.floor(seconds / 3600); + const minutes = Math.floor((seconds % 3600) / 60); + const secs = seconds % 60; + + const parts = []; + if (hours > 0) { + parts.push(`${hours}h`); + } + if (minutes > 0) { + parts.push(`${minutes}m`); + } + if (secs > 0) { + parts.push(`${secs}s`); + } + + return parts.join(', ') || '0 secondes'; +} + const parseMPDStream = async (mpdUrl) => { const mpdResponse = await axios({ url: mpdUrl, @@ -404,7 +427,9 @@ const parseMPDStream = async (mpdUrl) => { resolution: { width: parsedManifest.playlists?.[i]?.attributes?.RESOLUTION?.width, height: parsedManifest.playlists?.[i]?.attributes?.RESOLUTION?.height - } + }, + duration: parsedManifest.playlists?.[i]?.targetDuration, + formatedDuration: formatDuration(parsedManifest.playlists?.[i]?.targetDuration || 0) }); } obj.videoTracks = obj.videoTracks.sort((a, b) => b.resolution.width - a.resolution.width);