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

Source Code for Module Subtitles.Softni

  1  #!/usr/bin/env python 
  2  # 
  3  #       Softni.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   
 23  # This implementation is on real BAD alpha stage and should not be 
 24  # considered by any chance ready for production. 
 25  # A lot of study is still needed to transform the frames into timestamp 
 26  # since this format is a bit funky. 
 27   
 28  import os 
 29  import string 
 30  import re 
 31  import codecs 
 32   
 33  # This is not the best option since we rely on Linux-only 
 34  # Make use of file command to check on the file type 
 35  try: 
 36      import magic 
 37  except: 
 38      print "We need python-magic, otherwise, this format will not be \ 
 39      supported" 
 40      sys.exit(1) 
 41   
 42  from random import randint 
 43   
 44  from Subtitles import Subtitles 
 45  from Sub import * 
 46   
 47  FRAMERATE=25.00 
 48   
49 -def discover(file):
50 """ 51 Every subtitle should have a discover function 52 and return true if it should handle the requested 53 file. 54 """ 55 56 m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME) 57 status = m.load() 58 59 if m.file(file).split('/')[0] == "text": 60 # Open file and read it 61 fd = open(file, "r") 62 data = fd.read() 63 fd.close() 64 else: 65 return 66 67 # Test for SubRip by matching the header 68 rawstr = r"""^(?P<sub>.*\r?\n)*? 69 ^(?P<ts_from>\d{2}:\d{2}:\d{2}.\d{2})\\(?P<ts_to>\d{2}:\d{2}:\d{2}.\d{2})""" 70 71 regex = re.compile(rawstr, re.MULTILINE| re.VERBOSE) 72 73 if regex.search(data): 74 return True 75 return
76
77 -class Softni(Subtitles):
78 """ 79 This class handles the Softni file format 80 """
81 - def __init__(self, filename):
82 Subtitles.__init__(self,filename) 83 84 # Set the file encoding 85 m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME) 86 status = m.load() 87 self.encoding = m.file(filename).split('/')[1].split('=')[1] 88 89 self.subType="Softni" 90 91 self._loadFromFile(filename) 92 return
93
94 - def _loadFromFile(self, file):
95 """ 96 Parse and load the subtitle using a string 97 as input 98 """ 99 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) 100 101 # We reopen the file here so we can 102 # iterate over the lines 103 fd = codecs.open(file, "r", self.encoding) 104 str = fd.readlines() 105 fd.close() 106 107 # Lets set the data structure like we need it 108 info = [] 109 buffer = "" 110 for line in str: 111 if regex.search(line): 112 info.append(tuple([buffer] + line.split('\\'))) 113 buffer="" 114 else: 115 buffer+=line 116 117 # Iterate all the subs and create the 118 # sub objects 119 sub_count = 0 120 for sub in info: 121 text = sub[0] 122 stime = sub[1] 123 etime = sub[2] 124 TS = Sub(text) 125 TS.start_time = self._softniFormat2Timestamp(stime) 126 TS.end_time = self._softniFormat2Timestamp(etime) 127 TS.start_frame = self._softniFormat2Frame(stime) 128 TS.end_frame = self._softniFormat2Frame(etime) 129 TS.number = sub_count 130 sub_count += 1 131 self.subs[int(self._softniFormat2Timestamp(stime))]=TS 132 self.updateKeys() 133 return
134
135 - def _softniFormat2Frame(self, softniFormat):
136 """ 137 Convert Softni frame format to cumulative frame counting 138 """ 139 frames = ((float(softniFormat[0:2])*60*60) + \ 140 (float(softniFormat[3:5])*60) + \ 141 float(softniFormat[6:8]) * FRAMERATE) + \ 142 float(softniFormat[9:11]) 143 return frames
144
145 - def _softniFormat2Timestamp(self, softniFormat):
146 """ 147 Convert Softni frame format to cumulative frame counting 148 """ 149 timestamp = (float(softniFormat[0:2])*60*60) + \ 150 (float(softniFormat[3:5])*60) + \ 151 float(softniFormat[6:8]) + \ 152 (float(softniFormat[9:11])/FRAMERATE) 153 return timestamp*1000
154