Small code reduction in oc_vlc_mv_comp_unpack().
[xiph/unicode.git] / souffleur / Subtitles.py
blob37382568ccf29a5765fbf7109f42db73b95d7bdd
1 ## \file Subtitles.py
2 # Documentation for subtitles module of Souffleur project.
3 # \author Maxim Litvinov (aka DarakuTenshi) <otaky@ukr.net>
4 # \todo Add support of different subtitles format.
6 import os
7 import string
9 SUB_NONE=0
10 SUB_SRT=1
12 ## Sub class.
13 # The Sub class, tha handle subtitle
14 class Sub:
16 ## Constructor
17 def __init__(self):
18 self.text=""
19 self.start_time=None
20 self.end_time=None
21 self.Attributes=None
23 #==============================================================================
24 ## Check subtitle time.
25 # This function check if subtitle visibility in given time.
26 # \param[in] time - time to check
27 # \return 1 - if visibility in time, 0 - otherwise
28 def isInTime(self, time):
29 if( (time>=self.start_time) and (time<=self.end_time) ):
30 return 1
31 else:
32 return 0
34 ## \var text
35 # A variable to store subtitle text
37 ## \var start_time
38 # A variable to store a start time of visibility of subtitle (in ns).
40 ## \var end_time
41 # A variable to store a end time of visibility of subtitle (in ns).
43 ## \var Attributes
44 # A array of attributes of subtitle. (NOT USED YET)
46 #==============================================================================
48 ## Subtitles calss
49 # Class for subtitles stuff (load, save, get...)
50 class Subtitles:
52 ## Constructor
53 def __init__(self):
54 self.subs={}
55 self.subSource=None
56 self.subKeys=[]
58 #==============================================================================
59 ## Load subtitles.
60 # Load subtitles from file whith associated stream ID.
61 # \param fileName - name of subtitles file.
62 # \param ID - stream ID.
63 def subLoad(self, fileName, ID):
64 FILE=os.open(fileName, os.O_RDONLY)
65 FS=os.fstat(FILE)
66 DATA=os.read(FILE,FS.st_size)
67 os.close(FILE)
69 self._subSRTLoadFromString(DATA)
71 self.subSource=ID
73 #==============================================================================
74 ## Save subtitles.
75 # Save subtitles to the file.
76 # \param FN - file name.
77 # \param format - the store format of subtitles. (NOT USED YET)
78 def subSave(self, FN, format):
79 if (self.subSource!=None):
80 FUN=os.open(FN,os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
81 N=1
82 for i in self.subKeys:
83 SUB = self.subs[int(i)]
84 Text=str(N)+"\r\n"
85 Hour, Min, Sec, MSec = self._subTime2SRTtime(SUB.start_time)
86 Text+="%02d:%02d:%02d,%03d"%(Hour, Min, Sec, MSec)
87 Text+=" --> "
88 Hour, Min, Sec, MSec = self._subTime2SRTtime(SUB.end_time)
89 Text+="%02d:%02d:%02d,%03d"%(Hour, Min, Sec, MSec)+"\r\n"
90 Text+=SUB.text+"\r\n"
91 if (SUB.text[-2]!="\r\n"):
92 Text+="\r\n"
93 os.write(FUN, Text)
94 N+=1
95 os.close(FUN)
97 #==============================================================================
98 ## Convert subtitle time to SRT format.
99 # Convert subtitle time for saving in SRT subtitles file.
100 # \param time - subtitle time.
101 # \return list of: hour, minute, second and milisecond
102 def _subTime2SRTtime(self, time):
103 tTime = time
104 MSec = tTime%1000
105 tTime /=1000
106 Sec = tTime%60
107 tTime /= 60
108 Min = tTime%60
109 Hour = tTime/60
110 return Hour, Min, Sec, MSec
112 #==============================================================================
113 ## Load SRT formated subtitles.
114 # Load SRT formated subtitles from given string.
115 # \param DATA - string of SRT subtitles.
116 def _subSRTLoadFromString(self, DATA):
117 if (string.find(DATA, "\r\n")==-1):
118 DATA=string.split(DATA,"\n")
119 else:
120 DATA=string.split(DATA,"\r\n")
122 while(i<len(DATA)):
123 if(i>=len(DATA)):
124 break
125 N = DATA[i]
126 i+=1
127 if(i>=len(DATA)):
128 break
129 Timing = DATA[i]
130 Text="";
131 i+=1
132 if(i>=len(DATA)):
133 break
134 while(DATA[i]!=""):
135 Text=Text+DATA[i]+"\n"
136 i+=1
137 i+=1
138 Text=Text[0:-1]
139 ST=int(Timing[0:2])*3600000+int(Timing[3:5])*60000+int(Timing[6:8])*1000+int(Timing[9:12])
140 ET=int(Timing[17:19])*3600000+int(Timing[20:22])*60000+int(Timing[23:25])*1000+int(Timing[26:29])
142 TS=Sub()
143 TS.text=Text
144 TS.start_time=ST
145 TS.end_time=ET
146 self.subs[int(ST)]=TS
147 self.updateKeys()
149 #==============================================================================
150 ## Delete subtitle.
151 # Delete subtitle from subtitles array.
152 # \param time - key of subtitle in "subs" list.
153 def subDel(self, time):
154 del self.subs[time]
155 self.updateKeys()
157 #==============================================================================
158 ## Add subtitle.
159 # Add subtitle to the "subs" list.
160 # \param STime - start time of the subtitle.
161 # \param ETime - end time of the subtitle.
162 # \param Attrs - attributes of the subtitle.
163 # \param isUpdate - to update (or not) keys array of "subs" list.
164 def subAdd(self, STime, ETime, Text, Attrs, isUpdate=0):
165 TS=Sub()
166 TS.text=Text
167 TS.start_time=STime
168 TS.end_time=ETime
169 TS.Attributes=Attrs
170 self.subs[int(STime)]=TS
171 if isUpdate==1:
172 self.updateKeys()
174 #==============================================================================
175 ## Update keys array.
176 # Update array of "subs" keys.
177 def updateKeys(self):
178 self.subKeys=self.subs.keys()
179 self.subKeys.sort()
181 #==============================================================================
182 ## Update subtitle.
183 # Update subtitle key.
184 # \param upSubKey - subtitle to update.
185 def subUpdate(self, upSubKey):
186 Sub = self.subs[upSubKey]
187 self.subDel(upSubKey)
188 self.subAdd(Sub.start_time, Sub.end_time, Sub.text, Sub.Attributes, 1)
190 #==============================================================================
191 ## Get subtitle.
192 # Get subtitle with given time of visibility.
193 # \param time - time of requested subtitle.
194 # \return subtitle or "None".
195 def getSub(self, time):
197 for i in self.subKeys:
198 if(time>=i):
199 if(self.subs[i].isInTime(time)==1):
200 return self.subs[i]
201 else:
202 return None
203 return None
205 ## \var subs
206 # List of loaded subtitles.
208 ## \var subSource
209 # The source of subtitle
211 ## \var subKeys
212 # Array of "subs" keys (hashs)