Automatically tags your FLAC music library with genre data from Last.fm.
Python 3.x and the following packages:
pip install mutagen requests python-dotenv-
Get a free Last.fm API key at https://www.last.fm/api/account/create
- App name: anything (e.g. "Music Tagger")
- App description: anything
-
Create a
.envfile in the same folder as the script:
LASTFM_API_KEY=your_api_key_here- Edit the
MUSIC_DIRvariable in the script to point to your music folder:
MUSIC_DIR = r"D:\Music\Songs"python lastfm_genre_tagger.pyInside the script, you can adjust these settings:
| Setting | Default | Description |
|---|---|---|
MUSIC_DIR |
D:\Music\Songs |
Path to your music library |
MAX_GENRES |
3 |
Max number of genres to write per track |
MIN_TAG_COUNT |
10 |
Minimum Last.fm tag popularity (filters noise) |
SKIP_IF_HAS_GENRE |
True |
Skip files that already have a genre tag |
RATE_LIMIT_DELAY |
0.25 |
Seconds between API calls (don't go below 0.25) |
- Scans all
.flacfiles recursively inMUSIC_DIR - For each file, reads the
artistandtitletags - Queries Last.fm
track.getTopTagsfor genre tags - If no track tags found, falls back to
artist.getTopTags - Filters out noise tags (seen live, favorites, etc.)
- Writes genres back into the FLAC file's
genretag - Skips files that already have genre tags (preserves already tagged files)
~30-40 minutes for 3800 files at the default rate limit.
Once complete, use Mp3tag to export your library tags to CSV:
- Open Mp3tag, load your music folder
- File → Export
- Export with Title, Artist, Album, Genre columns
Then use the exported CSV to regenerate genre-based M3U playlists.
- Obscure indie tracks may still have no genre if Last.fm has no data for them
- The script is safe to re-run — it skips already-tagged files by default
- Set
SKIP_IF_HAS_GENRE = Falseto overwrite existing genre tags