-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.py
executable file
·99 lines (84 loc) · 2.04 KB
/
rename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import configparser
import io
import shutil
import subprocess
import os
import tempfile
import struct
import sys
import mutagen
FMT_VERSION = 'BB'
camelot_keys = {
'Abm': '1A',
'G#m': '1A',
'B': '1B',
'Ebm': '2A',
'F#': '2B',
'Bbm': '3A',
'Db': '3B',
'Fm': '4A',
'Ab': '4B',
'Cm': '5A',
'Eb': '5B',
'Gm': '6A',
'Bb': '6B',
'Dm': '7A',
'F': '7B',
'Am': '8A',
'C': '8B',
'Em': '9A',
'G': '9B',
'Bm': '10A',
'D': '10B',
'F#m': '11A',
'A': '11B',
'C#m': '12A',
'Dbm': '12A',
'E': '12B',
}
def readbytes(fp):
for x in iter(lambda: fp.read(1), b''):
if x == b'\00':
break
yield x
def parse(fp):
version = struct.unpack(FMT_VERSION, fp.read(2))
assert version == (0x01, 0x01)
for i in range(3):
data = b''.join(readbytes(fp))
yield float(data.decode('ascii'))
def renamefile(filename, bpm, music_key, tag):
newname = f'/Users/gdh/Desktop/qwe2/{bpm} {music_key} {tag["TPE1"][0][:15]} % {tag["TIT2"]}.mp3'
print('New name: ', newname)
os.rename(filename, newname)
return
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('file', metavar='FILE')
args = parser.parse_args(argv)
tagfile = mutagen.File(args.file)
if tagfile is not None:
try:
tag = tagfile['GEOB:Serato Autotags']
music_key = tagfile['TKEY']
except KeyError:
print('File is missing "GEOB:Serato Autotags" or "TKEY" tag')
return 1
else:
fp = io.BytesIO(tag.data)
else:
fp = open(args.file, mode='rb')
with fp:
bpm, autogain, gaindb = parse(fp)
print('BPM: {}'.format(int(bpm)))
music_key = format(music_key)
if not music_key[0].isnumeric():
music_key = camelot_keys[music_key]
print('Key: {}'.format(music_key))
renamefile(args.file, int(bpm), music_key, tagfile)
return 0
if __name__ == '__main__':
sys.exit(main())