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);