#!/usr/bin/python3 import music_tag import os import sys import subprocess def show_help(): print("Usage: " + sys.argv[0] + " {value}") print(" commands:") print(" album albumartist") print(" comment compilation composer") print(" discnumber") print(" genre") print(" rename") print(" totaldiscs totaltracks tracknumber tracktitle") print(" year") if len(sys.argv) < 2: show_help() sys.exit() filename = sys.argv[1] mp3 = music_tag.load_file(filename) if len(sys.argv) < 3: show_tags(mp3) sys.exit() command = sys.argv[2] # Dictionary to map commands to functions or subprocess commands command_actions = { 'rename': lambda: rename_mp3(mp3, filename), 'folder': lambda: move_to_album_folder(mp3, filename), '_folder': lambda: move_to_parent_album_folder(mp3, filename), '~comment': lambda: remove_tag(mp3, 'comment'), '~albumartist': lambda: remove_tag(mp3, 'albumartist'), 'album': lambda value: set_tag(mp3, 'album', value), 'albumartist': lambda value: set_tag(mp3, 'albumartist', value), 'artist': lambda value: set_tag(mp3, 'artist', value), 'comment': lambda value: set_tag(mp3, 'comment', value), 'composer': lambda value: set_tag(mp3, 'composer', value), 'discnumber': lambda value: set_tag(mp3, 'discnumber', value), 'totaldiscs': lambda value: set_tag(mp3, 'totaldiscs', value), 'genre': lambda value: set_tag(mp3, 'genre', value), 'totaltracks': lambda value: set_tag(mp3, 'totaltracks', value), 'tracktitle': lambda value: set_tag(mp3, 'tracktitle', value), 'tracknumber': lambda value: set_tag(mp3, 'tracknumber', value), 'year': lambda value: set_tag(mp3, 'year', value), } if command in command_actions: if len(sys.argv) < 4: print("Value is missing!") sys.exit() value = sys.argv[3] command_actions[command](value) else: print("Unknown command:", command) sys.exit() def rename_mp3(mp3, filename): newfilename = f"{mp3['tracknumber'].value.zfill(3)}.{mp3['tracktitle']}.{bitrate_kbps(mp3)}.mp3" subprocess.run(['mv', filename, newfilename]) def move_to_album_folder(mp3, filename): folder = mp3['album'] subprocess.run(['mkdir', folder]) subprocess.run(['mv', filename, folder]) def move_to_parent_album_folder(mp3, filename): folder = mp3['album'] subprocess.run(['mkdir', f'../{folder}']) subprocess.run(['mv', filename, f'../{folder}']) def remove_tag(mp3, tag): mp3.remove_tag(tag) mp3.save() print(f"{tag} removed!") def set_tag(mp3, tag, value): old_value = str(mp3[tag].value) mp3[tag] = value new_value = str(mp3[tag].value) mp3.save() print(f"{tag}: {old_value} -> {new_value}") def bitrate_kbps(mp3): bitrate = int(int(mp3['#bitrate']) / 1000) return f"{bitrate}kbps" def show_tags(mp3): print(f"{mp3['artist']} / {mp3['album']} / {mp3['tracknumber'].value.zfill(3)}.{mp3['tracktitle']}") print("========") print(f"album : {mp3['album']}") print(f"albumartist : {mp3['albumartist']}") print(f"artist : {mp3['artist']}") print(f"artwork : {mp3['artwork']}") print(f"comment : {mp3['comment']}") print(f"compilation : {mp3['compilation']}") print(f"composer : {mp3['composer']}") print(f"discnumber / totaldiscs : {mp3['discnumber'].value} / {mp3['totaldiscs'].value}") print(f"genre : {mp3['genre']}") print(f"tracknumber / totaltracks : {mp3['tracknumber'].value} / {mp3['totaltracks'].value}") print(f"tracktitle : {mp3['tracktitle']}") print(f"year : {mp3['year']}") print(f"isrc : {mp3['isrc']}") length = mp3['#length'].value min = int(length / 60) sec = int(length - min * 60 + .5) print("---") print(f"{bitrate_kbps(mp3)} / {mp3['#codec']} / {min}m{sec}s / {mp3['#channels']} channels / {mp3['#samplerate'] / 1000}kHz")