Steven Smith revised this gist . Go to revision
1 file changed, 42 insertions
mp3_fn_to_tags.py(file created)
| @@ -0,0 +1,42 @@ | |||
| 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) | |
Newer
Older