Specyfikacja formatu EBU (1991 rok; przewiduje, że napisy znajdą się na dyskietce ;))
Przykładowy parser
Przykładowe wykorzystanie
Tak to wygląda
Aczkolwiek nie wiem o co chodzi z kodowaniem. Niby jest ono w pliku podane, ale jednak to nie to.
<?php
require 'EBUParser.php';
function formatTimecode(EBUTimeCode $timecode)
{
return sprintf('%02d:%02d:%02d.%02d', $timecode->hour, $timecode->minute, $timecode->second, $timecode->frame);
}
function formatText($text, $codepage = null)
{
if($codepage !== null)
$text = iconv($codepage, 'utf-8', $text);
return nl2br($text);
}
$filename = 'przyklad.stl';
$subtitles = \EBUParser::Parse($filename);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<table>
<?php
foreach($subtitles->subtitles as $subtitle) {
?>
<tr>
<td><?= formatTimecode($subtitle->timeCodeIn) ?></td>
<td><?= formatTimecode($subtitle->timeCodeOut) ?></td>
<td><?= formatText($subtitle->text, $subtitles->codepage) ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
class EBUTimeCode
{
public $hour;
public $minute;
public $second;
public $frame;
public function __construct($stream)
{
$this->hour = self::GetNumber($stream);
$this->minute = self::GetNumber($stream);
$this->second = self::GetNumber($stream);
$this->frame = self::GetNumber($stream);
}
private static function GetNumber($stream)
{
return ord(fread($stream, 1));
}
}
class EBUSubtitle
{
public $timeCodeIn;
public $timeCodeOut;
public $text;
public function __construct($stream)
{
fseek($stream, 5, SEEK_CUR);
$this->timeCodeIn = new EBUTimeCode($stream);
$this->timeCodeOut = new EBUTimeCode($stream);
fseek($stream, 3, SEEK_CUR);
$this->text = self::GetSubtitleText($stream);
}
private function GetSubtitleText($stream)
{
$text = fread($stream, 112);
$text = str_replace(chr(0x8a), "\n", $text);
$text = trim($text, chr(0x8f));
return $text;
}
}
class EBUSubtitles
{
public $codepage;
public $subtitles;
}
class EBUParser
{
public static function Parse($filename)
{
$subtitles = new \EBUSubtitles();
$file = fopen($filename, 'rb');
$subtitles->codepage = (int)fread($file, 3);
fseek($file, 238);
$subtitleCount = (int)fread($file, 5);
fseek($file, 1024);
for($i = 0; $i < $subtitleCount; $i++)
$subtitles->subtitles[] = new \EBUSubtitle($file);
print "\n";
return $subtitles;
}
}