feat: scan playtime duration when adding tracks

This commit is contained in:
Armin 2026-07-08 16:57:22 +02:00
commit b4cae8eac2

View file

@ -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}`;