annotate tools/sg101mp3comptool.py @ 1189:83dd2db291f7

Added tool to tag MP3 comp files.
author Brian Neal <bgneal@gmail.com>
date Mon, 27 Dec 2021 19:35:53 -0600
parents
children ce5a5c9cd9d8
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@1189 18 m = TITLE_RE.match(title)
bgneal@1189 19 track_num = int(m[1])
bgneal@1189 20 artist = m[2].strip()
bgneal@1189 21 title = m[3]
bgneal@1189 22 filename = mp3['fields']['file'].split('/')[-1]
bgneal@1189 23 filepath = os.path.join(args.mp3_dir, filename)
bgneal@1189 24
bgneal@1189 25 mp3file = eyed3.load(filepath)
bgneal@1189 26 mp3file.tag.clear()
bgneal@1189 27 mp3file.tag.title = title
bgneal@1189 28 mp3file.tag.artist = artist
bgneal@1189 29 mp3file.tag.album = args.album
bgneal@1189 30 mp3file.tag.recording_date = args.recording_date
bgneal@1189 31 mp3file.tag.track_num = track_num
bgneal@1189 32 mp3file.tag.genre = 'Instrumental Rock'
bgneal@1189 33 mp3file.tag.save()
bgneal@1189 34
bgneal@1189 35
bgneal@1189 36 def main(argv=None):
bgneal@1189 37 parser = argparse.ArgumentParser(description=DESC)
bgneal@1189 38 parser.add_argument('json', metavar='JSON',
bgneal@1189 39 help='JSON file to control the tagging process')
bgneal@1189 40 parser.add_argument('-d', '--mp3-dir', default='.',
bgneal@1189 41 help='directory to find MP3 files')
bgneal@1189 42 parser.add_argument('-a', '--album',
bgneal@1189 43 default='SurfGuitar101.com 2021 MP3 Compilation',
bgneal@1189 44 help='album name tag value')
bgneal@1189 45 parser.add_argument('-r', '--recording-date', default='2021',
bgneal@1189 46 help='recording date tag value')
bgneal@1189 47
bgneal@1189 48 args = parser.parse_args(args=argv)
bgneal@1189 49
bgneal@1189 50 with open(args.json, 'r') as fp:
bgneal@1189 51 mp3data = json.load(fp)
bgneal@1189 52
bgneal@1189 53 for mp3 in mp3data:
bgneal@1189 54 process_mp3(mp3, args)
bgneal@1189 55
bgneal@1189 56 if __name__ == '__main__':
bgneal@1189 57 main()