Skip to content

Instantly share code, notes, and snippets.

@adammw
Created April 12, 2011 09:44
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save adammw/915259 to your computer and use it in GitHub Desktop.
Save adammw/915259 to your computer and use it in GitHub Desktop.
Timed Text Captions to SRT Subtitles converter script
# Usage: python tt2srt.py source.xml output.srt
from xml.dom.minidom import parse
import sys
i=1
dom = parse(sys.argv[1])
out = open(sys.argv[2], 'w')
body = dom.getElementsByTagName("body")[0]
paras = body.getElementsByTagName("p")
for para in paras:
out.write(str(i) + "\n")
out.write(para.attributes['begin'].value.replace('.',',') + ' --> ' + para.attributes['end'].value.replace('.',',') + "\n")
for child in para.childNodes:
if child.nodeName == 'br':
out.write("\n")
elif child.nodeName == '#text':
out.write(unicode(child.data).encode('utf=8'))
out.write("\n\n")
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment