Skip to content

Instantly share code, notes, and snippets.

@jilljenn
Forked from sophana/lrc2srt.py
Created April 17, 2022 19:40
Show Gist options
  • Save jilljenn/f5ca41d0c51f693d0b82229654a7cfe3 to your computer and use it in GitHub Desktop.
Save jilljenn/f5ca41d0c51f693d0b82229654a7cfe3 to your computer and use it in GitHub Desktop.
Convert all LRC files to SRT. With SRT files, you can display lyrics in most media players.
#!/usr/bin/python
import sys
import os
# usage: lrc2srt.py [directory]
# will recusively convert all lrc files to srt in directory
def conv(lrcPath, srtPath):
with open(lrcPath) as lrc:
with open(srtPath, "w") as srt:
prevDate = None
n = 1
for i in lrc.readlines():
if ']' not in i:
continue
d, s = i.split(']')
mm, sec = d[1:].split(':')
s = s.strip()
secInt, secCent = sec.split('.')
srtDate = '00:%s:%s,%s0' % (mm, secInt, secCent)
if prevDate is not None and prevDate != srtDate and prevStr:
print(n, file=srt)
print(prevDate, '-->', srtDate, file=srt)
print(prevStr, file=srt)
print(file=srt)
n = n+1
prevDate = srtDate
prevStr = s
def walkLrc(p='.'):
for root, _, files in os.walk(p):
for f in files:
if not f.endswith('.lrc'):
continue
p = os.path.join(root, f)
p2 = p[:-4]+'.srt'
print(p, p2)
conv(p, p2)
walkLrc(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment