diff --git a/h5p.classes.php b/h5p.classes.php index d91b794..87bcdcf 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -1866,6 +1866,68 @@ class H5PCore { return $dependencies; } + /** + * Process SRT to JSON files in order to Interactive Video load the subtitles + * + */ + public function processSRT($filename, $contentId) { + define('SRT_STATE_SUBNUMBER', 0); + define('SRT_STATE_TIME', 1); + define('SRT_STATE_TEXT', 2); + define('SRT_STATE_BLANK', 3); + + $lines = file('../wp-content/uploads/h5p/content/'. $contentId .'/videos/'.$filename); + + $subs = array(); + $state = SRT_STATE_SUBNUMBER; + $subNum = 0; + $subText = ''; + $subTime = ''; + + foreach($lines as $line) { + switch($state) { + case SRT_STATE_SUBNUMBER: + $subNum = trim($line); + $state = SRT_STATE_TIME; + break; + + case SRT_STATE_TIME: + $subTime = trim($line); + $state = SRT_STATE_TEXT; + break; + + case SRT_STATE_TEXT: + if (trim($line) == '') { + $sub = array(); + $sub['number'] = $subNum; + list($sub['startTime'], $sub['stopTime']) = explode(' --> ', $subTime); + $sub['text'] = $subText; + $subText = ''; + $state = SRT_STATE_SUBNUMBER; + + $subs[] = $sub; + } else { + $subText .= utf8_encode($line); + } + break; + } + } + + if ($subs) { + $filenameArray = explode(".", $filename); + $file = json_encode($subs); + $finalName = $filenameArray[0].'.json'; + file_put_contents('../wp-content/uploads/h5p/content/'. $contentId .'/videos/'.$contentId.'_'.$finalName, $file); + return TRUE; + } + return FALSE; + } + + + + + + /** * Get all dependency assets of the given type *