[xiph-commits] r15162 - in trunk/subtle: . Subtitles Subtitles/Formats
jmesquita at svn.xiph.org
jmesquita at svn.xiph.org
Wed Aug 6 22:27:28 PDT 2008
Author: jmesquita
Date: 2008-08-06 22:27:27 -0700 (Wed, 06 Aug 2008)
New Revision: 15162
Added:
trunk/subtle/Subtitles/Formats/
trunk/subtle/Subtitles/Formats/Softni.py
trunk/subtle/Subtitles/Formats/SubRip.py
trunk/subtle/Subtitles/Formats/__init__.py
trunk/subtle/Subtitles/Formats/dlg_softniFramerate.glade
Removed:
trunk/subtle/Subtitles/Softni.py
trunk/subtle/Subtitles/SubRip.py
Modified:
trunk/subtle/Subtitles/Discoverer.py
trunk/subtle/Subtitles/Subtitles.py
trunk/subtle/Subtitles/__init__.py
trunk/subtle/Subtle.py
Log:
Formats package: Changed the place where all the formats are stored. Just add a new .py on the Formats package and you are done implementing a new
format. Proper documentation will be generated on how to implement a new format later on.
Discoverer.py: Has been changed to act as a part of the _very_ simple plugin framework.
Modified: trunk/subtle/Subtitles/Discoverer.py
===================================================================
--- trunk/subtle/Subtitles/Discoverer.py 2008-08-06 20:33:51 UTC (rev 15161)
+++ trunk/subtle/Subtitles/Discoverer.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -19,22 +19,33 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
-import os
-import SubRip
-import Softni
+import os, glob
+from . import Formats
+discover_funcs = []
+
+format_path = os.path.dirname(Formats.__file__)
+modules = glob.glob(os.path.join(format_path, '*.py'))
+for module in modules:
+ module = __import__('Formats.' + os.path.basename(module)[:-3],
+ globals(), locals(), ['discover'], 1)
+ if hasattr(module, 'discover'):
+ discover_funcs.append(module.discover)
+
def discoverer(file):
"""
This procedure will negotiate and return the proper subtitle class to
handle the specific format. If it returns None, format is not yet
supported.
"""
-
- # Test for SubRip
- if SubRip.discover(file):
- return SubRip.SubRip(file)
- # Test for Softni
- if Softni.discover(file):
- return Softni.Softni(file)
-
- return None
+
+ for func in discover_funcs:
+ print "Testing this func: %s" % func
+ handle = func(file)
+ if handle is not None:
+ print "I have found a handle: %s" % handle
+ return handle(file)
+ else:
+ print '----'
+
+
Added: trunk/subtle/Subtitles/Formats/Softni.py
===================================================================
--- trunk/subtle/Subtitles/Formats/Softni.py (rev 0)
+++ trunk/subtle/Subtitles/Formats/Softni.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -0,0 +1,152 @@
+#!/usr/bin/env python
+#
+# Softni.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.
+
+
+# This implementation is on real BAD alpha stage and should not be
+# considered by any chance ready for production.
+# A lot of study is still needed to transform the frames into timestamp
+# since this format is a bit funky.
+
+import os
+import string
+import re
+import codecs
+
+# This is not the best option since we rely on Linux-only
+# Make use of file command to check on the file type
+try:
+ import magic
+except:
+ print "We need python-magic, otherwise, this format will not be \
+ supported"
+ sys.exit(1)
+
+from random import randint
+
+from .. Subtitles import Subtitles
+from .. Subtitles import Sub
+
+FRAMERATE=25.00
+
+def discover(file):
+ """
+ Every subtitle should have a discover function
+ and return true if it should handle the requested
+ file.
+ """
+
+ m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
+ status = m.load()
+
+ if m.file(file).split('/')[0] == "text":
+ # Open file and read it
+ fd = open(file, "r")
+ data = fd.read()
+ fd.close()
+ else:
+ return
+
+ #: Test for Softni by matching the timecode
+ rawstr = r"""^(?P<ts_from>\d{2}:\d{2}:\d{2}.\d{2})\\(?P<ts_to>\d{2}:\d{2}:\d{2}.\d{2})"""
+
+ regex = re.compile(rawstr, re.MULTILINE| re.VERBOSE)
+
+ if regex.search(data):
+ return Softni
+ return
+
+class Softni(Subtitles):
+ """
+ This class handles the Softni file format
+ """
+ def __init__(self, filename):
+ Subtitles.__init__(self,filename)
+
+ # Set the file encoding
+ m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
+ status = m.load()
+ self.encoding = m.file(filename).split('/')[1].split('=')[1]
+
+ self.subType="Softni"
+
+ self._loadFromFile(filename)
+ return
+
+ def _loadFromFile(self, file):
+ """
+ Parse and load the subtitle using a string
+ as input
+ """
+ regex = re.compile(r"""^(?P<ts_from>\d{2}:\d{2}:\d{2}.\d{2})\\(?P<ts_to>\d{2}:\d{2}:\d{2}.\d{2})""", re.MULTILINE)
+
+ # We reopen the file here so we can
+ # iterate over the lines
+ fd = codecs.open(file, "r", self.encoding)
+ contents = fd.readlines()
+ fd.close()
+
+ # Lets set the data structure like we need it
+ info = []
+ buffer = ""
+ for line in contents:
+ if regex.search(line):
+ info.append(tuple([buffer] + line.split('\\')))
+ buffer=""
+ else:
+ buffer+=line
+
+ # Iterate all the subs and create the
+ # sub objects
+ sub_count = 0
+ for sub in info:
+ text = sub[0]
+ stime = sub[1]
+ etime = sub[2]
+ TS = Sub(text)
+ TS.start_time = self._softniFormat2Timestamp(stime)
+ TS.end_time = self._softniFormat2Timestamp(etime)
+ TS.start_frame = self._softniFormat2Frame(stime)
+ TS.end_frame = self._softniFormat2Frame(etime)
+ TS.number = sub_count
+ sub_count += 1
+ self.subs[int(self._softniFormat2Timestamp(stime))]=TS
+ self.updateKeys()
+ return
+
+ def _softniFormat2Frame(self, softniFormat):
+ """
+ Convert Softni frame format to cumulative frame counting
+ """
+ frames = ((float(softniFormat[0:2])*60*60) + \
+ (float(softniFormat[3:5])*60) + \
+ float(softniFormat[6:8]) * FRAMERATE) + \
+ float(softniFormat[9:11])
+ return frames
+
+ def _softniFormat2Timestamp(self, softniFormat):
+ """
+ Convert Softni frame format to cumulative frame counting
+ """
+ timestamp = (float(softniFormat[0:2])*60*60) + \
+ (float(softniFormat[3:5])*60) + \
+ float(softniFormat[6:8]) + \
+ (float(softniFormat[9:11])/FRAMERATE)
+ return timestamp*1000
Added: trunk/subtle/Subtitles/Formats/SubRip.py
===================================================================
--- trunk/subtle/Subtitles/Formats/SubRip.py (rev 0)
+++ trunk/subtle/Subtitles/Formats/SubRip.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -0,0 +1,170 @@
+#!/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
+import re
+import codecs
+
+# This is not the best option since we rely on Linux-only
+# Make use of file command to check on the file type
+try:
+ import magic
+except:
+ print "We need python-magic, otherwise, this format will not be \
+ supported"
+ sys.exit(1)
+
+from .. Subtitles import Subtitles
+from Subtitles.Sub import *
+
+FRAMERATE=25.00
+
+def discover(file):
+ """
+ Every subtitle should have a discover function
+ and return true if it should handle the requested
+ file.
+ """
+
+ m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
+ status = m.load()
+
+ if m.file(file).split('/')[0] == "text":
+ # Open file and read it
+ fd = open(file, "r")
+ data = fd.read()
+ fd.close()
+ else:
+ return
+
+ # Test for SubRip by matching the header
+ rawstr = r"""^(?P<counter>\d+)\s*
+ ^(?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?"""
+ regex = re.compile(rawstr,re.MULTILINE| re.VERBOSE)
+
+ print "BEGIN: Test for SubRip"
+ if regex.search(data):
+ print "END: Test for SubRip"
+ return SubRip
+ return
+
+class SubRip(Subtitles):
+ """
+ This class handles the SubRip subtitle format
+ """
+
+ ## Load subtitles.
+ # Load subtitles from file.
+ def __init__(self, filename):
+ Subtitles.__init__(self,filename)
+
+ # Set the file encoding
+ m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
+ status = m.load()
+ self.encoding = m.file(filename).split('/')[1].split('=')[1]
+
+ FILE = codecs.open(filename, 'r', self.encoding)
+ DATA = FILE.read()
+ FILE.close()
+
+ 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(Text)
+ num_sub += 1
+ #TS.text=Text
+ TS.start_time=ST
+ TS.end_time=ET
+ TS.start_frame=ST*FRAMERATE/1000
+ TS.end_frame=ET*FRAMERATE/1000
+ TS.number = num_sub
+ self.subs[int(ST)]=TS
+ self.updateKeys()
Added: trunk/subtle/Subtitles/Formats/__init__.py
===================================================================
--- trunk/subtle/Subtitles/Formats/__init__.py (rev 0)
+++ trunk/subtle/Subtitles/Formats/__init__.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -0,0 +1,20 @@
+#!/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.
Added: trunk/subtle/Subtitles/Formats/dlg_softniFramerate.glade
===================================================================
--- trunk/subtle/Subtitles/Formats/dlg_softniFramerate.glade (rev 0)
+++ trunk/subtle/Subtitles/Formats/dlg_softniFramerate.glade 2008-08-07 05:27:27 UTC (rev 15162)
@@ -0,0 +1,91 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
+<!--Generated with glade3 3.4.5 on Tue Jul 29 02:14:40 2008 -->
+<glade-interface>
+ <widget class="GtkDialog" id="dlg_SoftniFramerate">
+ <property name="border_width">5</property>
+ <property name="type">GTK_WINDOW_POPUP</property>
+ <property name="modal">True</property>
+ <property name="window_position">GTK_WIN_POS_CENTER_ON_PARENT</property>
+ <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
+ <property name="has_separator">False</property>
+ <child internal-child="vbox">
+ <widget class="GtkVBox" id="dialog-vbox1">
+ <property name="visible">True</property>
+ <property name="spacing">2</property>
+ <child>
+ <widget class="GtkVBox" id="vbox1">
+ <property name="visible">True</property>
+ <child>
+ <widget class="GtkLabel" id="label1">
+ <property name="visible">True</property>
+ <property name="label" translatable="yes">Softni needs a framerate specification!
+Since no movie stream has been opened,
+would you like to choose the framerate to be used?</property>
+ </widget>
+ </child>
+ <child>
+ <widget class="GtkHBox" id="hbox1">
+ <property name="visible">True</property>
+ <child>
+ <widget class="GtkLabel" id="label2">
+ <property name="visible">True</property>
+ <property name="label" translatable="yes">Framerate:</property>
+ </widget>
+ </child>
+ <child>
+ <widget class="GtkEntry" id="en_Framerate">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ </widget>
+ <packing>
+ <property name="position">1</property>
+ </packing>
+ </child>
+ </widget>
+ <packing>
+ <property name="position">1</property>
+ </packing>
+ </child>
+ </widget>
+ <packing>
+ <property name="position">1</property>
+ </packing>
+ </child>
+ <child internal-child="action_area">
+ <widget class="GtkHButtonBox" id="dialog-action_area1">
+ <property name="visible">True</property>
+ <property name="layout_style">GTK_BUTTONBOX_END</property>
+ <child>
+ <widget class="GtkButton" id="btn_OK">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="receives_default">True</property>
+ <property name="label" translatable="yes">gtk-apply</property>
+ <property name="use_stock">True</property>
+ <property name="response_id">0</property>
+ </widget>
+ </child>
+ <child>
+ <widget class="GtkButton" id="btn_Cancel">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="receives_default">True</property>
+ <property name="label" translatable="yes">gtk-cancel</property>
+ <property name="use_stock">True</property>
+ <property name="response_id">0</property>
+ </widget>
+ <packing>
+ <property name="position">1</property>
+ </packing>
+ </child>
+ </widget>
+ <packing>
+ <property name="expand">False</property>
+ <property name="pack_type">GTK_PACK_END</property>
+ </packing>
+ </child>
+ </widget>
+ </child>
+ </widget>
+</glade-interface>
Deleted: trunk/subtle/Subtitles/Softni.py
===================================================================
--- trunk/subtle/Subtitles/Softni.py 2008-08-06 20:33:51 UTC (rev 15161)
+++ trunk/subtle/Subtitles/Softni.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -1,153 +0,0 @@
-#!/usr/bin/env python
-#
-# Softni.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.
-
-
-# This implementation is on real BAD alpha stage and should not be
-# considered by any chance ready for production.
-# A lot of study is still needed to transform the frames into timestamp
-# since this format is a bit funky.
-
-import os
-import string
-import re
-import codecs
-
-# This is not the best option since we rely on Linux-only
-# Make use of file command to check on the file type
-try:
- import magic
-except:
- print "We need python-magic, otherwise, this format will not be \
- supported"
- sys.exit(1)
-
-from random import randint
-
-from Subtitles import Subtitles
-from Sub import *
-
-FRAMERATE=25.00
-
-def discover(file):
- """
- Every subtitle should have a discover function
- and return true if it should handle the requested
- file.
- """
-
- m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
- status = m.load()
-
- if m.file(file).split('/')[0] == "text":
- # Open file and read it
- fd = open(file, "r")
- data = fd.read()
- fd.close()
- else:
- return
-
- # Test for SubRip by matching the header
- rawstr = r"""^(?P<sub>.*\r?\n)*?
- ^(?P<ts_from>\d{2}:\d{2}:\d{2}.\d{2})\\(?P<ts_to>\d{2}:\d{2}:\d{2}.\d{2})"""
-
- regex = re.compile(rawstr, re.MULTILINE| re.VERBOSE)
-
- if regex.search(data):
- return True
- return
-
-class Softni(Subtitles):
- """
- This class handles the Softni file format
- """
- def __init__(self, filename):
- Subtitles.__init__(self,filename)
-
- # Set the file encoding
- m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
- status = m.load()
- self.encoding = m.file(filename).split('/')[1].split('=')[1]
-
- self.subType="Softni"
-
- self._loadFromFile(filename)
- return
-
- def _loadFromFile(self, file):
- """
- Parse and load the subtitle using a string
- as input
- """
- regex = re.compile(r"""^(?P<ts_from>\d{2}:\d{2}:\d{2}.\d{2})\\(?P<ts_to>\d{2}:\d{2}:\d{2}.\d{2})""", re.MULTILINE)
-
- # We reopen the file here so we can
- # iterate over the lines
- fd = codecs.open(file, "r", self.encoding)
- str = fd.readlines()
- fd.close()
-
- # Lets set the data structure like we need it
- info = []
- buffer = ""
- for line in str:
- if regex.search(line):
- info.append(tuple([buffer] + line.split('\\')))
- buffer=""
- else:
- buffer+=line
-
- # Iterate all the subs and create the
- # sub objects
- sub_count = 0
- for sub in info:
- text = sub[0]
- stime = sub[1]
- etime = sub[2]
- TS = Sub(text)
- TS.start_time = self._softniFormat2Timestamp(stime)
- TS.end_time = self._softniFormat2Timestamp(etime)
- TS.start_frame = self._softniFormat2Frame(stime)
- TS.end_frame = self._softniFormat2Frame(etime)
- TS.number = sub_count
- sub_count += 1
- self.subs[int(self._softniFormat2Timestamp(stime))]=TS
- self.updateKeys()
- return
-
- def _softniFormat2Frame(self, softniFormat):
- """
- Convert Softni frame format to cumulative frame counting
- """
- frames = ((float(softniFormat[0:2])*60*60) + \
- (float(softniFormat[3:5])*60) + \
- float(softniFormat[6:8]) * FRAMERATE) + \
- float(softniFormat[9:11])
- return frames
-
- def _softniFormat2Timestamp(self, softniFormat):
- """
- Convert Softni frame format to cumulative frame counting
- """
- timestamp = (float(softniFormat[0:2])*60*60) + \
- (float(softniFormat[3:5])*60) + \
- float(softniFormat[6:8]) + \
- (float(softniFormat[9:11])/FRAMERATE)
- return timestamp*1000
Deleted: trunk/subtle/Subtitles/SubRip.py
===================================================================
--- trunk/subtle/Subtitles/SubRip.py 2008-08-06 20:33:51 UTC (rev 15161)
+++ trunk/subtle/Subtitles/SubRip.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -1,167 +0,0 @@
-#!/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
-import re
-import codecs
-
-# This is not the best option since we rely on Linux-only
-# Make use of file command to check on the file type
-try:
- import magic
-except:
- print "We need python-magic, otherwise, this format will not be \
- supported"
- sys.exit(1)
-
-from Subtitles import Subtitles
-from Sub import *
-
-FRAMERATE=25.00
-
-def discover(file):
- """
- Every subtitle should have a discover function
- and return true if it should handle the requested
- file.
- """
-
- m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
- status = m.load()
-
- if m.file(file).split('/')[0] == "text":
- # Open file and read it
- fd = open(file, "r")
- data = fd.read()
- fd.close()
- else:
- return
-
- # Test for SubRip by matching the header
- rawstr = r"""^(?P<counter>\d+)\s*
- ^(?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?"""
- regex = re.compile(rawstr,re.MULTILINE| re.VERBOSE)
- if regex.search(data):
- return True
- return
-
-class SubRip(Subtitles):
- """
- This class handles the SubRip subtitle format
- """
-
- ## Load subtitles.
- # Load subtitles from file.
- def __init__(self, filename):
- Subtitles.__init__(self,filename)
-
- # Set the file encoding
- m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
- status = m.load()
- self.encoding = m.file(filename).split('/')[1].split('=')[1]
-
- FILE = codecs.open(filename, 'r', self.encoding)
- DATA = FILE.read()
- FILE.close()
-
- 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(Text)
- num_sub += 1
- #TS.text=Text
- TS.start_time=ST
- TS.end_time=ET
- TS.start_frame=ST*FRAMERATE/1000
- TS.end_frame=ET*FRAMERATE/1000
- TS.number = num_sub
- self.subs[int(ST)]=TS
- self.updateKeys()
Modified: trunk/subtle/Subtitles/Subtitles.py
===================================================================
--- trunk/subtle/Subtitles/Subtitles.py 2008-08-06 20:33:51 UTC (rev 15161)
+++ trunk/subtle/Subtitles/Subtitles.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -42,6 +42,7 @@
# TODO: Support more subtitles types
self.subType = None
self.encoding = None
+ self.framerate = None
return
## Delete subtitle.
Modified: trunk/subtle/Subtitles/__init__.py
===================================================================
--- trunk/subtle/Subtitles/__init__.py 2008-08-06 20:33:51 UTC (rev 15161)
+++ trunk/subtle/Subtitles/__init__.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -18,5 +18,5 @@
# 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","Softni","Discoverer","Line"]
+__all__=["Subtitles", "Formats","Discoverer","Line"]
Modified: trunk/subtle/Subtle.py
===================================================================
--- trunk/subtle/Subtle.py 2008-08-06 20:33:51 UTC (rev 15161)
+++ trunk/subtle/Subtle.py 2008-08-07 05:27:27 UTC (rev 15162)
@@ -543,6 +543,9 @@
return True
mInfo = mInfo.getMedia()
if mInfo.MIME:
+ # Set the subtitle framerate
+ if self.Subtitle:
+ self.Subtitle.framerate = mInfo.framerate
self.media.append(mInfo)
self.updateStreamWindow()
#Set videoWidget sizes according to media standards
More information about the commits
mailing list