-
Notifications
You must be signed in to change notification settings - Fork 0
/
alignment.php
392 lines (289 loc) · 12.2 KB
/
alignment.php
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
set_time_limit(0);
ini_set('memory_limit', '500M');
date_default_timezone_set('CET');
ob_implicit_flush(true);
ob_end_flush();
//disable_ob();
function logMessage($message) {
/*
echo $message;
echo "</br>";
*/
file_put_contents("alignment-log.txt", $message, FILE_APPEND);
}
/**
*
*
*
*
*
* Preparation for Alignment
*
*
*
*
*
*/
function textObjectToHTMLString($inputTextObject, $mediaFileURI, $autoAddIDs = false) {
$sentenceID = 0;
if (is_string($inputTextObject)) {
$inputTextObject = json_decode($inputTextObject, 1);
if (!$inputTextObject) {
//echo 'Input text could not be parsed as JSON.';
}
} else {
//echo 'Input text needs to be a String';
}
$outputHTML = '<div data-media-file-uri="'.$mediaFileURI.'">';
foreach ($inputTextObject['textBody'] as $paragraph) {
$outputHTML .= '<p data-type="'.$paragraph['type'].'">';
$sentences = $paragraph['sentences'];
foreach ($sentences as $sentence) {
$idAttribute = '';
$timeAttributes = '';
if ($autoAddIDs && $paragraph['type'] == 'speech') {
$idAttribute = ' id="s'.sprintf('%06d', ++$sentenceID).'"';
}
if (isset($sentence['timeStart']) && isset($sentence['timeEnd'])) {
$timeAttributes = ' class="timebased" data-start="'.$sentence['timeStart'].'" data-end="'.$sentence['timeEnd'].'"';
}
$sentenceText = (is_array($sentence)) ? $sentence['text'] : $sentence;
$outputHTML .= '<span'.$idAttribute.$timeAttributes.'>'.$sentenceText.' </span>';
}
$outputHTML .= '</p>';
}
$outputHTML .= '</div>';
return $outputHTML;
}
function simpleTextBodyArrayToHTMLString($textBody) {
$outputHTML = '<p data-type="'.$textBody['type'].'">';
//TODO: REMOVE QUICK FIX
/*
if (count($paragraph['sentences']) == 1) {
$sentences = $paragraph['sentences'][0];
} else {
$sentences = $paragraph['sentences'];
}
*/
$sentences = $textBody['sentences'];
foreach ($sentences as $sentence) {
$timeAttributes = '';
if (isset($sentence['timeStart']) && isset($sentence['timeEnd'])) {
$timeAttributes = ' class="timebased" data-start="'.$sentence['timeStart'].'" data-end="'.$sentence['timeEnd'].'"';
}
$sentenceText = (is_array($sentence)) ? $sentence['text'] : $sentence;
$outputHTML .= '<span'.$timeAttributes.'>'.$sentenceText.' </span>';
}
$outputHTML .= '</p>';
return $outputHTML;
}
function textObjectToAlignmentInput($inputTextObject, $mediaFileURI) {
$outputXML = '<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8"/></head><body>';
$outputXML .= textObjectToHTMLString($inputTextObject, $mediaFileURI, true);
$outputXML .= '</body></html>';
return $outputXML;
}
function mergeAlignmentOutputWithTextObject($alignmentOutput, $inputTextObject) {
$fragmentCnt = 0;
if (is_string($inputTextObject)) {
$inputTextObject = json_decode($inputTextObject, 1);
if (!$inputTextObject) {
echo 'Input text could not be parsed as JSON.';
}
} else {
echo 'Input text needs to be a String';
}
if (is_string($alignmentOutput)) {
$alignmentOutput = json_decode($alignmentOutput, 1);
if (!$alignmentOutput) {
echo 'Alignment Output could not be parsed as JSON.';
}
} else {
echo 'Alignment Output needs to be a String';
}
foreach ($inputTextObject['textBody'] as $paragraphIndex => $paragraph) {
if ($paragraph['type'] == 'speech') {
foreach ($paragraph['sentences'] as $sentenceIndex => $sentence) {
$fragmentID = 's'.sprintf('%06d', ++$fragmentCnt);
foreach ($alignmentOutput['fragments'] as $fragment) {
if ($fragment['id'] == $fragmentID) {
$inputTextObject['textBody'][$paragraphIndex]['sentences'][$sentenceIndex]['timeStart'] = $fragment['begin'];
$inputTextObject['textBody'][$paragraphIndex]['sentences'][$sentenceIndex]['timeEnd'] = $fragment['end'];
}
}
}
}
$inputTextObject['textBody'][$paragraphIndex]['text'] = simpleTextBodyArrayToHTMLString($inputTextObject['textBody'][$paragraphIndex]);
}
$newTextHTML = '';
foreach ($inputTextObject['textBody'] as $paragraphIndex => $paragraph) {
foreach ($paragraph['sentences'] as $sentenceIndex => $sentence) {
$newTextHTML .= $inputTextObject['textBody'][$paragraphIndex]['text'];
}
}
$inputTextObject['textHTML'] = $newTextHTML;
//return json_encode($inputTextObject, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $inputTextObject;
}
/**
*
*
*
* Alignment
*
*
*
*
*/
/**
* @param $XMLFilePath
* @param $sleep
* @return mixed
*/
function forceAlignXMLData($XMLFilePath, $sleep=1) {
global $conf;
echo $XMLFilePath."<br>";
$return["success"] = "false";
$fileName = preg_replace('/\\.[^.\\s]{3,4}$/', '', $XMLFilePath);
$return["filename"] = $conf["dir"]["output"]."/".$fileName.".json";
$file_content = file_get_contents($conf["dir"]["input"]."/".$XMLFilePath);
ini_set('mbstring.substitute_character', 32);
if (mb_detect_encoding($file_content) == 'ISO-8859-1') {
$file_content = mb_convert_encoding($file_content, 'UTF-8', 'ISO-8859-1');
}
$file_content_xml = new SimpleXMLElement($file_content);
$metaElem = $file_content_xml->body->div;
$mediaFilePath = $metaElem["data-media-file-uri"];
$mediaFilePathArray = preg_split("/\\//", $mediaFilePath);
$mediaFileName = array_pop($mediaFilePathArray);
if (!file_exists($conf["dir"]["cache"].'/'.$mediaFileName)) {
logMessage("Audio file not found. Downloading file...\n");
getMediaFile($mediaFilePath);
} else {
//logMessage("Audio file found (".$conf["dir"]["cache"]."/".$mediaFileName."). No download necessary.\n");
}
if (!file_exists($conf["dir"]["output"]."/".$fileName.".json")) {
logMessage("Force aligning ".$mediaFileName." with ".$XMLFilePath." ...\n");
sleep($sleep);
$return["success"] = forceAlignMedia($conf["dir"]["cache"]."/".$mediaFileName, $conf["dir"]["input"]."/".$XMLFilePath, $conf["dir"]["output"]."/".$fileName.".json");
} else {
$return["success"] = "true";
logMessage("JSON timings file found (" . $conf["dir"]["output"] . "/" . $fileName . ".json). Force Align not necessary.\n");
}
sleep($sleep);
return $return;
}
/**
* @param $mediaFilePath
* @return mixed
*/
function getMediaFile($mediaFilePath) {
global $conf;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $mediaFilePath);
// set buffer size to 1mb to execute progress function less often
curl_setopt($ch, CURLOPT_BUFFERSIZE, 600000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$filePathArray = preg_split("/\\//", $mediaFilePath);
$mediaFileName = array_pop($filePathArray);
if (!$output || $status != 200) {
logMessage("Error: Audio file could not be downloaded (HTTP Error ".$status.").\n");
} else {
file_put_contents($conf["dir"]["cache"]."/".$mediaFileName, $output);
logMessage("Audio file successfully downloaded.\n");
}
}
$down = 0;
/**
* @param $resource
* @param $download_size
* @param $downloaded
* @param $upload_size
* @param $uploaded
* @return mixed
*/
function progress($resource,$download_size, $downloaded, $upload_size, $uploaded) {
global $down;
if ($download_size > 0 && $downloaded > ($down + 600000)) {
$down = $downloaded + 600000;
$progress = $downloaded / $download_size * 100;
logMessage("Media download progress: ".$progress."\n");
}
}
/**
* @param $mediaFilePath
* @param $optimisedXMLFilePath
* @param $outputFilePath
* @return mixed
*/
function forceAlignMedia($mediaFilePath, $optimisedXMLFilePath, $outputFilePath,$sleep=1) {
global $conf;
if ($conf["serverType"] == 'windows') {
$secureAudioPath = str_replace("/","\\",escapeshellcmd(__DIR__."\\".$mediaFilePath));
$secureXMLPath = str_replace("/","\\",escapeshellcmd(__DIR__."\\".$optimisedXMLFilePath));
$secureOutputPath = str_replace("/","\\",escapeshellcmd(__DIR__."\\".$outputFilePath));
} else {
$secureAudioPath = escapeshellcmd(__DIR__."/".$mediaFilePath);
$secureXMLPath = escapeshellcmd(__DIR__."/".$optimisedXMLFilePath);
$secureOutputPath = escapeshellcmd(__DIR__."/".$outputFilePath);
}
$exec_enabled =
function_exists('exec') &&
!in_array('exec', array_map('trim', explode(', ', ini_get('disable_functions')))) &&
strtolower(ini_get('safe_mode')) != 1
;
if (!$exec_enabled) {
logMessage("PHP shell exec not allowed. Aeneas can not be executed.\n");
exit();
}
if ($conf["serverType"] == 'windows') {
putenv('PATH=$PATH;'.$conf["ffmpeg"].";".$conf["pythonDir"].";".$conf["pythonDirScripts"].";".$conf["eSpeak"]);
} else {
putenv('PATH=$PATH:'.$conf["pythonDir"]);
}
putenv('set PYTHONIOENCODING="UTF-8"');
//$command = 'set PYTHONIOENCODING="UTF-8" && python -m aeneas.tools.execute_task '.$secureAudioPath.' '.$secureXMLPath.' "task_language=deu|os_task_file_format=json|is_text_type=unparsed|is_text_unparsed_id_regex=s[0-9]+|is_text_unparsed_id_sort=numeric|task_adjust_boundary_no_zero=true|task_adjust_boundary_nonspeech_min=2|task_adjust_boundary_nonspeech_string=REMOVE" '.$secureOutputPath.' -vv -l --log="F:\webdev\VideoTranscriptGenerator-master\www\admin\tmpaeneas.log" 2>&1';
//$command = 'set PYTHONIOENCODING="UTF-8" && python -m aeneas.tools.execute_task '.$secureAudioPath.' '.$secureXMLPath.' "task_language=deu|os_task_file_format=json|is_text_type=unparsed|is_text_unparsed_id_regex=s[0-9]+|is_text_unparsed_id_sort=numeric|task_adjust_boundary_no_zero=true|task_adjust_boundary_nonspeech_min=2|task_adjust_boundary_nonspeech_string=REMOVE" '.$secureOutputPath.' 2>&1';
$command = 'set PYTHONIOENCODING="UTF-8" && python -m aeneas.tools.execute_task '.$secureAudioPath.' '.$secureXMLPath.' "task_language=deu|os_task_file_format=json|is_text_type=unparsed|is_text_unparsed_id_regex=s[0-9]+|is_text_unparsed_id_sort=numeric|task_adjust_boundary_no_zero=false|task_adjust_boundary_nonspeech_min=2|task_adjust_boundary_nonspeech_string=REMOVE|task_adjust_boundary_nonspeech_remove=REMOVE|is_audio_file_detect_head_min=0.1|is_audio_file_detect_head_max=3|is_audio_file_detect_tail_min=0.1|is_audio_file_detect_tail_max=3|task_adjust_boundary_algorithm=aftercurrent|task_adjust_boundary_aftercurrent_value=0.5|is_audio_file_head_length=1" '.$secureOutputPath.' 2>&1';
$output = exec($command,$foo);
if (strpos($output, '[INFO] Created file ') !== false) {
logMessage("Force align success. Aeneas Output: ".$output."\n");
return "true";
} else {
logMessage("Force align error. Output: ".$output."\n");
return "false";
}
}
function disable_ob() {
// Turn off output buffering
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);
// Implicitly flush the buffer(s)
ini_set('implicit_flush', true);
ob_implicit_flush(true);
// Clear, and turn off output buffering
while (ob_get_level() > 0) {
// Get the curent level
$level = ob_get_level();
// End the buffering
ob_end_clean();
// If the current level has not changed, abort
if (ob_get_level() == $level) break;
}
// Disable apache output buffering/compression
if (function_exists('apache_setenv')) {
apache_setenv('no-gzip', '1');
apache_setenv('dont-vary', '1');
}
}
?>