Package Subtitles :: Module SubRip
[hide private]
[frames] | no frames]

Source Code for Module Subtitles.SubRip

  1  #!/usr/bin/env python 
  2  # 
  3  #       SubRip.py 
  4  #        
  5  #       Copyright 2008 Joao Mesquita <jmesquita@gmail.com> 
  6  #        
  7  #       This program is free software; you can redistribute it and/or modify 
  8  #       it under the terms of the GNU General Public License as published by 
  9  #       the Free Software Foundation; either version 3 of the License, or 
 10  #       (at your option) any later version. 
 11  #        
 12  #       This program is distributed in the hope that it will be useful, 
 13  #       but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  #       GNU General Public License for more details. 
 16  #        
 17  #       You should have received a copy of the GNU General Public License 
 18  #       along with this program; if not, write to the Free Software 
 19  #       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 20  #       MA 02110-1301, USA. 
 21   
 22  import os 
 23  import string 
 24  import re 
 25  import codecs 
 26   
 27  # This is not the best option since we rely on Linux-only 
 28  # Make use of file command to check on the file type 
 29  try: 
 30      import magic 
 31  except: 
 32      print "We need python-magic, otherwise, this format will not be \ 
 33      supported" 
 34      sys.exit(1) 
 35   
 36  from Subtitles import Subtitles 
 37  from Sub import * 
 38   
 39  FRAMERATE=25.00 
 40   
41 -def discover(file):
42 """ 43 Every subtitle should have a discover function 44 and return true if it should handle the requested 45 file. 46 """ 47 48 m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME) 49 status = m.load() 50 51 if m.file(file).split('/')[0] == "text": 52 # Open file and read it 53 fd = open(file, "r") 54 data = fd.read() 55 fd.close() 56 else: 57 return 58 59 # Test for SubRip by matching the header 60 rawstr = r"""^(?P<counter>\d+)\s* 61 ^(?P<ts_from>\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(?P<ts_to>\d{2}:\d{2}:\d{2},\d{3})\r?""" 62 regex = re.compile(rawstr,re.MULTILINE| re.VERBOSE) 63 if regex.search(data): 64 return True 65 return
66
67 -class SubRip(Subtitles):
68 """ 69 This class handles the SubRip subtitle format 70 """ 71 72 ## Load subtitles. 73 # Load subtitles from file.
74 - def __init__(self, filename):
75 Subtitles.__init__(self,filename) 76 77 # Set the file encoding 78 m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME) 79 status = m.load() 80 self.encoding = m.file(filename).split('/')[1].split('=')[1] 81 82 FILE = codecs.open(filename, 'r', self.encoding) 83 DATA = FILE.read() 84 FILE.close() 85 86 self.subType="SubRip" 87 88 self._subSRTLoadFromString(DATA) 89 return
90 91 ## Save subtitles. 92 # Save subtitles to the file. 93 # \param FN - file name. 94 # \param format - the store format of subtitles. (NOT USED YET)
95 - def subSave(self, FN, format):
96 FUN=os.open(FN,os.O_WRONLY|os.O_CREAT|os.O_TRUNC) 97 N=1 98 for i in self.subKeys: 99 SUB = self.subs[int(i)] 100 Text=str(N)+"\r\n" 101 Hour, Min, Sec, MSec = self._subTime2SRTtime(SUB.start_time) 102 Text+="%02d:%02d:%02d,%03d"%(Hour, Min, Sec, MSec) 103 Text+=" --> " 104 Hour, Min, Sec, MSec = self._subTime2SRTtime(SUB.end_time) 105 Text+="%02d:%02d:%02d,%03d"%(Hour, Min, Sec, MSec)+"\r\n" 106 Text+=SUB.text+"\r\n" 107 if (SUB.text[-2]!="\r\n"): 108 Text+="\r\n" 109 os.write(FUN, Text) 110 N+=1 111 os.close(FUN) 112 return
113 114 ## Convert subtitle time to SRT format. 115 # Convert subtitle time for saving in SRT subtitles file. 116 # \param time - subtitle time. 117 # \return list of: hour, minute, second and milisecond
118 - def _subTime2SRTtime(self, time):
119 tTime = time 120 MSec = tTime%1000 121 tTime /=1000 122 Sec = tTime%60 123 tTime /= 60 124 Min = tTime%60 125 Hour = tTime/60 126 return Hour, Min, Sec, MSec
127 128 ## Load SRT formated subtitles. 129 # Load SRT formated subtitles from given string. 130 # \param DATA - string of SRT subtitles.
131 - def _subSRTLoadFromString(self, DATA):
132 num_sub = 0 133 if (string.find(DATA, "\r\n")==-1): 134 DATA=string.split(DATA,"\n") 135 else: 136 DATA=string.split(DATA,"\r\n") 137 i=0 138 while(i<len(DATA)): 139 if(i>=len(DATA)): 140 break 141 N = DATA[i] 142 i+=1 143 if(i>=len(DATA)): 144 break 145 Timing = DATA[i] 146 Text=""; 147 i+=1 148 if(i>=len(DATA)): 149 break 150 while(DATA[i]!=""): 151 Text=Text+DATA[i]+"\n" 152 i+=1 153 i+=1 154 Text=Text[0:-1] 155 ST=int(Timing[0:2])*3600000+int(Timing[3:5])*60000+int(Timing[6:8])*1000+int(Timing[9:12]) 156 ET=int(Timing[17:19])*3600000+int(Timing[20:22])*60000+int(Timing[23:25])*1000+int(Timing[26:29]) 157 158 TS=Sub(Text) 159 num_sub += 1 160 #TS.text=Text 161 TS.start_time=ST 162 TS.end_time=ET 163 TS.start_frame=ST*FRAMERATE/1000 164 TS.end_frame=ET*FRAMERATE/1000 165 TS.number = num_sub 166 self.subs[int(ST)]=TS 167 self.updateKeys()
168