ziqiangao commited on
Commit
78dd807
·
1 Parent(s): 011064a

Implemet Lyrics

Browse files
Files changed (3) hide show
  1. LRC.py +1 -1
  2. LRC2SRT.py +82 -0
  3. app.py +22 -3
LRC.py CHANGED
@@ -14,7 +14,7 @@ def safe_read(i, a):
14
  def get_line_info(linein):
15
  # Extract minutes and seconds, handle potential issues with slicing and conversion
16
  minutes = float(linein[1:3])
17
- seconds = float(linein[5:9])
18
  lt = minutes * 60 + seconds
19
  line = ""
20
  i = 10
 
14
  def get_line_info(linein):
15
  # Extract minutes and seconds, handle potential issues with slicing and conversion
16
  minutes = float(linein[1:3])
17
+ seconds = float(linein[4:9])
18
  lt = minutes * 60 + seconds
19
  line = ""
20
  i = 10
LRC2SRT.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import LRC as Parse
3
+
4
+ SRT = []
5
+ LRC = []
6
+
7
+ def safe_read(i, a):
8
+ if i > len(a):
9
+ return None
10
+ else:
11
+ return a[i]
12
+
13
+ def clear():
14
+ global SRT
15
+ SRT = []
16
+
17
+ def convert_time(sec):
18
+ t = ""
19
+ t += str(int(sec//3600)).rjust(2, "0")
20
+ t += ':'
21
+ t += str(int(sec%3600//60)).rjust(2, "0")
22
+ t += ':'
23
+ t += str(int(sec%3600%60//1)).rjust(2, "0")
24
+ t += ','
25
+ t += str(round(sec%3600%60%1*1000)).rjust(3, "0")
26
+ return t
27
+
28
+ def add_chunk(line, start, end, number):
29
+ global SRT
30
+ SRT.append(str(number))
31
+ SRT.append(convert_time(float(start)) + " --> " + convert_time(float(end)))
32
+ SRT.append(line)
33
+ SRT.append("")
34
+
35
+ def load(input: str):
36
+ global LRC
37
+ LRC = input.split("\n")
38
+
39
+ def convert_line(number, chunk):
40
+ m = chunk
41
+ info = Parse.get_line_info(LRC[number])
42
+ if number+1 < len(LRC):
43
+ info1 = Parse.get_line_info(LRC[number+1])
44
+ print(info)
45
+ tmp = ""
46
+ i = 0
47
+ if not info.get("wordbreaks"):
48
+ add_chunk(info.get('line'),info.get('time'),info1.get('time'),m)
49
+ return
50
+ tmp = info.get('line')[0:info.get('wordbreaks')[0]-1]
51
+ if tmp:
52
+ add_chunk(tmp,info.get('time'),info.get('wordtimes')[0],m)
53
+ while i <= len(info.get('wordtimes')) - 1:
54
+ if tmp:
55
+ m += 1
56
+
57
+ if i+1 > len(info.get('wordtimes')):
58
+ add_chunk(info.get('line'),info.get('wordtimes')[i],info1.get('time'),m)
59
+ else:
60
+ if i+1 < len(info.get('wordbreaks')):
61
+ tmp = info.get('line')[0:info.get('wordbreaks')[i+1]-1]
62
+ add_chunk(tmp,info.get('wordtimes')[i],info.get('wordtimes')[i+1],m)
63
+ i += 1
64
+
65
+ def convert_to_srt(ly):
66
+ load(Parse.remove_metadata(ly))
67
+ i = 0
68
+ e = 1
69
+ while i < len(LRC):
70
+ convert_line(i,e)
71
+ i += 1
72
+ e = int(SRT[len(SRT)-4])+1
73
+
74
+ if __name__ == "__main__":
75
+ kk = open(".lrc", encoding="UTF8")
76
+
77
+ convert_to_srt(kk.read())
78
+
79
+ print('\n'.join(SRT))
80
+ if os.path.exists("out.srt"):
81
+ os.remove("out.srt")
82
+ open("out.srt", mode='x', encoding='UTF8').write('\n'.join(SRT))
app.py CHANGED
@@ -15,6 +15,8 @@ import subprocess
15
  import traceback
16
  import time
17
  import shutil
 
 
18
 
19
  path = "" # Update with your path
20
 
@@ -156,16 +158,33 @@ def RenderVid(af, n, fps=30):
156
  )
157
  gr.Interface.download(f"{n}.mp4")
158
 
159
- def main(file, name, fps=30, res: tuple=(1280,720), oscres=512, sr=11025):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  os.makedirs(path+f'out/{name}/', exist_ok=True)
161
  global iii
162
  iii = 0
163
  # Load the audio file
 
164
  audio_path = file
165
  y, sr = librosa.load(audio_path, sr=sr) # Resample to 11025 Hz
166
  y_u8 = (y * 128 + 128).astype('uint8')
167
  samples_array = y_u8.tolist()
168
-
169
  # Extract cover image, title, and artist
170
  cover_img = extract_cover_image(audio_path)
171
  if cover_img is None:
@@ -187,7 +206,7 @@ def main(file, name, fps=30, res: tuple=(1280,720), oscres=512, sr=11025):
187
 
188
  # Prepare parameters for each frame
189
  params = [(n, samples_array, cover_img, title, artist, dominant_color, width, height, fps, name, oscres, sr) for n in range(num_frames)]
190
- p = gr.Progress()
191
  try:
192
  with Pool(cpu_count()) as pool:
193
 
 
15
  import traceback
16
  import time
17
  import shutil
18
+ import LRC
19
+ import LRC2SRT
20
 
21
  path = "" # Update with your path
22
 
 
158
  )
159
  gr.Interface.download(f"{n}.mp4")
160
 
161
+ def main(file, name, fps=30, res: tuple=(1280,720), oscres=512, sr=11025, lyrics=None):
162
+ p = gr.Progress()
163
+ if os.path.exists("out.srt"):
164
+ os.remove("out.srt")
165
+ if lyrics:
166
+ p(0.5,"parsing lyrics")
167
+ try:
168
+ sf = open(lyrics, encoding="UTF8").read()
169
+ if sf[1] == '[':
170
+ rr = LRC.convert_to_json(lyrics)
171
+ elif sf[1].isdigit():
172
+ outf = open("out.srt",mode="x", encoding="UTF8").write()
173
+ else:
174
+ gr.Warning("Lyrics file is invalid, skipping")
175
+ except:
176
+ gr.Warning("Failed to parse lyrics, skipping")
177
+
178
  os.makedirs(path+f'out/{name}/', exist_ok=True)
179
  global iii
180
  iii = 0
181
  # Load the audio file
182
+ p(0.25,"loading file")
183
  audio_path = file
184
  y, sr = librosa.load(audio_path, sr=sr) # Resample to 11025 Hz
185
  y_u8 = (y * 128 + 128).astype('uint8')
186
  samples_array = y_u8.tolist()
187
+ p(0.5,"extracting metadata")
188
  # Extract cover image, title, and artist
189
  cover_img = extract_cover_image(audio_path)
190
  if cover_img is None:
 
206
 
207
  # Prepare parameters for each frame
208
  params = [(n, samples_array, cover_img, title, artist, dominant_color, width, height, fps, name, oscres, sr) for n in range(num_frames)]
209
+
210
  try:
211
  with Pool(cpu_count()) as pool:
212