Skip to content

Instantly share code, notes, and snippets.

@sophana
Last active April 17, 2022 19:41
Show Gist options
  • Save sophana/c49c76fd57d10fb3fe0fbf1564127fb7 to your computer and use it in GitHub Desktop.
Save sophana/c49c76fd57d10fb3fe0fbf1564127fb7 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):
lrc=file(lrcPath)
srt=file(srtPath,"w")
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 >>srt, n
print >>srt, prevDate,'-->',srtDate
print >>srt, prevStr
print >>srt
n=n+1
prevDate=srtDate
prevStr=s
def walkLrc(p='.'):
for root, dir, 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])
@jilljenn
Copy link

Thanks! I made a Python 3 version out of your code: https://gist.github.com/jilljenn/f5ca41d0c51f693d0b82229654a7cfe3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment