forked from hamidrezakp/SubFixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subfixer
executable file
·149 lines (113 loc) · 3.23 KB
/
subfixer
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
# Programmer : Hamid Reza Kaveh PishGhadam <[email protected]>
# Programmer of Base Package : Ali Vakilzade <[email protected]>
# License : GPLv2
import os
import re
import sys
class Main:
def start_fixing(self):
for i in range(0,self.total):
sub_file = self.files[i]
with open(sub_file, 'r') as f:
lines = f.read()
lines = self.subtitle_fixer.decode_string(lines)
os.remove(self.files[i])
with open(sub_file, 'w') as f:
f.write(lines.encode('utf-8'))
def __init__ (self):
# args
self.files = sys.argv[:]
self.files.remove(self.files[0])
self.total = len(self.files)
self.skipped = False
# if no file was selected
if self.total == 0:
exit()
# load subtitle fixer
self.subtitle_fixer = SubtitleFixer()
# fixing files
self.start_fixing()
class SubtitleFixer:
def __init__(self):
self.time = '\d\d:\d\d:\d\d,\d\d\d'
self.number = u'۰۱۲۳۴۵۶۷۸۹'
self.string = ''
def fix_encoding(self):
assert isinstance(self.string, str), repr(self.string)
#if isinstance(self.string, unicode):
#return 'utf8'
try:
self.string.decode('utf8', 'strict')
self.string = self.string.decode('utf8')
return 'utf8'
except UnicodeError:
pass
try:
self.string.decode('utf16', 'strict')
self.string = self.string.decode('utf16')
return 'utf16'
except UnicodeError:
pass
self.string = self.string.decode('windows-1256')
return 'windows-1256'
def fix_italic(self):
self.string = self.string.replace('<i>' , '')
self.string = self.string.replace('</i>', '')
def fix_arabic(self):
self.string = self.string.replace(u'ي', u'ی')
self.string = self.string.replace(u'ك', u'ک')
def fix_question_mark(self):
# quistion mark in persina is ؟ not ?
self.string = self.string.replace('?', u'؟')
def fix_other(self):
self.string = self.string.replace(u'\u202B', u'')
lines = self.string.split('\n')
string = ''
ln = 0
for line in lines:
# Fixing the bug on persian number at line 1.
if ln == 0:
string += line
elif re.match('^%s\s-->\s%s$' % (self.time, self.time), line):
string += line
elif re.match('^%s\s-->\s%s$' % (self.time, self.time), line[:-1]):
string += line
elif line.strip() == '':
string += line
elif re.match('^\d+$', line):
string += line
elif re.match('^\d+$', line[:-1]):
string += line
else:
# this should be subtitle
s = re.match('^([\.!?]*)', line)
try:
line = re.sub('^%s' % s.group(), '', line)
except:
pass
# use persian numbers
for i in range(0, 10):
line = line.replace(str(i), self.number[i])
# for ltr problems some peoples put '-' on EOL
# it should be in start
if len(line) != 0 and line[-1] == '-':
line = '- %s' % line[:-1]
line += s.group()
# put rtl char in start of line (it forces some player to show that line rtl
string += u'\u202B' + unicode(line)
# noting to see here
string += '\n'
self.string = string
ln+1
def decode_string(self, string):
self.string = string
self.fix_encoding()
self.fix_italic()
self.fix_arabic()
self.fix_question_mark()
self.fix_other()
return self.string
if __name__ == '__main__':
Main()