[xiph-commits] r15084 - in trunk/subtle: . Subtitles

jmesquita at svn.xiph.org jmesquita at svn.xiph.org
Sat Jun 28 21:02:19 PDT 2008


Author: jmesquita
Date: 2008-06-28 21:02:17 -0700 (Sat, 28 Jun 2008)
New Revision: 15084

Added:
   trunk/subtle/Subtitles/
   trunk/subtle/Subtitles/Sub.py
   trunk/subtle/Subtitles/SubRip.py
   trunk/subtle/Subtitles/Subtitles.py
   trunk/subtle/Subtitles/__init__.py
Removed:
   trunk/subtle/Subtitles.py
Modified:
   trunk/subtle/Subtle.py
Log:
* Create a new package to hold all the subtitle handling
* Each new subtitle format will have a new class to handle the format
* Subtitles.py will be the base class and the standard interface for the rest of the application

Added: trunk/subtle/Subtitles/Sub.py
===================================================================
--- trunk/subtle/Subtitles/Sub.py	                        (rev 0)
+++ trunk/subtle/Subtitles/Sub.py	2008-06-29 04:02:17 UTC (rev 15084)
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+#
+#       Sub.py
+#       
+#       Copyright 2008 Joao Mesquita <jmesquita at gmail.com>
+#       
+#       This program is free software; you can redistribute it and/or modify
+#       it under the terms of the GNU General Public License as published by
+#       the Free Software Foundation; either version 3 of the License, or
+#       (at your option) any later version.
+#       
+#       This program is distributed in the hope that it will be useful,
+#       but WITHOUT ANY WARRANTY; without even the implied warranty of
+#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#       GNU General Public License for more details.
+#       
+#       You should have received a copy of the GNU General Public License
+#       along with this program; if not, write to the Free Software
+#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+#       MA 02110-1301, USA.
+
+
+class Sub:
+    """
+        The Sub class, is the class that handles each subtitle 
+        individually
+    """
+
+    def __init__(self):
+        """
+            Lets init all the variables
+            This might work more or less like a C struct
+        """
+        self.text=""
+        self.start_time=None
+        self.end_time=None
+        self.Attributes=None
+        self.number=None
+
+    ## Check subtitle time.
+    # This function check if subtitle visibility in given time.
+    # \param[in] time - time to check
+    # \return 1 - if visibility in time, 0 - otherwise
+    def isInTime(self, time):
+        if( (time>=self.start_time) and (time<=self.end_time) ):
+            return 1
+        else:
+            return 0
+
+    ## \var text
+    # A variable to store subtitle text
+    
+    ## \var start_time
+    # A variable to store a start time of visibility of subtitle (in ns).
+    
+    ## \var end_time
+    # A variable to store a end time of visibility of subtitle (in ns).
+    
+    ## \var Attributes
+    # A array of attributes of subtitle. (NOT USED YET)
+
+#==============================================================================

Added: trunk/subtle/Subtitles/SubRip.py
===================================================================
--- trunk/subtle/Subtitles/SubRip.py	                        (rev 0)
+++ trunk/subtle/Subtitles/SubRip.py	2008-06-29 04:02:17 UTC (rev 15084)
@@ -0,0 +1,121 @@
+#!/usr/bin/env python
+#
+#       SubRip.py
+#       
+#       Copyright 2008 Joao Mesquita <jmesquita at gmail.com>
+#       
+#       This program is free software; you can redistribute it and/or modify
+#       it under the terms of the GNU General Public License as published by
+#       the Free Software Foundation; either version 3 of the License, or
+#       (at your option) any later version.
+#       
+#       This program is distributed in the hope that it will be useful,
+#       but WITHOUT ANY WARRANTY; without even the implied warranty of
+#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#       GNU General Public License for more details.
+#       
+#       You should have received a copy of the GNU General Public License
+#       along with this program; if not, write to the Free Software
+#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+#       MA 02110-1301, USA.
+
+import os
+import string
+
+from Subtitles import Subtitles
+from Sub import *
+
+class SubRip(Subtitles):
+    """
+        This class handles the SubRip subtitle format
+    """
+    
+    ## Load subtitles.
+    # Load subtitles from file.
+    def __init__(self, filename):
+        Subtitles.__init__(self,filename)
+        FILE=os.open(filename, os.O_RDONLY)
+        FS=os.fstat(FILE)
+        DATA=os.read(FILE,FS.st_size)
+        os.close(FILE)
+        
+        self.subType="SubRip"
+
+        self._subSRTLoadFromString(DATA)
+        return
+
+    ## Save subtitles.
+    # Save subtitles to the file.
+    # \param FN - file name.
+    # \param format - the store format of subtitles. (NOT USED YET)
+    def subSave(self, FN, format):
+        FUN=os.open(FN,os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
+        N=1
+        for i in self.subKeys:
+            SUB = self.subs[int(i)]
+            Text=str(N)+"\r\n"
+            Hour, Min, Sec, MSec = self._subTime2SRTtime(SUB.start_time)
+            Text+="%02d:%02d:%02d,%03d"%(Hour, Min, Sec, MSec)
+            Text+=" --> "
+            Hour, Min, Sec, MSec = self._subTime2SRTtime(SUB.end_time)
+            Text+="%02d:%02d:%02d,%03d"%(Hour, Min, Sec, MSec)+"\r\n"
+            Text+=SUB.text+"\r\n"
+            if (SUB.text[-2]!="\r\n"):
+                Text+="\r\n"
+            os.write(FUN, Text)
+            N+=1
+        os.close(FUN)
+        return
+        
+    ## Convert subtitle time to SRT format.
+    # Convert subtitle time for saving in SRT subtitles file.
+    # \param time - subtitle time.
+    # \return list of: hour, minute, second and milisecond
+    def _subTime2SRTtime(self, time):
+        tTime = time
+        MSec = tTime%1000
+        tTime /=1000
+        Sec = tTime%60
+        tTime /= 60
+        Min = tTime%60
+        Hour = tTime/60
+        return Hour, Min, Sec, MSec
+
+    ## Load SRT formated subtitles.
+    # Load SRT formated subtitles from given string.
+    # \param DATA - string of SRT subtitles.
+    def _subSRTLoadFromString(self, DATA):
+        num_sub = 0
+        if (string.find(DATA, "\r\n")==-1):
+            DATA=string.split(DATA,"\n")
+        else:
+            DATA=string.split(DATA,"\r\n")
+        i=0
+        while(i<len(DATA)):
+            if(i>=len(DATA)):
+                break
+            N = DATA[i]
+            i+=1
+            if(i>=len(DATA)):
+                break
+            Timing = DATA[i]
+            Text="";
+            i+=1
+            if(i>=len(DATA)):
+                break
+            while(DATA[i]!=""):
+                Text=Text+DATA[i]+"\n"
+                i+=1
+            i+=1
+            Text=Text[0:-1]
+            ST=int(Timing[0:2])*3600000+int(Timing[3:5])*60000+int(Timing[6:8])*1000+int(Timing[9:12])
+            ET=int(Timing[17:19])*3600000+int(Timing[20:22])*60000+int(Timing[23:25])*1000+int(Timing[26:29])
+            
+            TS=Sub()
+            num_sub += 1
+            TS.text=Text
+            TS.start_time=ST
+            TS.end_time=ET
+            TS.number = num_sub
+            self.subs[int(ST)]=TS
+        self.updateKeys()

Added: trunk/subtle/Subtitles/Subtitles.py
===================================================================
--- trunk/subtle/Subtitles/Subtitles.py	                        (rev 0)
+++ trunk/subtle/Subtitles/Subtitles.py	2008-06-29 04:02:17 UTC (rev 15084)
@@ -0,0 +1,110 @@
+#!/usr/bin/env python
+#
+#       Subtitles.py
+#       
+#       Copyright 2008 Joao Mesquita <jmesquita at gmail.com>
+#       
+#       This program is free software; you can redistribute it and/or modify
+#       it under the terms of the GNU General Public License as published by
+#       the Free Software Foundation; either version 3 of the License, or
+#       (at your option) any later version.
+#       
+#       This program is distributed in the hope that it will be useful,
+#       but WITHOUT ANY WARRANTY; without even the implied warranty of
+#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#       GNU General Public License for more details.
+#       
+#       You should have received a copy of the GNU General Public License
+#       along with this program; if not, write to the Free Software
+#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+#       MA 02110-1301, USA.
+
+import os
+import string
+
+# Subtle imports
+from Sub import *
+
+class Subtitles:
+    """
+        This class defines all the interface for the application to handle
+        subtitles and to ease the implementation of new formats
+    """
+    
+    def __init__(self,FN):
+        """
+            Initialize all the attributes needed to handle
+            all types of subtitle formats as well as their manipulation
+        """
+        self.subs={}
+        self.subKeys=[]
+        self.filename = FN
+        # TODO: Support more subtitles types
+        self.subType = None
+        return
+
+    ## Delete subtitle.
+    # Delete subtitle from subtitles array.
+    # \param time - key of subtitle in "subs" list.
+    def subDel(self, time):
+        del self.subs[time]
+        self.updateKeys()
+        
+    ## Add subtitle.
+    # Add subtitle to the "subs" list.
+    # \param STime - start time of the subtitle.
+    # \param ETime - end time of the subtitle.
+    # \param Attrs - attributes of the subtitle.
+    # \param isUpdate - to update (or not) keys array of "subs" list.
+    def subAdd(self, STime, ETime, Text, Attrs, isUpdate=0):
+        TS=Sub()
+        TS.text=Text
+        TS.start_time=STime
+        TS.end_time=ETime
+        TS.Attributes=Attrs
+        self.subs[int(STime)]=TS
+        if isUpdate==1:
+            self.updateKeys()
+
+    ## Update keys array.
+    # Update array of "subs" keys.
+    def updateKeys(self):
+        self.subKeys=self.subs.keys()
+        self.subKeys.sort()
+
+    ## Update sub text.
+    # Update text for sub.
+    def updateText(self, key, text):
+        if key in self.subs.keys():
+            self.subs[key].text = text
+        else:
+            print "Subkey %s not found" % key
+
+    ## Update subtitle.
+    # Update subtitle key.
+    # \param upSubKey - subtitle to update.
+    def subUpdate(self, upSubKey):
+        Sub = self.subs[upSubKey]
+        self.subDel(upSubKey)
+        self.subAdd(Sub.start_time, Sub.end_time, Sub.text, Sub.Attributes, 1)
+
+    ## Get subtitle.
+    # Get subtitle with given time of visibility.
+    # \param time - time of requested subtitle.
+    # \return subtitle or "None".
+    def getSub(self, time):
+        i=0
+        for i in self.subKeys:
+            if(time>=i):
+                if(self.subs[i].isInTime(time)==1):
+                    return self.subs[i]
+            else:
+                return None
+        return None
+
+
+    ## Get subtitle supported types.
+    # Get subtitle supported types
+    # \return supported subtitle types 
+    def getSupportedTypes(self):
+        return [".srt"]

Added: trunk/subtle/Subtitles/__init__.py
===================================================================
--- trunk/subtle/Subtitles/__init__.py	                        (rev 0)
+++ trunk/subtle/Subtitles/__init__.py	2008-06-29 04:02:17 UTC (rev 15084)
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+#
+#       __init__.py
+#       
+#       Copyright 2008 Joao Mesquita <jmesquita at gmail.com>
+#       
+#       This program is free software; you can redistribute it and/or modify
+#       it under the terms of the GNU General Public License as published by
+#       the Free Software Foundation; either version 3 of the License, or
+#       (at your option) any later version.
+#       
+#       This program is distributed in the hope that it will be useful,
+#       but WITHOUT ANY WARRANTY; without even the implied warranty of
+#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#       GNU General Public License for more details.
+#       
+#       You should have received a copy of the GNU General Public License
+#       along with this program; if not, write to the Free Software
+#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+#       MA 02110-1301, USA.
+__all__=["Subtitles", "SubRip"]
+

Deleted: trunk/subtle/Subtitles.py
===================================================================
--- trunk/subtle/Subtitles.py	2008-06-29 01:36:13 UTC (rev 15083)
+++ trunk/subtle/Subtitles.py	2008-06-29 04:02:17 UTC (rev 15084)
@@ -1,241 +0,0 @@
-#    This file is part of Subtle
-#
-#    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
-#
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-## \file Subtitles.py
-# Documentation for subtitles module of Souffleur project.
-# \author Maxim Litvinov (aka DarakuTenshi) <otaky at ukr.net>
-# \todo Add support of different subtitles format.
-
-import os
-import string
-
-SUB_NONE=0
-SUB_SRT=1
-
-## Sub class.
-# The Sub class, tha handle subtitle
-class Sub:
-
-    ## Constructor
-    def __init__(self):
-        self.text=""
-        self.start_time=None
-        self.end_time=None
-        self.Attributes=None
-        self.number=None
-
-#==============================================================================
-    ## Check subtitle time.
-    # This function check if subtitle visibility in given time.
-    # \param[in] time - time to check
-    # \return 1 - if visibility in time, 0 - otherwise
-    def isInTime(self, time):
-        if( (time>=self.start_time) and (time<=self.end_time) ):
-            return 1
-        else:
-            return 0
-
-    ## \var text
-    # A variable to store subtitle text
-    
-    ## \var start_time
-    # A variable to store a start time of visibility of subtitle (in ns).
-    
-    ## \var end_time
-    # A variable to store a end time of visibility of subtitle (in ns).
-    
-    ## \var Attributes
-    # A array of attributes of subtitle. (NOT USED YET)
-
-#==============================================================================
-
-## Subtitles calss
-# Class for subtitles stuff (load, save, get...)
-class Subtitles:
-    
-    ## Constructor
-    def __init__(self,FN):
-        self.subs={}
-        self.subKeys=[]
-        self.filename = FN
-        # TODO: Support more subtitles types
-        self.subType = "SubRip"
-
-#==============================================================================
-    ## Load subtitles.
-    # Load subtitles from file whith associated stream ID.
-    # \param fileName - name of subtitles file.
-    def subLoad(self, fileName):
-        FILE=os.open(fileName, os.O_RDONLY)
-        FS=os.fstat(FILE)
-        DATA=os.read(FILE,FS.st_size)
-        os.close(FILE)
-
-        self._subSRTLoadFromString(DATA)
-
-#==============================================================================
-    ## Save subtitles.
-    # Save subtitles to the file.
-    # \param FN - file name.
-    # \param format - the store format of subtitles. (NOT USED YET)
-    def subSave(self, FN, format):
-        FUN=os.open(FN,os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
-        N=1
-        for i in self.subKeys:
-            SUB = self.subs[int(i)]
-            Text=str(N)+"\r\n"
-            Hour, Min, Sec, MSec = self._subTime2SRTtime(SUB.start_time)
-            Text+="%02d:%02d:%02d,%03d"%(Hour, Min, Sec, MSec)
-            Text+=" --> "
-            Hour, Min, Sec, MSec = self._subTime2SRTtime(SUB.end_time)
-            Text+="%02d:%02d:%02d,%03d"%(Hour, Min, Sec, MSec)+"\r\n"
-            Text+=SUB.text+"\r\n"
-            if (SUB.text[-2]!="\r\n"):
-                Text+="\r\n"
-            os.write(FUN, Text)
-            N+=1
-        os.close(FUN)
-
-#==============================================================================
-    ## Convert subtitle time to SRT format.
-    # Convert subtitle time for saving in SRT subtitles file.
-    # \param time - subtitle time.
-    # \return list of: hour, minute, second and milisecond
-    def _subTime2SRTtime(self, time):
-        tTime = time
-        MSec = tTime%1000
-        tTime /=1000
-        Sec = tTime%60
-        tTime /= 60
-        Min = tTime%60
-        Hour = tTime/60
-        return Hour, Min, Sec, MSec
-
-#==============================================================================
-    ## Load SRT formated subtitles.
-    # Load SRT formated subtitles from given string.
-    # \param DATA - string of SRT subtitles.
-    def _subSRTLoadFromString(self, DATA):
-        num_sub = 0
-        if (string.find(DATA, "\r\n")==-1):
-            DATA=string.split(DATA,"\n")
-        else:
-            DATA=string.split(DATA,"\r\n")
-        i=0
-        while(i<len(DATA)):
-            if(i>=len(DATA)):
-                break
-            N = DATA[i]
-            i+=1
-            if(i>=len(DATA)):
-                break
-            Timing = DATA[i]
-            Text="";
-            i+=1
-            if(i>=len(DATA)):
-                break
-            while(DATA[i]!=""):
-                Text=Text+DATA[i]+"\n"
-                i+=1
-            i+=1
-            Text=Text[0:-1]
-            ST=int(Timing[0:2])*3600000+int(Timing[3:5])*60000+int(Timing[6:8])*1000+int(Timing[9:12])
-            ET=int(Timing[17:19])*3600000+int(Timing[20:22])*60000+int(Timing[23:25])*1000+int(Timing[26:29])
-            
-            TS=Sub()
-            num_sub += 1
-            TS.text=Text
-            TS.start_time=ST
-            TS.end_time=ET
-            TS.number = num_sub
-            self.subs[int(ST)]=TS
-        self.updateKeys()
-
-#==============================================================================    
-    ## Delete subtitle.
-    # Delete subtitle from subtitles array.
-    # \param time - key of subtitle in "subs" list.
-    def subDel(self, time):
-        del self.subs[time]
-        self.updateKeys()
-        
-#==============================================================================
-    ## Add subtitle.
-    # Add subtitle to the "subs" list.
-    # \param STime - start time of the subtitle.
-    # \param ETime - end time of the subtitle.
-    # \param Attrs - attributes of the subtitle.
-    # \param isUpdate - to update (or not) keys array of "subs" list.
-    def subAdd(self, STime, ETime, Text, Attrs, isUpdate=0):
-        TS=Sub()
-        TS.text=Text
-        TS.start_time=STime
-        TS.end_time=ETime
-        TS.Attributes=Attrs
-        self.subs[int(STime)]=TS
-        if isUpdate==1:
-            self.updateKeys()
-
-#==============================================================================    
-    ## Update keys array.
-    # Update array of "subs" keys.
-    def updateKeys(self):
-        self.subKeys=self.subs.keys()
-        self.subKeys.sort()
-
-#==============================================================================    
-    ## Update sub text.
-    # Update text for sub.
-    def updateText(self, key, text):
-        if key in self.subs.keys():
-            self.subs[key].text = text
-        else:
-            print "Subkey %s not found" % key
-
-#==============================================================================
-    ## Update subtitle.
-    # Update subtitle key.
-    # \param upSubKey - subtitle to update.
-    def subUpdate(self, upSubKey):
-        Sub = self.subs[upSubKey]
-        self.subDel(upSubKey)
-        self.subAdd(Sub.start_time, Sub.end_time, Sub.text, Sub.Attributes, 1)
-
-#==============================================================================
-    ## Get subtitle.
-    # Get subtitle with given time of visibility.
-    # \param time - time of requested subtitle.
-    # \return subtitle or "None".
-    def getSub(self, time):
-        i=0
-        for i in self.subKeys:
-            if(time>=i):
-                if(self.subs[i].isInTime(time)==1):
-                    return self.subs[i]
-            else:
-                return None
-        return None
-
-    ## \var subs
-    # List of loaded subtitles.
-    
-    ## \var subKeys
-    # Array of "subs" keys (hashs)
-#==============================================================================
-    ## Get subtitle supported types.
-    # Get subtitle supported types
-    # \return supported subtitle types 
-    def getSupportedTypes(self):
-        return [".srt"]

Modified: trunk/subtle/Subtle.py
===================================================================
--- trunk/subtle/Subtle.py	2008-06-29 01:36:13 UTC (rev 15083)
+++ trunk/subtle/Subtle.py	2008-06-29 04:02:17 UTC (rev 15084)
@@ -18,7 +18,7 @@
 
 from GPlayer import VideoWidget
 from GPlayer import GstPlayer
-from Subtitles import Subtitles
+from Subtitles import *
 import sys
 import os
 
@@ -509,10 +509,9 @@
         WND.hide()
 
         extension = os.path.splitext(FN)[1]
-        tmpSub = Subtitles(FN)
-        if extension in tmpSub.getSupportedTypes():
+        if extension == ".srt":
             #TODO: We should improve the way we check subtitles
-            tmpSub.subLoad(FN)
+            tmpSub = SubRip.SubRip(FN)
             self.Subtitle = tmpSub
             self.Subtitles.append(tmpSub)
             self.updateStreamWindow()
@@ -774,6 +773,7 @@
         cur_position = self.player.query_position()[0]
         next_position = cur_position + ( self.spin_seek_value.get_value_as_int()*1000000000 )
         self.player.seek(next_position)
+        self.update_scale_cb()
         return
 
 
@@ -786,6 +786,7 @@
         cur_position = self.player.query_position()[0]
         next_position = cur_position - ( self.spin_seek_value.get_value_as_int()*1000000000 )
         self.player.seek(next_position)
+        self.update_scale_cb()
         return
 
 
@@ -879,7 +880,6 @@
 
 
     def update_scale_cb(self):
-        had_duration = self.p_duration != gst.CLOCK_TIME_NONE
         self.p_position, self.p_duration = self.player.query_position()
         if self.p_duration != self.t_duration:
             self.t_duration = self.p_duration



More information about the commits mailing list