Skip to content

Instantly share code, notes, and snippets.

@Pastez
Created December 14, 2014 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Pastez/498329b607dc6be11730 to your computer and use it in GitHub Desktop.
Save Pastez/498329b607dc6be11730 to your computer and use it in GitHub Desktop.
Converts TXT (MicroDVD, MPL2) subtitles to SRT (SubRip) format. Movie FPS is detected using mplayer
#!/usr/bin/python
import sys, subprocess, getopt, math
import re, os
FORMAT_MICRO_DVD = "}"
FORMAT_MPL2 = "]"
MPLAYER_PATH = "/Applications/MPlayerX.app/Contents/Resources/MPlayerX.mplayer.bundle/Contents/Resources/x86_64/mplayer"
MOVIE_FORMATS = ['avi', 'mp4']
def addZero(ts):
if len(ts) < 2:
ts = '0' + ts
return ts
def secondsToTimeformat(s):
output = '';
h = int(math.floor(s / 3600))
s -= h * 3600
output += addZero(str(h)) + ":"
m = int(math.floor(s / 60))
s -= m * 60
output += addZero(str(m)) + ":"
ss = int(math.floor(s))
s -= ss
output += addZero(str(ss)) + ',' + str(s)[2:5]
return output
def getMovieFPS(moviePath):
pattern = re.compile(r'(\d{2}.\d{3}) fps')
mplayerOutput = subprocess.Popen((MPLAYER_PATH, "-identify", "-frames", "0", "o-ao", "null", moviePath), stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
fps = pattern.search(mplayerOutput).groups()[0]
return fps
def getMoviePath(inputfile):
currentDir = os.path.dirname(os.path.abspath(inputfile))
for fileName in os.listdir(currentDir):
ext = fileName[-3:]
if any(ext in s for s in MOVIE_FORMATS):
return currentDir + "/" + fileName
return None
def main(argv):
if len(argv) == 0:
print "pasTxtSrt.py <input_file>"
return
inputfile = argv[0]
movieFile = getMoviePath(inputfile)
if movieFile is not None:
fps = float(getMovieFPS(movieFile))
print "movie file:" + movieFile
print "fps detected:" + str(fps)
else:
fps = 23.978
print 'fps not detected usind default: ' + str(fps)
print 'input file: ' + inputfile
fileContent = open( inputfile, "r" )
format = ''
srtOutput = ''
l = 0;
intro = 1
for line in fileContent:
if len(line) < 7: continue
if len(format) == 0:
if line[0] == '{': format = FORMAT_MICRO_DVD #MicroDVD
elif line[0] == '[': format = FORMAT_MPL2 #MPL2
l += 1
timeBeginSIndex = 1
timeBeginEIndex = line.find(format)
timeEndSIndex = timeBeginEIndex + 2
timeEndEIndex = line.find(format, timeEndSIndex)
text = line[timeEndEIndex+1:]
text = text.replace('|','\n')
frameStart = float(line[timeBeginSIndex:timeBeginEIndex])
frameEnd = float(line[timeEndSIndex:timeEndEIndex])
secondsStart = 0.0
secondsEnd = 0.0
if format == FORMAT_MICRO_DVD:
secondsStart = frameStart / fps
secondsEnd = frameEnd / fps
reBold = re.compile(r'(?i){Y:b}')
reUnderline = re.compile(r'(?i){Y:u}')
reItalic = re.compile(r'(?i){Y:i}')
if reBold.search(text) is not None:
text = re.sub("(?i){Y:b}","<b>", text)
text = text[:-2] + '</b>\n'
if reUnderline.search(text) is not None:
text = re.sub("(?i){Y:u}","<u>", text)
text = text[:-2] + '</u>\n'
if reItalic.search(text) is not None:
text = re.sub("(?i){Y:i}","<i>", text)
text = text[:-2] + '</i>\n'
elif format == FORMAT_MPL2:
secondsStart = frameStart / 10
secondsEnd = frameEnd / 10
text = text.replace('/','<i>',len(text))
if intro == 1:
srtOutput += str(l) + '\n'
srtOutput += "00:00:00,00 --> 00:00:03,00" + '\n'
srtOutput += "TXT TO SRT\nDla Ukochanej Asi\nwww.pastez.com\n\n"
l += 1
intro = 0
srtOutput += str(l) + '\n'
srtOutput += secondsToTimeformat(secondsStart) + " --> " + secondsToTimeformat(secondsEnd) + '\n'
srtOutput += text + '\n'
outputFile = open(inputfile[:-3]+'srt', "w")
outputFile.write(srtOutput)
outputFile.close()
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment