Created
October 21, 2016 19:10
-
-
Save rondog/5d25797c14fb6bec7c820c149cb79a9c to your computer and use it in GitHub Desktop.
Convert TTML captions to a JSON array
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
<?php | |
if (isset($_POST['submit'])) { | |
$total = count($_FILES['captions']['name']); | |
$zipname = 'json_captions.zip'; | |
$zip = new ZipArchive(); | |
$zip->open($zipname, ZipArchive::CREATE); | |
for ($i = 0; $i < $total; $i++) { | |
$contents = file_get_contents($_FILES['captions']['tmp_name'][$i]); | |
$replaced = preg_replace("/<br\W*?\/>/", " ", $contents); | |
$xml = simplexml_load_string($replaced); | |
$captionLength = count($xml->body->div->p); | |
$jsonCaption = array(); | |
for ($j = 0; $j < $captionLength; $j++) { | |
$caption = array( | |
'begin' => (string) $xml->body->div->p[$j]->attributes()['begin'], | |
'end' => (string) $xml->body->div->p[$j]->attributes()['end'], | |
'text' => (string) $xml->body->div->p[$j] | |
); | |
array_push($jsonCaption, $caption); | |
} | |
$json = json_encode($jsonCaption, JSON_PRETTY_PRINT); | |
$slide_index = preg_replace("/[^0-9]/","", $_FILES['captions']['name'][$i]); | |
$fd = fopen('php://temp/maxmemory:1048576', 'w'); | |
fwrite($fd, $json); | |
rewind($fd); | |
$zip->addFromString('caption' . $slide_index . '.js', stream_get_contents($fd)); | |
fclose($fd); | |
} | |
$zip->close(); | |
header('Content-Type: application/zip'); | |
header('Content-disposition: attachment; filename='.$zipname); | |
header('Content-Length: ' . filesize($zipname)); | |
readfile($zipname); | |
unlink($zipname); | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>TTML to JSON</title> | |
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> | |
<style> | |
em { | |
font-weight: 700; | |
} | |
form { | |
text-align:center; | |
} | |
input { | |
display: inline-block; | |
margin:auto; | |
} | |
button,input { | |
padding:30px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="well well-sm col-md-6 col-md-offset-3"> | |
<h3>Select all TTML files to parse to JSON files</h3> | |
<p>The file name number in <em>captionN.js</em> is determined by the number on the selected TTML file. For example, <em>SLIDE 13 CA.ttml</em> will output a file named <em>caption13.js</em>.</p> | |
<p>This tool is for one project at a time. Do not upload <em>SLIDE 1 CA.ttml</em> and <em>SLIDE 1 NON-CA.ttml</em> at the same time. This will overwrite eachother</p> | |
<form method="POST" enctype="multipart/form-data"> | |
<input type="file" name="captions[]" multiple /> | |
<input type="submit" name="submit" class="btn btn-success" /> | |
</form> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment