1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import os
29 import string
30 import re
31 import codecs
32
33
34
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
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
61 fd = open(file, "r")
62 data = fd.read()
63 fd.close()
64 else:
65 return
66
67
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
78 """
79 This class handles the Softni file format
80 """
82 Subtitles.__init__(self,filename)
83
84
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
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
102
103 fd = codecs.open(file, "r", self.encoding)
104 str = fd.readlines()
105 fd.close()
106
107
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
118
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
144
154