0) {
writeOutput($captionArray,$descArray);
}
else {
// do nothing
// an error has occurred (and hopefully an error code was displayed)
}
}
else {
echo '0'; // insuficcient parameters were passed by URL
}
}
function toArray($type,$content) {
//standardize on \n for eol character
$content = preg_replace("/\r\n|\r|\n/m", "\n", trim($content));
$cues = explode("\n\n",$content);
$n = 0; // counter of kept cues
if (sizeof($cues)>1) {
if (trim(strtoupper($cues[0])) == 'WEBVTT') { //spec requires WEBVTT on the first line
$i=1;
while ($i < sizeof($cues)) {
$cue = explode("\n",$cues[$i]);
if (sizeof($cue) >=2) { // this seems to be a valid cue. Keep it
$times = explode(' --> ',$cue[0]);
$c['type'][$n] = $type;
$c['start'][$n] = toSeconds(trim($times[0]));
$c['end'][$n] = toSeconds(trim($times[1]));
$cueText = $cue[1];
if(sizeof($cue) > 2) { // this is a multi-line cue
$j=2;
while ($j < sizeof($cue)) {
// ensure there's one space between cues, but not more than one
$cueText .= ' '.trim($cue[$j]);
$j++;
}
}
$c['text'][$n] = $cueText;
$n++;
}
$i++;
}
}
else {
echo '2'; // this is not a WebVTT file
}
}
else {
echo '3'; // too few cues were found
}
return $c;
}
function toSeconds($time) {
$seconds = 0;
if ($time) {
$parts = explode(':',$time);
$i=0;
while ($i < sizeof($parts)) {
$seconds = $seconds * 60 + str_replace(',','.',$parts[$i]);
$i++;
}
return $seconds;
}
}
function writeOutput($c,$d) {
// merge arrays and sort by start time
$allCues = array_merge_recursive($c,$d);
array_multisort($allCues['start'],$allCues['end'],$allCues['type'],$allCues['text']);
//echo "
";
//var_dump($allCues);
//echo "
";
$numCues = sizeof($allCues['start']);
if ($numCues > 0) {
echo ''."\n";
$divOpen = false;
$descOpen = false;
$i=0;
while ($i < $numCues) {
$cue = trim($allCues['text'][$i]);
$cueType = $allCues['type'][$i];
// make transcript more readable by breaking divs on parenthetical or bracketed text
// brackets contain unspoken information such as sounds or speaker names
//standardize on square brackets []
// $cue = @preg_replace("(|(", "[", $cue); // this wasn't working
// $cue = @preg_replace(")|)", "]", $cue); // trying to account for various ( symbols
$cue = str_replace('(','[',$cue);
$cue = str_replace(')',']',$cue);
if ($descOpen && $cueType != 'desc') {
// close description div
echo "\n
\n";
$descOpen = false;
$divOpen = false;
}
// if cue contains bracketed content, or is the start of a new description
// both make good breaking points
if (substr($cue,0,1) == '[' || ($cueType =='desc' && !$descOpen)) {
if ($divOpen) {
// close the preceding div
echo "\n\n\n";
if ($cueType == 'desc') {
// this is the start of a new description (following a caption)
echo ''."\n";
// preface description with a hidden prompt for non-visual users
echo '
Description: ';
$descOpen = true;
}
else {
// this is the start of a new caption with bracketed content
echo "
\n";
}
}
else { // div is not already open
if ($cueType == 'desc') {
// this is the start of a new description
echo '
'."\n";
// preface description with a hidden prompt for non-visual users
echo '
Description: ';
$descOpen = true;
}
else {
// this is a new caption
echo "
\n";
$divOpen = true;
}
}
$bracketEndPos = strpos($cue,']');
if (substr($cue,0,1) == '[') {
echo '';
echo '['.ucfirst(trim(substr($cue,1,$bracketEndPos-1))).']';
echo "\n";
if (strlen($cue) > ($bracketEndPos+1)) {
// there is additional content in this cue, not just bracketed content
$cue = substr($cue,$bracketEndPos+1);
}
else {
// nothing in this queue but bracketed text. Close the div
echo "
\n";
$divOpen = false;
$cue = '';
}
}
}
// now write the cue
if ($cue != '') {
if ($cueType == 'desc') {
echo $cue.' ';
}
else {
// add attributes to make this cue clickable and highlightable
// Note: tabindex="0" will be added dynamically based on user preference
echo '
';
echo $cue.' ';
echo " ";
}
}
$i++;
}
if ($divOpen) { //close it
echo "
\n";
}
echo "
\n"; //close transcript
}
}
?>