Add duplicate check before downloading tracks
Before starting yt-dlp, check if Artist/Album/Title.{ext} already
exists on disk. If found, log a skip message and return early instead
of re-downloading and overwriting the existing file.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
023d3710ab
commit
f08303753c
1 file changed
+16
-2
@@ -107,6 +107,14 @@ async function downloadArtwork(url, dir) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const AUDIO_EXTS = ['mp3', 'flac', 'opus', 'm4a', 'ogg', 'wav'];
|
||||||
|
|
||||||
|
function findExistingTrack(dir, trackPrefix, safeTitle) {
|
||||||
|
return AUDIO_EXTS
|
||||||
|
.map(ext => path.join(dir, `${trackPrefix}${safeTitle}.${ext}`))
|
||||||
|
.find(f => fs.existsSync(f)) || null;
|
||||||
|
}
|
||||||
|
|
||||||
// Download a single track: yt-dlp audio + ffmpeg clean metadata + iTunes cover embedded
|
// Download a single track: yt-dlp audio + ffmpeg clean metadata + iTunes cover embedded
|
||||||
async function downloadTrack({ jobId, artist, albumArtist, title, album, year, trackNum, totalTracks, discNum, discTotal, compilation, genre, artworkUrl, outputDir }) {
|
async function downloadTrack({ jobId, artist, albumArtist, title, album, year, trackNum, totalTracks, discNum, discTotal, compilation, genre, artworkUrl, outputDir }) {
|
||||||
const mainArtist = albumArtist || artist;
|
const mainArtist = albumArtist || artist;
|
||||||
@@ -117,6 +125,13 @@ async function downloadTrack({ jobId, artist, albumArtist, title, album, year, t
|
|||||||
|
|
||||||
const safeTitle = sanitize(title);
|
const safeTitle = sanitize(title);
|
||||||
const trackPrefix = trackNum ? String(trackNum).padStart(2, '0') + ' - ' : '';
|
const trackPrefix = trackNum ? String(trackNum).padStart(2, '0') + ' - ' : '';
|
||||||
|
|
||||||
|
const existing = findExistingTrack(dir, trackPrefix, safeTitle);
|
||||||
|
if (existing) {
|
||||||
|
log(jobId, `⏭ Bereits vorhanden: ${path.basename(existing)} — übersprungen`);
|
||||||
|
return { success: true, dir, skipped: true };
|
||||||
|
}
|
||||||
|
|
||||||
const outputTemplate = path.join(dir, `${trackPrefix}${safeTitle}.%(ext)s`);
|
const outputTemplate = path.join(dir, `${trackPrefix}${safeTitle}.%(ext)s`);
|
||||||
const query = `${mainArtist} - ${title} official audio`;
|
const query = `${mainArtist} - ${title} official audio`;
|
||||||
log(jobId, `🔍 Suche: "${query}"`);
|
log(jobId, `🔍 Suche: "${query}"`);
|
||||||
@@ -156,9 +171,8 @@ async function downloadTrack({ jobId, artist, albumArtist, title, album, year, t
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Step 2: find the audio file yt-dlp wrote (newest audio file in dir)
|
// Step 2: find the audio file yt-dlp wrote (newest audio file in dir)
|
||||||
const audioExts = ['mp3', 'flac', 'opus', 'm4a', 'ogg', 'wav'];
|
|
||||||
const audioFile = fs.readdirSync(dir)
|
const audioFile = fs.readdirSync(dir)
|
||||||
.filter(f => audioExts.some(e => f.endsWith(`.${e}`)))
|
.filter(f => AUDIO_EXTS.some(e => f.endsWith(`.${e}`)))
|
||||||
.map(f => path.join(dir, f))
|
.map(f => path.join(dir, f))
|
||||||
.sort((a, b) => fs.statSync(b).mtimeMs - fs.statSync(a).mtimeMs)[0];
|
.sort((a, b) => fs.statSync(b).mtimeMs - fs.statSync(a).mtimeMs)[0];
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user