#!/usr/bin/env python # From http://www.dwerg.net/projects/python/srttool.py # Converts SubRip Scripts into CMML # More information at http://trac.annodex.net/wiki/CmmlSubtitles ## Copyright (C) 2004- Commonwealth Scientific and Industrial Research ## Organisation (CSIRO) Australia ## ## Redistribution and use in source and binary forms, with or without ## modification, are permitted provided that the following conditions ## are met: ## ## - Redistributions of source code must retain the above copyright ## notice, this list of conditions and the following disclaimer. ## ## - Redistributions in binary form must reproduce the above copyright ## notice, this list of conditions and the following disclaimer in the ## documentation and/or other materials provided with the distribution. ## ## - Neither the name of CSIRO Australia nor the names of its ## contributors may be used to endorse or promote products derived from ## this software without specific prior written permission. ## ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ## ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A ## PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ## PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ## LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ## NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ## SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import re global savecmml timeRe = re.compile('(?P\d\d):(?P\d\d):(?P\d\d),(?P\d\d\d)') class CorruptSrtError(Exception): """ The SubRip File is incorrectly formatted """ pass def timeformat(times): return "%02d:%02d:%02d.%03d" % tuple(times) def get_times(line): parts = line.split('-->') try: time1 = timeRe.match(parts[0].strip()) time2 = timeRe.match(parts[1].strip()) timear1 = [ int(time1.groupdict()['hours']), int(time1.groupdict()['minutes']), int(time1.groupdict()['seconds']), int(time1.groupdict()['millis']), ] timear2 = [ int(time2.groupdict()['hours']), int(time2.groupdict()['minutes']), int(time2.groupdict()['seconds']), int(time2.groupdict()['millis']), ] #print "%s --> %s" % (timeformat(timear1), timeformat(timear2)) return (timeformat(timear1), timeformat(timear2)) except IndexError: raise CorruptSrtError("Corrupt Time Format") def convert_block(lines): global savecmml id='c' + lines[0] (start, end) = get_times(lines[1]) savecmml += '\n' % (id, start, end) savecmml += ' \n' for line in lines[2:]: savecmml += ' ' + line + '\n' savecmml += ' \n' savecmml += '\n' def convert_clips(file): current = [] for line in file: if not line.strip(): convert_block(current) current = [] else: current.append(line.strip()) def convert_srt(file): global savecmml savecmml = '\n' savecmml += '\n' savecmml += ' ' + sys.argv[1] + '\n' savecmml += '\n' convert_clips (file) savecmml += '\n' if __name__ == '__main__': global savecmml import sys try: try: filesave = open(sys.argv[2], 'w') convert_srt(file(sys.argv[1])) filesave.write(savecmml) filesave.close() except IndexError: convert_srt(file(sys.argv[1])) print savecmml except IndexError: print "Usage: %s [Input SubRip File] [Output CMML File]" % sys.argv[0]