Skip to content

Commit

Permalink
New subtitle support (#49)
Browse files Browse the repository at this point in the history
* fix for new subtitles, fixes #48

* coding style correction

* according to suggestions

* change according to request,
minor improvements
  • Loading branch information
ferishili authored Dec 1, 2024
1 parent 89dbe1a commit aaf8358
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
61 changes: 59 additions & 2 deletions classes/local/paella_transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,40 @@ private static function get_captions($publication) {
foreach ($publication->media as $media) {
list($type1, $type2) = explode('/', $media->flavor, 2);
if ($type1 === 'captions') {
list($format, $lang) = explode('+', $type2, 2);
$lang = 'undefined';
$format = 'vtt'; // Default standard format.
$text = 'unknown';
// Prior to Opencast 15 or manually added subtitles in block opencast.
if (strpos($type2, 'vtt+') !== false) {
list($format, $lang) = explode('+', $type2, 2);
$text = $lang;
} else if (in_array($type2, ['delivery', 'prepared', 'preview', 'vtt']) && !empty($media->tags)) { // Opencast 15 coverage.
$tagdataarr = [];
foreach ($media->tags as $tag) {
// The safety checker.
if (!is_string($tag)) {
continue;
}
if (strpos($tag, 'lang:') === 0) {
$lang = substr($tag, strlen('lang:'));
$tagdataarr['lang'] = $lang;
}
if (strpos($tag, 'generator-type:') === 0) {
$tagdataarr['generatortype'] = substr($tag, strlen('generator-type:'));
}
if (strpos($tag, 'generator:') === 0) {
$tagdataarr['generator'] = substr($tag, strlen('generator:'));
}
if (strpos($tag, 'type:') === 0) {
$tagdataarr['type'] = substr($tag, strlen('type:'));
}
}
$text = self::prepare_caption_text($tagdataarr);
list($mimefiletype, $format) = explode('/', $media->mediatype, 2);
}
$captions[] = [
'lang' => $lang,
'text' => $lang,
'text' => $text,
'format' => $format,
'url' => $media->url,
];
Expand All @@ -267,6 +297,33 @@ private static function get_captions($publication) {
return $captions;
}

/**
* Generates the caption text in case the caption info is included in the tags (as introduced in Opencast 15)
* @param array $tagdataarr array of caption data.
* @return string the complied text for the caption.
*/
private static function prepare_caption_text($tagdataarr) {
$titlearr = [];
if (array_key_exists('lang', $tagdataarr)) {
$titlearr[] = $tagdataarr['lang'];
}
if (array_key_exists('generator', $tagdataarr)) {
$generator = ucfirst($tagdataarr['generator']);
$titlearr[] = $generator;
}
if (array_key_exists('generatortype', $tagdataarr)) {
$generatortype = $tagdataarr['generatortype'] === 'auto' ?
get_string('captions_generator_type_auto', 'mod_opencast') :
get_string('captions_generator_type_manual', 'mod_opencast');
$titlearr[] = "($generatortype)";
}
if (array_key_exists('type', $tagdataarr)) {
$type = ucfirst($tagdataarr['type']);
$titlearr[] = "($type)";
}
return implode(' ', $titlearr);
}

/**
* Returns the video data from Opencast in the format for the paella player.
* @param int $ocinstanceid Opencast instance id
Expand Down
3 changes: 3 additions & 0 deletions lang/en/opencast.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
$string['allowdownload'] = 'Allow students to download the video(s)';
$string['allvideos'] = 'All videos';

$string['captions_generator_type_auto'] = 'Auto generated';
$string['captions_generator_type_manual'] = 'Manually generated';

$string['date'] = 'Date';
$string['defaultuploadedvideotitle'] = 'Uploaded video';
$string['dnduploadvideofile'] = 'Upload video file to Opencast';
Expand Down

0 comments on commit aaf8358

Please sign in to comment.