Skip to content

Commit e072fb2

Browse files
remove blank lines, update version number (#29)
* Remove useless blank lines in some vtt files. 1- add a new function to remove any unwanted blank lines in vtt file 2- also update test files by removing useless empty lines from them. * Backporting the vtt_to_srt script from python 3 to python 2 and test files as will. - backporting vtt_to_srt2 script to python 2.7.18 - backporting test files to run with pytest 4.6.11 * remove uesless blank lines update setup file with new version number moving version 2 code to it's new repository --------- Co-authored-by: karimkoko <karimkoko0@yahoo.com>
1 parent 91404ae commit e072fb2

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
long_description = fh.read()
66

77
setuptools.setup(name='vtt_to_srt3',
8-
version='0.2.0.1',
8+
version='0.2.0.3',
99
author="Jeison Cardoso",
1010
author_email="j@jsonzilla.com",
1111
maintainer="Jeison Cardoso",

tests/valid_output_idd.srt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
1
32
00:00:08,393 --> 00:00:10,437
43
♪ ♪
@@ -8,7 +7,6 @@
87
Narrator: <i>blaba</i>
98
2
109

11-
1210
3
1311
00:00:15,024 --> 00:00:15,817
1412
<i>bla</i>
@@ -30,15 +28,12 @@ ah</i>
3028
00:00:25,994 --> 00:00:28,371
3129
<i>blaba</i>
3230

33-
3431
8
3532
00:00:28,455 --> 00:00:32,125
3633
<i>blaba
3734
blaba</i>
3835

39-
4036
9
4137
00:00:32,208 --> 00:00:34,002
4238
<i>blaba</i>
4339
1010
44-

tests/valid_output_iso-8859-2.srt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
1
32
00:00:01,000 --> 00:00:04,000
43
- Trinken Sie niemals flüssigen Stickstoff.
@@ -7,4 +6,3 @@
76
00:00:05,000 --> 00:00:09,000
87
- Es wird Ihren Magen perforieren.
98
- Du könntest sterben.
10-

tests/valid_output_utf8.srt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
1
32
00:00:01,000 --> 00:00:04,000
43
- Never drink liquid nitrogen.
@@ -7,4 +6,3 @@
76
00:00:05,000 --> 00:00:09,000
87
- It will perforate your stomach.
98
- You could die.
10-

vtt_to_srt/vtt_to_srt.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def convert_content(self, contents: str) -> str:
6363
replacement = re.sub(
6464
r"::[\-\w]+\([\-.\w\d]+\)[ ]*{[.,:;\(\) \-\w\d]+\n }\n", "", replacement)
6565
replacement = re.sub(r"Style:\n##\n", "", replacement)
66+
replacement = self.remove_blank_lines(replacement)
6667
replacement = self.remove_simple_identifiers(replacement)
6768
replacement = self.add_sequence_numbers(replacement)
6869

@@ -89,7 +90,36 @@ def add_sequence_numbers(self, contents: str) -> str:
8990
counter += 1
9091
out += line + '\n'
9192
return out
92-
93+
94+
def remove_blank_lines(self, contents: str) -> str:
95+
# Remove useless blank lines from the vtt file
96+
lines = contents.split('\n')
97+
lines = [x for x in lines if x != '']
98+
lines.append('')
99+
out = []
100+
num = 0
101+
while num < len(lines) :
102+
if re.match(r"^\d+$", lines[num]) and self.has_timestamp(lines[num + 1]):
103+
if num == 0 :
104+
pass
105+
else:
106+
out.append('')
107+
out.append(lines[num])
108+
out.append(lines[num + 1])
109+
num += 2
110+
elif self.has_timestamp(lines[num]):
111+
if num == 0 :
112+
pass
113+
else :
114+
out.append('')
115+
out.append(lines[num])
116+
num += 1
117+
else:
118+
out.append(lines[num])
119+
num += 1
120+
out.pop()
121+
return '\n'.join(out)
122+
93123
def remove_simple_identifiers(self, contents: str) -> str:
94124
"""Remove simple identifiers of vtt file
95125
@@ -102,7 +132,7 @@ def remove_simple_identifiers(self, contents: str) -> str:
102132
if re.match(r"^\d+$", lines[i - 1]):
103133
out.pop()
104134
out.append(line)
105-
return '\n'.join(out)
135+
return '\n'.join(out)
106136

107137
def write_file(self, filename: str, data, encoding_format: str = "utf-8"):
108138
"""Create a file with some data

0 commit comments

Comments
 (0)