annotate tools/sg101mp3comptool.py @ 1190:ce5a5c9cd9d8

Tweaks to MP3 comp tool
author Brian Neal <bgneal@gmail.com>
date Sun, 13 Mar 2022 13:09:37 -0500
parents 83dd2db291f7
children
rev   line source
bgneal@1189 1 #!/usr/bin/env python
bgneal@1189 2 # Copyright (C) 2021 by Brian Neal.
bgneal@1189 3
bgneal@1189 4 """This file contains the main routine for the SG101 MP3 Comp Tool."""
bgneal@1189 5
bgneal@1189 6 import argparse
bgneal@1189 7 import eyed3
bgneal@1189 8 import json
bgneal@1189 9 import os.path
bgneal@1189 10 import re
bgneal@1189 11
bgneal@1189 12
bgneal@1189 13 DESC = """SG101 MP3 Comp Tool"""
bgneal@1189 14 TITLE_RE = re.compile(r'^(\d+)\s+-\s+(.+)"(.+)"$')
bgneal@1189 15
bgneal@1189 16 def process_mp3(mp3, args):
bgneal@1189 17 title = mp3['fields']['title']
bgneal@1190 18 print(f'Processing {title}...')
bgneal@1189 19 m = TITLE_RE.match(title)
bgneal@1189 20 track_num = int(m[1])
bgneal@1189 21 artist = m[2].strip()
bgneal@1189 22 title = m[3]
bgneal@1189 23 filename = mp3['fields']['file'].split('/')[-1]
bgneal@1189 24 filepath = os.path.join(args.mp3_dir, filename)
bgneal@1189 25
bgneal@1189 26 mp3file = eyed3.load(filepath)
bgneal@1190 27 if mp3file.tag is None:
bgneal@1190 28 mp3file.tag = eyed3.id3.tag.Tag()
bgneal@1189 29 mp3file.tag.clear()
bgneal@1189 30 mp3file.tag.title = title
bgneal@1189 31 mp3file.tag.artist = artist
bgneal@1189 32 mp3file.tag.album = args.album
bgneal@1189 33 mp3file.tag.recording_date = args.recording_date
bgneal@1189 34 mp3file.tag.track_num = track_num
bgneal@1189 35 mp3file.tag.genre = 'Instrumental Rock'
bgneal@1189 36 mp3file.tag.save()
bgneal@1189 37
bgneal@1189 38
bgneal@1189 39 def main(argv=None):
bgneal@1189 40 parser = argparse.ArgumentParser(description=DESC)
bgneal@1189 41 parser.add_argument('json', metavar='JSON',
bgneal@1189 42 help='JSON file to control the tagging process')
bgneal@1189 43 parser.add_argument('-d', '--mp3-dir', default='.',
bgneal@1189 44 help='directory to find MP3 files')
bgneal@1189 45 parser.add_argument('-a', '--album',
bgneal@1189 46 default='SurfGuitar101.com 2021 MP3 Compilation',
bgneal@1189 47 help='album name tag value')
bgneal@1189 48 parser.add_argument('-r', '--recording-date', default='2021',
bgneal@1189 49 help='recording date tag value')
bgneal@1189 50
bgneal@1189 51 args = parser.parse_args(args=argv)
bgneal@1189 52
bgneal@1189 53 with open(args.json, 'r') as fp:
bgneal@1189 54 mp3data = json.load(fp)
bgneal@1189 55
bgneal@1189 56 for mp3 in mp3data:
bgneal@1189 57 process_mp3(mp3, args)
bgneal@1189 58
bgneal@1189 59 if __name__ == '__main__':
bgneal@1189 60 main()