Rebuilding a 9,000-file music library index after a bad tag sweep

这篇内容暂时只有 English 版本,以下是原文。

Two winters ago I ran a bulk tag rewrite across my library: 9,142 files, mostly FLAC with a few hundred 320k MP3s, sitting on a 4TB external drive formatted exFAT because I share it with a Windows machine. I used a small Python script with mutagen to normalize artist fields — “The Beatles” vs “Beatles”, that kind of thing. The script wrote in place. Halfway through, the drive dropped off the bus (cheap USB hub, my fault), and I ended up with roughly 1,300 files whose tags were truncated or blank. The audio was intact; the metadata was gone. I only noticed weeks later when my player showed a wall of “Unknown Artist” entries.

My first move was to stop writing anything to that drive. I copied the whole tree to a second disk with rsync -a --info=progress2, then worked only on the copy. For recovery I leaned on two things: the filenames (which followed Artist - Album - NN - Title.flac about 80% of the time) and the embedded cuesheets/cover art that survived in some files. I wrote a parser that pulled candidate fields from the path and only filled blanks:

from pathlib import Path
import mutagen

for p in Path('/mnt/music_copy').rglob('*.flac'):
    try:
        f = mutagen.File(p, easy=True)
    except Exception:
        continue
    if f is None or f.get('artist'):
        continue
    parts = p.stem.split(' - ')
    if len(parts) == 4:
        f['artist'] = parts[0]
        f['album'] = parts[1]
        f['tracknumber'] = parts[2]
        f['title'] = parts[3]
        f.save()

That recovered maybe 900 files. The rest I handled by hand — about 400, which took two evenings with a player open next to a terminal. I did not try to be clever with audio fingerprinting. AcoustID would have been the right tool, but the drive was offline-only and I did not want to upload a library I could not fully account for.

The reason I filled blanks instead of rewriting everything: the surviving tags were the only trustworthy source, and a second bulk pass is exactly how I got here. The cost is that the library is now inconsistent — some albums have artist as “Beatles” and some as “The Beatles” — but every file has something, and my player’s grouping is stable enough to live with.

Where this breaks: my path parser silently mis-filled anything with a hyphen in the artist or title (“Jay-Z”, “Wilco - Yankee Hotel Foxtrot” as a single field). I only caught it because a friend’s name appeared as an album. If your filenames do not follow one convention, parse nothing and flag instead. Also, mutagen easy=True will happily write to MP3 and FLAC but not to every container; my Ogg files were skipped without an error I noticed for a week. And exFAT plus a flaky hub is the real war story here — no amount of scripting fixes a bus that drops mid-write.

What I would do differently: hash every file before and after a bulk tag pass, and abort the whole run if any write fails, rather than continuing to the next file. Curious whether anyone here has a tag-normalization scheme that survives a mixed library without a second pass, or whether the honest answer is just “keep the raw files read-only and tag a separate database.”

0
赞同度 100% · 0 条评论 · 7 阅读

0 条评论

支持 Markdown · 回复会实时推送给对方

还没有评论,写下第一条。