forked from voxolab/voxo-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_decode.py
131 lines (101 loc) · 4.26 KB
/
fast_decode.py
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
import logging
import os
from voxolab import convert
from voxolab import convert_subtitle
from voxolab import seg_ctm_to_xml
from voxolab import xml_alpha_to_numbers
from voxolab import xml_to_xml_punctuated
from shutil import copyfile
logger = logging.getLogger(__name__)
def docker_decode(filename, output_dir, model, models,
scripts_dir):
filename = os.path.basename(filename)
base, ext = os.path.splitext(filename)
decode_dir = os.path.join(output_dir, base)
if model not in models:
raise KeyError("model {} is not present in loaded"
"configuration.".format(model))
command = models[model]['DECODE_CMD']
full_command = command.format(
file=filename)
print("Hell yeah, trying to decode {} in {} with command\n{}"
.format(filename, output_dir, full_command))
# TODO: manage return code
os.system(full_command)
convert_decode_output(
model,
decode_dir,
base,
scripts_dir,
models[model]['RECASE_DIR']
if 'RECASE_DIR' in models[model] else None,
models[model]['PUNCTUATE_CMD']
if 'PUNCTUATE_CMD' in models[model] else None)
return 0
def convert_decode_output(
model, decode_dir, base, scripts_dir, recase_path,
punctuate_command):
ctm_file_min = os.path.join(decode_dir, base + ".ctm")
ctm_file_maj = os.path.join(decode_dir, base + ".MAJ.ctm")
seg_file = os.path.join(decode_dir, 'seg', base + ".iv.seg")
seg_file_sorted = os.path.join(decode_dir, 'seg', base + ".iv.seg.sorted")
xml_file_without_numbers = os.path.join(
decode_dir, base + ".withoutnumbers.xml")
xml_file_with_punctuation = os.path.join(
decode_dir, base + ".withpunctuation.xml")
xml_file = os.path.join(decode_dir, base + ".v2.xml")
srt_file = os.path.join(decode_dir, base + ".srt")
webvtt_file = os.path.join(decode_dir, base + ".vtt")
txt_file = os.path.join(decode_dir, base + ".txt")
txt_file_postprocessing = os.path.join(decode_dir, base + ".pp.txt")
txt_file_original = os.path.join(decode_dir, base + ".original.txt")
if('french' in model and recase_path is not None):
convert.recase_ctm(ctm_file_min, ctm_file_maj, recase_path)
else:
copyfile(ctm_file_min, ctm_file_maj)
os.system("sort -k3,3 -n {} > {}".format(seg_file, seg_file_sorted))
seg_ctm_to_xml.seg_ctm_to_xml(
seg_file_sorted, ctm_file_maj, xml_file_without_numbers)
convert.xml_to_txt(xml_file_without_numbers, txt_file)
if punctuate_command is not None:
try:
xml_string =\
xml_to_xml_punctuated.punctuate_xml(
xml_file_without_numbers,
punctuate_command)
with open(xml_file_with_punctuation, "w") as f:
f.write(xml_string)
except Exception as e:
logger.exception("Error while punctuating: " + str(e))
copyfile(xml_file_without_numbers, xml_file_with_punctuation)
else:
copyfile(xml_file_without_numbers, xml_file_with_punctuation)
if 'french' in model:
xml_alpha_to_numbers.xml_alpha_to_numbers_from_file(
xml_file_with_punctuation,
os.path.join(scripts_dir, 'convertirAlphaEnNombre.pl'),
os.path.join(scripts_dir, 'convertirNombreEnAlpha.pl'),
xml_file,
'utf-8', 'utf-8')
else:
copyfile(xml_file_with_punctuation, xml_file)
convert.xml_to_srt(xml_file, srt_file)
convert.xml_to_webvtt(xml_file, webvtt_file)
try:
convert_subtitle.make_convert_subtitle(
srt_file,
'ScenaristClosedCaptions',
'{mono} {se}'.format(
mono='mono',
se=os.path.join(scripts_dir, 'SubtitleEdit.exe')
)
)
except Exception as e:
logger.exception("Error while converting to SCC")
if os.path.exists(os.path.join(
scripts_dir, 'txt_post_processing_{}.pl'.format(model))):
# Save file before post processing
copyfile(txt_file, txt_file_original)
os.system('perl {}/txt_post_processing_{}.pl < {} > {}'.format(
scripts_dir, model, txt_file_postprocessing))
copyfile(txt_file_postprocessing, txt_file)