Last active
July 13, 2020 08:22
-
-
Save pixelchai/26cfc953cf0da1f1a0d431ce37972b19 to your computer and use it in GitHub Desktop.
Redistribute ID3 POPM ratings to follow a different distribution (especially: to import songs rated in MusicBee into Quod Libet)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from mutagen.id3 import ID3 | |
from pathlib import Path | |
WRITE = 1 | |
bins = [248, 219, 190, 157, 121, 100, 59, 20, 8, 1, 0] | |
# new_dist = [255, 229, 204, 178, 135, 127, 80, 56, 51, 25, 0] | |
new_dist = [int(255 * x / 10) for x in range(10, -1, -1)] | |
old_dist = [] | |
try: | |
songs_path = sys.argv[1] | |
for path in Path(songs_path).rglob("*.mp3"): | |
try: | |
song_file = ID3(path) | |
popms = song_file.getall('POPM') | |
for popm in popms: | |
new_rating = 0 | |
rating = popm.rating | |
for i, lb in enumerate(bins): | |
if rating >= lb: | |
new_rating = new_dist[i] | |
break | |
if rating not in old_dist: | |
old_dist.append(rating) | |
popm.rating = new_rating | |
print("{} -> {}".format(rating, new_rating)) | |
if WRITE: | |
song_file.save() | |
except: | |
print("Error dealing with: {}".format(path)) | |
except KeyError: | |
print("Please provide music path") | |
print("Old ratings dist: ") | |
print(sorted(old_dist, reverse=True)) | |
print("New ratings dist: ") | |
print(sorted(new_dist, reverse=True)) | |
print(("Not w" if not WRITE else "W") + "ritten") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment