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.
13 # The Sub class, tha handle subtitle
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
) ):
35 # A variable to store subtitle text
38 # A variable to store a start time of visibility of subtitle (in ns).
41 # A variable to store a end time of visibility of subtitle (in ns).
44 # A array of attributes of subtitle. (NOT USED YET)
46 #==============================================================================
49 # Class for subtitles stuff (load, save, get...)
58 #==============================================================================
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
)
66 DATA
=os
.read(FILE
,FS
.st_size
)
69 self
._subSRTLoadFromString
(DATA
)
73 #==============================================================================
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
)
82 for i
in self
.subKeys
:
83 SUB
= self
.subs
[int(i
)]
85 Hour
, Min
, Sec
, MSec
= self
._subTime
2SRTtime
(SUB
.start_time
)
86 Text
+="%02d:%02d:%02d,%03d"%(Hour
, Min
, Sec
, MSec
)
88 Hour
, Min
, Sec
, MSec
= self
._subTime
2SRTtime
(SUB
.end_time
)
89 Text
+="%02d:%02d:%02d,%03d"%(Hour
, Min
, Sec
, MSec
)+"\r\n"
91 if (SUB
.text
[-2]!="\r\n"):
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
):
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")
120 DATA
=string
.split(DATA
,"\r\n")
135 Text
=Text
+DATA
[i
]+"\n"
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])
146 self
.subs
[int(ST
)]=TS
149 #==============================================================================
151 # Delete subtitle from subtitles array.
152 # \param time - key of subtitle in "subs" list.
153 def subDel(self
, time
):
157 #==============================================================================
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):
170 self
.subs
[int(STime
)]=TS
174 #==============================================================================
175 ## Update keys array.
176 # Update array of "subs" keys.
177 def updateKeys(self
):
178 self
.subKeys
=self
.subs
.keys()
181 #==============================================================================
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 #==============================================================================
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
:
199 if(self
.subs
[i
].isInTime(time
)==1):
206 # List of loaded subtitles.
209 # The source of subtitle
212 # Array of "subs" keys (hashs)