Created
October 16, 2011 12:40
-
-
Save gorlum0/1290835 to your computer and use it in GitHub Desktop.
Convert xml subtitles to srt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import os | |
import datetime as dt | |
import glob | |
from BeautifulSoup import BeautifulSoup | |
def xml2srt(pathname): | |
root, _ = os.path.splitext(pathname) | |
if os.path.exists('%s.srt' % root): | |
return | |
with open(pathname) as fin: | |
text = fin.read() | |
text = text.replace('-1:', '00:') # broken xml | |
soup = BeautifulSoup(text) | |
ps = soup.findAll('p') | |
with open('%s.srt' % root, 'w') as fout: | |
for i, p in enumerate(ps): | |
begin = p['begin'] | |
timestamp, seconds = begin[:5], begin[6:] # begin.rsplit(':', 1) | |
begin = dt.datetime.strptime(timestamp, '%H:%M') + \ | |
dt.timedelta(0, 0, float(seconds) * 1e6) | |
dur = p['dur'] | |
timestamp, seconds = dur[:5], dur[6:] # dur.rsplit(':', 1) | |
dur = dt.datetime.strptime(timestamp, '%H:%M') + \ | |
dt.timedelta(0, 0, float(seconds) * 1e6) | |
dur = dt.timedelta(0, dur.hour, | |
(dur.minute*60 + dur.second) * 1e6 + dur.microsecond) | |
end = begin + dur | |
begin = '%s,%03d' % (begin.strftime('%H:%M:%S'), begin.microsecond//1000) | |
end = '%s,%03d' % (end.strftime('%H:%M:%S'), end.microsecond//1000) | |
print >>fout, i+1 | |
print >>fout, '%s --> %s' % (begin, end) | |
print >>fout, p.text | |
print >>fout | |
if __name__ == '__main__': | |
for pathname in sys.argv[1:]: | |
xml2srt(pathname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment