1- #!/usr/bin/python
1+ #!/usr/bin/python
22# Jansen A. Simanullang / Jeison Cardoso
33
4+ """Convert of vtt to srt format"""
5+
46import os
57import re
68import sys
7- from stat import *
9+ from string import Template
10+ from stat import S_ISDIR , ST_MODE , S_ISREG
11+
812
913def convert_header (contents ):
10- """Convert of vtt header to str format
14+ """Convert of vtt header to srt format
1115
1216 Keyword arguments:
1317 contents
@@ -17,17 +21,39 @@ def convert_header(contents):
1721 replacement = re .sub (r"Language:[ \-\w]+\n" , "" , replacement )
1822 return replacement
1923
24+
25+ def padding_timestamp (contents ):
26+ """Add 00 to padding timestamp of to srt format
27+
28+ Keyword arguments:
29+ contents
30+ """
31+ find_srt = Template (r'$a,$b --> $a,$b(?:[ \-\w]+:[\w\%\d:,.]+)*\n' )
32+ minute = r"((?:\d\d:){1}\d\d)"
33+ second = r"((?:\d\d:){0}\d\d)"
34+ padding_minute = find_srt .substitute (a = minute , b = r"(\d{0,3})" )
35+ padding_second = find_srt .substitute (a = second , b = r"(\d{0,3})" )
36+ replacement = re .sub (padding_minute , r"00:\1,\2 --> 00:\3,\4\n" , contents )
37+ return re .sub (padding_second , r"00:00:\1,\2 --> 00:00:\3,\4\n" , replacement )
38+
39+
2040def convert_timestamp (contents ):
21- replacement = re .sub (r"(\d\d:\d\d:\d\d).(\d\d\d) --> (\d\d:\d\d:\d\d).(\d\d\d)(?:[ \-\w]+:[\w\%\d:.,]+)*\n" , r"\1,\2 --> \3,\4\n" , contents )
22- replacement = re .sub (r"(\d\d:\d\d).(\d\d\d) --> (\d\d:\d\d).(\d\d\d)(?:[ \-\w]+:[\w\%\d:.,]+)*\n" , r"00:\1,\2 --> 00:\3,\4\n" , replacement )
23- return re .sub (r"(\d\d).(\d\d\d) --> (\d\d).(\d\d\d)(?:[ \-\w]+:[\w\%\d:.,]+)*\n" , r"00:00:\1,\2 --> 00:00:\3,\4\n" , replacement )
41+ """Convert timestamp of vtt file to srt format
42+
43+ Keyword arguments:
44+ contents
45+ """
46+ find_vtt = Template (r'$a.$b --> $a.$b(?:[ \-\w]+:[\w\%\d:,.]+)*\n' )
47+ all_timestamp = find_vtt .substitute (a = r"((?:\d\d:){0,2}\d\d)" , b = r"(\d{0,3})" )
48+ return padding_timestamp (re .sub (all_timestamp , r"\1,\2 --> \3,\4\n" , contents ))
49+
2450
2551def convert_content (contents ):
26- """Convert content of vtt file to str format
52+ """Convert content of vtt file to srt format
2753
2854 Keyword arguments:
2955 contents
30- """
56+ """
3157 replacement = convert_timestamp (contents )
3258 replacement = convert_header (replacement )
3359 replacement = re .sub (r"<c[.\w\d]*>" , "" , replacement )
@@ -38,8 +64,15 @@ def convert_content(contents):
3864 replacement = add_sequence_numbers (replacement )
3965 return replacement
4066
67+
4168def timestamp_line (content ):
42- return re .match (r"((\d\d:){0,3}\d\d),(\d{0,3}) --> ((\d\d:){0,2}\d\d),(\d{0,3})" , content ) != None
69+ """Check if line is a timestamp srt format
70+
71+ Keyword arguments:
72+ contents
73+ """
74+ return re .match (r"((\d\d:){2}\d\d),(\d{3}) --> ((\d\d:){2}\d\d),(\d{3})" , content ) is not None
75+
4376
4477def add_sequence_numbers (contents ):
4578 """Adds sequence numbers to subtitle contents and returns new subtitle contents
@@ -59,40 +92,37 @@ def add_sequence_numbers(contents):
5992 return output
6093
6194
62- def file_create (str_name_file , str_data ):
95+ def file_create (str_name_file : str , str_data ):
6396 """Create a file with some data
6497
6598 Keyword arguments:
6699 str_name_file -- filename pat
67100 str_data -- dat to write
68101 """
69- # --------------------------------
70- # file_create(str_name_file, str_data)
71- # create a text file
72102 try :
73- f = open (str_name_file , "w" , encoding = 'utf-8' )
74- f .writelines (str (str_data ))
75- f .close ()
103+ with open (str_name_file , "w" , encoding = 'utf-8' ) as file :
104+ file .writelines (str (str_data ))
76105 except IOError :
77106 str_name_file = str_name_file .split (os .sep )[- 1 ]
78- f = open (str_name_file , "w" )
79- f .writelines (str (str_data ))
80- f .close ()
107+ with open (str_name_file , "w" ) as file :
108+ file .writelines (str (str_data ))
81109 print ("file created: " + str_name_file + "\n " )
82110
83111
84- def read_text_file (str_name_file ):
112+ def read_text_file (str_name_file : str ):
85113 """Read a file text
86114
87115 Keyword arguments:
88116 str_name_file -- filename pat
89117 """
90- f = open (str_name_file , mode = "r" , encoding = 'utf-8' )
91- print ("file being read: " + str_name_file + "\n " )
92- return f .read ()
118+ content : str = ''
119+ with open (str_name_file , mode = "r" , encoding = 'utf-8' ) as file :
120+ print ("file being read: " + str_name_file + "\n " )
121+ content = file .read ()
122+ return content
93123
94124
95- def vtt_to_srt (str_name_file ):
125+ def vtt_to_srt (str_name_file : str ):
96126 """Convert vtt file to a srt file
97127
98128 Keyword arguments:
@@ -114,8 +144,8 @@ def walk_tree(top_most_path, callback):
114144 top_most_path -- parent directory
115145 callback -- function to call
116146 """
117- for f in os .listdir (top_most_path ):
118- pathname = os .path .join (top_most_path , f )
147+ for file in os .listdir (top_most_path ):
148+ pathname = os .path .join (top_most_path , file )
119149 mode = os .stat (pathname )[ST_MODE ]
120150 if S_ISDIR (mode ):
121151 # It's a directory, recurse into it
@@ -135,21 +165,21 @@ def walk_dir(top_most_path, callback):
135165 top_most_path -- parent directory
136166 callback -- function to call
137167 """
138- for f in os .listdir (top_most_path ):
139- pathname = os .path .join (top_most_path , f )
168+ for file in os .listdir (top_most_path ):
169+ pathname = os .path .join (top_most_path , file )
140170 if not os .path .isdir (pathname ):
141171 # It"s a file, call the callback function
142172 callback (pathname )
143173
144174
145- def convert_vtt_to_str (f ):
175+ def convert_vtt_to_str (file ):
146176 """Convert vtt file to string
147177
148178 Keyword arguments:
149179 f -- file to convert
150180 """
151- if ".vtt" in f :
152- vtt_to_srt (f )
181+ if ".vtt" in file :
182+ vtt_to_srt (file )
153183
154184
155185def vtts_to_srt (directory , rec = False ):
@@ -172,18 +202,24 @@ def print_usage():
172202 print ("\t pathname\t - a file or directory with files to be converted" )
173203 print ("\t -r\t \t - walk path recursively\n " )
174204
205+
175206def main ():
207+ """main
208+
209+ Keyword arguments:
210+ pathname - a file or directory with files to be converted
211+ -r walk path recursively
212+ """
176213 if len (sys .argv ) < 2 or sys .argv [1 ] == "--help" or not os .path .exists (sys .argv [1 ]):
177214 print_usage ()
178- exit ()
215+ sys . exit ()
179216 path = sys .argv [1 ]
180- rec = True if len (sys .argv ) > 2 and sys .argv [2 ] == "-r" else False
217+ rec = bool ( len (sys .argv ) > 2 and sys .argv [2 ] == "-r" )
181218 if os .path .isdir (path ):
182219 vtts_to_srt (path , rec )
183220 else :
184- vtt_to_srt (path )
221+ vtt_to_srt (path )
185222
186223
187224if __name__ == "__main__" :
188- main ()
189-
225+ main ()
0 commit comments