mp3_fn_to_tags.py
· 1.7 KiB · Python
Raw
#!/usr/bin/env python3
from mutagen.easyid3 import EasyID3 # pypi mutagen
from parse import parse # pypi parse
from argparse import ArgumentParser
from glob import glob
from sys import exit
def write_tags(filename, newinfo):
audio = EasyID3(filename)
audio.update(newinfo)
audio.save()
return "{}: {}".format(filename, str(audio))
def main():
parser = ArgumentParser()
parser.add_argument("format", help="String format for input files e.g '{tracknumber} {title}.mp3'. Use '\"\" \"\" --print-keys' for valid keys")
parser.add_argument("files", nargs="+", help="Filenames to process. Accepts shell wildcard expansion")
parser.add_argument("--artist", help="Artist name to add to track tags, overriding filename")
parser.add_argument("--album", help="Album name to add to track tags, overriding filename")
parser.add_argument("--print-keys", help="Prints valid keys for string format via mutagen. You'll need to add \"\" \"\" to satisfy the missing format/files parameters.", action="store_true")
argv = parser.parse_args()
if argv.print_keys:
print(EasyID3.valid_keys.keys())
return 0
if len(argv.files) == 1:
argv.files = glob(argv.files[0])
for f in argv.files:
newinfo = parse(argv.format, f)
if not newinfo:
print("Error parsing {}: Format does not match filename".format(f))
continue
newinfo = newinfo.named
if argv.artist: newinfo["artist"] = argv.artist
if argv.album: newinfo["album"] = argv.album
print(write_tags(f, newinfo))
return 0
if __name__ == "__main__":
try:
exit(main())
except KeyboardInterrupt:
exit(130)
| 1 | #!/usr/bin/env python3 |
| 2 | from mutagen.easyid3 import EasyID3 # pypi mutagen |
| 3 | from parse import parse # pypi parse |
| 4 | from argparse import ArgumentParser |
| 5 | from glob import glob |
| 6 | from sys import exit |
| 7 | |
| 8 | def write_tags(filename, newinfo): |
| 9 | audio = EasyID3(filename) |
| 10 | audio.update(newinfo) |
| 11 | audio.save() |
| 12 | return "{}: {}".format(filename, str(audio)) |
| 13 | |
| 14 | def main(): |
| 15 | parser = ArgumentParser() |
| 16 | parser.add_argument("format", help="String format for input files e.g '{tracknumber} {title}.mp3'. Use '\"\" \"\" --print-keys' for valid keys") |
| 17 | parser.add_argument("files", nargs="+", help="Filenames to process. Accepts shell wildcard expansion") |
| 18 | parser.add_argument("--artist", help="Artist name to add to track tags, overriding filename") |
| 19 | parser.add_argument("--album", help="Album name to add to track tags, overriding filename") |
| 20 | parser.add_argument("--print-keys", help="Prints valid keys for string format via mutagen. You'll need to add \"\" \"\" to satisfy the missing format/files parameters.", action="store_true") |
| 21 | argv = parser.parse_args() |
| 22 | if argv.print_keys: |
| 23 | print(EasyID3.valid_keys.keys()) |
| 24 | return 0 |
| 25 | if len(argv.files) == 1: |
| 26 | argv.files = glob(argv.files[0]) |
| 27 | for f in argv.files: |
| 28 | newinfo = parse(argv.format, f) |
| 29 | if not newinfo: |
| 30 | print("Error parsing {}: Format does not match filename".format(f)) |
| 31 | continue |
| 32 | newinfo = newinfo.named |
| 33 | if argv.artist: newinfo["artist"] = argv.artist |
| 34 | if argv.album: newinfo["album"] = argv.album |
| 35 | print(write_tags(f, newinfo)) |
| 36 | return 0 |
| 37 | |
| 38 | if __name__ == "__main__": |
| 39 | try: |
| 40 | exit(main()) |
| 41 | except KeyboardInterrupt: |
| 42 | exit(130) |