import os import LRC as Parse SRT = [] LRC = [] def safe_read(i, a): if i > len(a): return None else: return a[i] def clear(): global SRT global LRC SRT = [] LRC = [] def convert_time(sec): t = "" t += str(int(sec//3600)).rjust(2, "0") t += ':' t += str(int(sec%3600//60)).rjust(2, "0") t += ':' t += str(int(sec%3600%60//1)).rjust(2, "0") t += ',' t += str(round(sec%3600%60%1*1000)).rjust(3, "0") return t def add_chunk(line, start, end, number): global SRT SRT.append(str(number)) SRT.append(convert_time(float(start)) + " --> " + convert_time(float(end))) SRT.append(line) SRT.append("") def load(input: str): global LRC LRC = input.split("\n") def convert_line(number, chunk): m = chunk info = Parse.get_line_info(LRC[number]) if number+1 < len(LRC): info1 = Parse.get_line_info(LRC[number+1]) else: info1 = None print(info) tmp = "" i = 0 if not info.get("wordbreaks"): add_chunk(info.get('line'),info.get('time'),info1.get('time'),m) return tmp = info.get('line')[0:info.get('wordbreaks')[0]-1] if tmp: add_chunk(tmp,info.get('time'),info.get('wordtimes')[0],m) while i <= len(info.get('wordtimes')) - 1: if tmp: m += 1 if i+1 > len(info.get('wordtimes')): add_chunk(info.get('line'),info.get('wordtimes')[i],info1.get('time'),m) else: if i+1 < len(info.get('wordbreaks')): tmp = info.get('line')[0:info.get('wordbreaks')[i+1]-1] add_chunk(tmp,info.get('wordtimes')[i],info.get('wordtimes')[i+1],m) i += 1 def convert_to_srt(ly): load(Parse.remove_metadata(ly)) i = 0 e = 1 while i < len(LRC): convert_line(i,e) i += 1 e = int(SRT[len(SRT)-4])+1 if __name__ == "__main__": kk = open(".lrc", encoding="UTF8") convert_to_srt(kk.read()) print('\n'.join(SRT)) if os.path.exists("out.srt"): os.remove("out.srt") open("out.srt", mode='x', encoding='UTF8').write('\n'.join(SRT))