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

Source Code for Module Subtitles.Subtitles

  1  #!/usr/bin/env python 
  2  # 
  3  #       Subtitles.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   
 25  # Subtle imports 
 26  from Sub import * 
 27   
28 -class Subtitles:
29 """ 30 This class defines all the interface for the application to handle 31 subtitles and to ease the implementation of new formats 32 """ 33
34 - def __init__(self,FN):
35 """ 36 Initialize all the attributes needed to handle 37 all types of subtitle formats as well as their manipulation 38 """ 39 self.subs={} 40 self.subKeys=[] 41 self.filename = FN 42 # TODO: Support more subtitles types 43 self.subType = None 44 self.encoding = None 45 self.framerate = None 46 return
47 48 ## Delete subtitle. 49 # Delete subtitle from subtitles array. 50 # \param time - key of subtitle in "subs" list.
51 - def subDel(self, time):
52 del self.subs[time] 53 self.updateKeys()
54 55 ## Add subtitle. 56 # Add subtitle to the "subs" list. 57 # \param STime - start time of the subtitle. 58 # \param ETime - end time of the subtitle. 59 # \param Attrs - attributes of the subtitle. 60 # \param isUpdate - to update (or not) keys array of "subs" list.
61 - def subAdd(self, STime, ETime, Text, Attrs, isUpdate=0):
62 TS=Sub() 63 TS.text=Text 64 TS.start_time=STime 65 TS.end_time=ETime 66 TS.Attributes=Attrs 67 self.subs[int(STime)]=TS 68 if isUpdate==1: 69 self.updateKeys()
70 71 ## Update keys array. 72 # Update array of "subs" keys.
73 - def updateKeys(self):
74 self.subKeys=self.subs.keys() 75 self.subKeys.sort()
76 77 ## Update sub text. 78 # Update text for sub.
79 - def updateText(self, key, text):
80 if key in self.subs.keys(): 81 self.subs[key].text = text 82 else: 83 print "Subkey %s not found" % key
84 85 ## Update subtitle. 86 # Update subtitle key. 87 # \param upSubKey - subtitle to update.
88 - def subUpdate(self, upSubKey):
89 Sub = self.subs[upSubKey] 90 self.subDel(upSubKey) 91 self.subAdd(Sub.start_time, Sub.end_time, Sub.text, Sub.Attributes, 1)
92 93 ## Get subtitle. 94 # Get subtitle with given time of visibility. 95 # \param time - time of requested subtitle. 96 # \return subtitle or "None".
97 - def getSub(self, time):
98 i=0 99 for i in self.subKeys: 100 if(time>=i): 101 if(self.subs[i].isInTime(time)==1): 102 return self.subs[i] 103 else: 104 return None 105 return None
106 107 108 ## Get subtitle supported types. 109 # Get subtitle supported types 110 # \return supported subtitle types
111 - def getSupportedTypes(self):
112 return [".srt"]
113