Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Web Video Text Tracks Format subtitles #2077

Merged
merged 3 commits into from Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/mime.conf
Expand Up @@ -15,6 +15,7 @@ wav audio/wav
webm video/webm
ogv video/ogg
mp4 video/mp4
vtt text/vtt

tgz !application/octet-stream
tar !application/x-gtar
Expand Down
35 changes: 35 additions & 0 deletions inc/media.php
Expand Up @@ -2453,4 +2453,39 @@ function media_supportedav($mime, $type=NULL){
return in_array($mime, $supportedAv);
}

/**
* Return track media files with the same base name
* but extensions that indicate kind and lang.
* ie for foo.webm search foo.sub.lang.vtt, foo.cap.lang.vtt...
*
* @param string $src - ID of media file
* @return array - array(mediaID => array( kind, srclang ))
*
* @author Schplurtz le Déboulonné <Schplurtz@laposte.net>
*/
function media_trackfiles($src){
$kinds=array(
'sub' => 'subtitles',
'cap' => 'captions',
'des' => 'descriptions',
'cha' => 'chapters',
'met' => 'metadata'
);

$files = array();
$re='/\\.(sub|cap|des|cha|met)\\.([^.]+)\\.vtt$/';
$baseid=pathinfo($src, PATHINFO_FILENAME);
$pattern=mediaFN($baseid).'.*.*.vtt';
$list=glob($pattern);
foreach($list as $track) {
if(preg_match($re, $track, $matches)){
$files[$baseid.'.'.$matches[1].'.'.$matches[2].'.vtt']=array(
$kinds[$matches[1]],
$matches[2],
);
}
}
return $files;
}

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
11 changes: 11 additions & 0 deletions inc/parser/xhtml.php
Expand Up @@ -1777,6 +1777,7 @@ function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $rende
* Embed video(s) in HTML
*
* @author Anika Henke <anika@selfthinker.org>
* @author Schplurtz le Déboulonné <Schplurtz@laposte.net>
*
* @param string $src - ID of video to embed
* @param int $width - width of the video in pixels
Expand All @@ -1794,6 +1795,7 @@ function _video($src, $width, $height, $atts = null) {

$posterUrl = '';
$files = array();
$tracks = array();
$isExternal = media_isexternal($src);

if ($isExternal) {
Expand All @@ -1805,6 +1807,7 @@ function _video($src, $width, $height, $atts = null) {
$extensions = array('webm', 'ogv', 'mp4');
$files = media_alternativefiles($src, $extensions);
$poster = media_alternativefiles($src, array('jpg', 'png'));
$tracks = media_trackfiles($src);
if(!empty($poster)) {
$posterUrl = ml(reset($poster), '', true, '&');
}
Expand Down Expand Up @@ -1833,6 +1836,14 @@ function _video($src, $width, $height, $atts = null) {
$fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
}

// output each track if any
foreach( $tracks as $trackid => $info ) {
list( $kind, $srclang ) = array_map( 'hsc', $info );
$out .= "<track kind=\"$kind\" srclang=\"$srclang\" ";
$out .= "label=\"$srclang\" ";
$out .= 'src="'.ml($trackid, '', true).'">'.NL;
}

// finish
$out .= $fallback;
$out .= '</video>'.NL;
Expand Down