-
Notifications
You must be signed in to change notification settings - Fork 4
/
CUE.py
145 lines (142 loc) · 5.41 KB
/
CUE.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import sys
import subprocess
metadata={b"TITLE":[],b"PERFORMER":[],b"INDEX":[],b"REM COMPOSER":[]}
def timedif(i1,i2):
i1,i2=i1.split(":"),i2.split(":")
a=(int(i1[0])*60)+int(i1[1])
b=(int(i2[0])*60)+int(i2[1])
return b-a
def cuedata(pth):
with open(pth,"+r",encoding="utf-8") as ff:
f=ff.read()
k=f.encode('utf-8')
ff=k.split(b"TRACK")
ff.pop(0)
for i in ff:
for spi in i.split(b"\n"):
for ky in metadata:
if ky in spi:
if ky==b"INDEX":
spi=spi.split(ky)[1].strip().split(b" ")[1]
else:
spi=spi.split(ky)[1].strip().strip(b'""')
metadata[ky].append(spi)
break
return metadata
def chaff(time):
min,sec=time.split(':')
min=int(min)
sec=int(sec)
if min>59:
hr=min//60
min%=60
if hr==0:
return "%02d:%02d" % (min, sec)
elif hr < 10:
return "0%0d:%02d:%02d" % (hr, min, sec)
else:
return "%d:%02d:%02d" % (hr, min, sec)
return time
def validtitle(name):
for inva in ['/','\\','?','%','*',':', '|', '”', '<','>']:
if inva in name:
name=name.replace(inva,'')
return name
def main(arg=sys.argv[1:]):
if arg:
if os.path.isfile(arg[0]):
if os.path.splitext(arg[0])[1].lower() in ['.png','.jpg','.jpeg','.webp']:
asmodeus=arg[0]
else:
print(f"The extension of the file '{arg[0]}' is unrecognised.")
exit()
else:
print(f"The path '{arg[0]}' is not valid.")
exit()
while True:
repm=input("Location of CUE:")
if repm.lower()=="break":
exit()
if not os.path.exists(repm):
print('Location not valid.Try again or use "break" to exit')
else:
break
while True:
dspth=input("\nExtract Location:")
if dspth=="":
break
if dspth.lower()=="break":
exit()
if not os.path.exists(dspth):
print("Location not valid.Press Enter or enter a valid location")
else:
break
reps=os.listdir(repm)
treatgm=0
for rep in reps:
if rep.lower().endswith('.cue'):
treatgm=1
chk=0
rep=repm+"\\"+rep
for i in ['flac','m4a','mp3','aac','wav','ogg']:
loc=rep[:-3]+i
if os.path.exists(loc):
chk=1
break
if chk:
datacu=cuedata(rep)
mfile=loc
ext=loc[loc.rindex('.'):]
if not arg:
cimg=["ffmpeg","-hide_banner","-y","-i",mfile,"-an","-vcodec","copy","cover.png"]
aimg=subprocess.run(cimg,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
asmodeus='cover.png'
a=0
b=0
wolfe=0
print("\n","—"*55)
for i in datacu[b'TITLE']:
i=i.decode('utf-8')
ior=validtitle(i)
otfl=f'{ior+"_tmp"+ext}'
otfl_fn=f'{ior+ext}'
if dspth:
otfl=dspth+"\\"+otfl
otfl_fn=dspth+"\\"+otfl_fn
tit=f'title={i}'
artt='artist='+datacu[b'PERFORMER'][a].decode('utf-8')
atime=datacu[b'INDEX'][b:b+2]
if len(atime)==1:
wolfe=1
stime=atime[0].decode('utf-8')
else:
stime,etime=atime[0].decode('utf-8').strip(),atime[1].decode('utf-8').strip()
diff=str(timedif(stime,etime))
stime=stime.rsplit(":",1)[0]
stime=chaff(stime)
a+=1
b+=2
trno=f'track={a}'
print(f"TRACK {a}: {i}")
if wolfe:
if ext!='.flac':
cmd=["ffmpeg","-hide_banner","-ss",stime,"-y","-i",mfile,"-avoid_negative_ts","make_zero","-c","copy","-metadata",tit,"-metadata",artt,"-metadata",trno,otfl]
else:
cmd=cmd=["ffmpeg","-hide_banner","-ss",stime,"-y","-i",mfile,"-avoid_negative_ts","make_zero","-map","0","-metadata",tit,"-metadata",artt,"-metadata",trno,otfl]
if ext!='.flac':
cmd=["ffmpeg","-hide_banner","-ss",stime,"-y","-i",mfile,"-t",diff,"-avoid_negative_ts","make_zero","-c","copy","-metadata",tit,"-metadata",artt,"-metadata",trno,otfl]
else:
cmd=["ffmpeg","-hide_banner","-ss",stime,"-y","-i",mfile,"-t",diff,"-map","0","-metadata",tit,"-metadata",artt,"-metadata",trno,otfl]
aa=subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
cimgad=['ffmpeg','-hide_banner','-y','-i',otfl,'-i',asmodeus,'-map','0:a','-map','1','-codec','copy','-metadata:s:v','title="Album cover"','-metadata:s:v','comment="Cover (front)"','-disposition:v','attached_pic',otfl_fn]
aimgad=subprocess.run(cimgad, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
os.remove(otfl)
if not arg:
os.remove(asmodeus)
else:
print("\nAudio file not found.")
if not treatgm:
print("\nNo CUE file found.")
if __name__=='__main__':
main()