From b4cae8eac28f50afa92d82bf647a2ab4b58a29ab Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 8 Jul 2026 16:57:22 +0200 Subject: [PATCH] feat: scan playtime duration when adding tracks --- index.html | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 93b0f19..b16d67b 100644 --- a/index.html +++ b/index.html @@ -929,6 +929,20 @@ async function readAudioInfo(file) { } catch {} return info; } +async function readDuration(file) { + try { + const url = URL.createObjectURL(file); + const dur = await new Promise((resolve) => { + const a = new Audio(); + a.preload = 'metadata'; + a.onloadedmetadata = () => { resolve(a.duration); }; + a.onerror = () => { resolve(0); }; + a.src = url; + }); + URL.revokeObjectURL(url); + return isFinite(dur) && dur > 0 ? dur : 0; + } catch { return 0; } +} async function processFiles(files, paths) { scanning.style.display = 'flex'; const total = files.length; @@ -940,7 +954,7 @@ async function processFiles(files, paths) { prog++; const url = URL.createObjectURL(file); const path = paths ? paths[i] : file.name; - const [tags, ainfo] = await Promise.all([readTags(file), readAudioInfo(file)]); + const [tags, ainfo, duration] = await Promise.all([readTags(file), readAudioInfo(file), readDuration(file)]); const name = tags.title || file.name.replace(/\.[^.]+$/, ''); found.push({ name, @@ -958,7 +972,7 @@ async function processFiles(files, paths) { file, path, size: file.size, - playtime: 0, + playtime: duration, ainfo, }); scanningText.textContent = `Processing ${prog}: ${name}`;