-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix French subtitles + refactor conversion script (#431)
* Fix subtitles and scripts * Fix subtitle
- Loading branch information
Showing
9 changed files
with
80 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,53 @@ | ||
#!/usr/bin/python3 | ||
import getopt | ||
import re | ||
import sys | ||
import argparse | ||
from pathlib import Path | ||
|
||
PATTERN_TIMESTAMP = re.compile('^[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9] --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]') | ||
PATTERN_NUM = re.compile('\\d+') | ||
PATTERN_TIMESTAMP = re.compile( | ||
"^[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9] --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]" | ||
) | ||
PATTERN_NUM = re.compile("\\d+") | ||
|
||
|
||
def main(argv): | ||
inputfile = '' | ||
outputfile = '' | ||
try: | ||
opts, args = getopt.getopt(argv, "hi:o:", ["ifile=", "ofile="]) | ||
except getopt.GetoptError: | ||
print('srt_worker.py -i <inputfile> -o <outputfile>') | ||
sys.exit(2) | ||
for opt, arg in opts: | ||
if opt == '-h': | ||
print( 'Usage: convert_bilingual_monolingual.py -i <inputfile> -o <outputfile>') | ||
sys.exit(-2) | ||
elif opt in ("-i", "--ifile"): | ||
inputfile = arg | ||
elif opt in ("-o", "--ofile"): | ||
outputfile = arg | ||
|
||
if not inputfile: | ||
print('no input file is specified.\nUsage: convert_bilingual_monolingual.py -i <inputfile> -o <outputfile>') | ||
elif not outputfile: | ||
print('no output file is specified.\nUsage: convert_bilingual_monolingual.py -i <inputfile> -o <outputfile>') | ||
else: | ||
process(inputfile, outputfile) | ||
|
||
|
||
def process(input_file, output): | ||
def convert(input_file, output_file): | ||
""" | ||
Convert bilingual caption file to monolingual caption, supported caption file type is srt. | ||
Convert bilingual caption file to monolingual caption. Supported caption file type is SRT. | ||
""" | ||
line_count = 0 | ||
with open(input_file) as file: | ||
with open(output, 'a') as output: | ||
with open(output_file, "w") as output_file: | ||
for line in file: | ||
if line_count == 0: | ||
line_count += 1 | ||
output.write(line) | ||
output_file.write(line) | ||
elif PATTERN_TIMESTAMP.match(line): | ||
line_count += 1 | ||
output.write(line) | ||
elif line == '\n': | ||
output_file.write(line) | ||
elif line == "\n": | ||
line_count = 0 | ||
output.write(line) | ||
output_file.write(line) | ||
else: | ||
if line_count == 2: | ||
output.write(line) | ||
output_file.write(line) | ||
line_count += 1 | ||
output.close() | ||
print('conversion completed!') | ||
output_file.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
main(sys.argv[1:]) | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--input_language_folder", type=str, help="Folder with input bilingual SRT files to be converted" | ||
) | ||
parser.add_argument( | ||
"--output_language_folder", | ||
type=str, | ||
default="tmp-subtitles", | ||
help="Folder to store converted monolingual SRT files", | ||
) | ||
args = parser.parse_args() | ||
|
||
output_path = Path(args.output_language_folder) | ||
output_path.mkdir(parents=True, exist_ok=True) | ||
input_files = Path(args.input_language_folder).glob("*.srt") | ||
for input_file in input_files: | ||
convert(input_file, output_path / input_file.name) | ||
print(f"Succesfully converted {len(list(input_files))} files to {args.output_language_folder} folder") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters