在PHP中读取WebVTT文件
有没有人有使用PHP读取WebVTT(.vtt)文件的经验 我正在用CakePHP开发一个应用程序,在这里我需要阅读一堆vtt文件,并获取开始时间和相关文本 因此,作为该文件的一个示例: 00:00.999 --> 00:04.999 sentence one 00:04.999 --> 00:07.999 sentence two 00:07.999 --> 00:10.999 third sentence with a line break 00:10.999 --> 00:14.999 a fourth sentence on three lines 00:00.999 --> 00:04.999 第一句 00:04.999 --> 00:07.999 第二句 00:07.999 --> 00:10.999 第三句 断线 00:10.999 --> 00:14.999 第四句 三点 线 我需要能够提取如下内容: 00:00.999 sentence one 00:04.999 sentence two 00:07.999 third sentence with a line break 00:10.999 a fourth sentence on three lines在PHP中读取WebVTT文件,php,parsing,webvtt,vtt,Php,Parsing,Webvtt,Vtt,有没有人有使用PHP读取WebVTT(.vtt)文件的经验 我正在用CakePHP开发一个应用程序,在这里我需要阅读一堆vtt文件,并获取开始时间和相关文本 因此,作为该文件的一个示例: 00:00.999 --> 00:04.999 sentence one 00:04.999 --> 00:07.999 sentence two 00:07.999 --> 00:10.999 third sentence with a line break 00:10.999 --> 00:14.99
<?PHP
function send_reformatted($vtt_file){
// Add these headers to ease saving the output as text file
header("Content-type: text/plain");
header('Content-Disposition: inline; filename="'.$vtt_file.'.txt"');
$f = fopen($vtt_file, "r");
$line_new = "";
while($line = fgets($f)){
if (preg_match("/^(\d{2}:[\d\.]+) --> \d{2}:[\d\.]+$/", $line, $match)) {
if($line_new) echo $line_new."\n";
$line_new = $match[1];
} else{
$line = trim($line);
if($line) $line_new .= " $line";
}
}
echo $line_new."\n";
fclose($f);
}
send_reformatted("test.vtt");
?>
$subtitles = Subtitles::load('subtitles.vtt');
$blocks = $subtitles->getInternalFormat(); // array
foreach ($blocks as $block) {
echo $block['start'];
echo $block['end'];
foreach ($block['lines'] as $line) {
echo $line;
}
}
00:00.999第一句
00:04.999第二句
00:07.999带换行符的第三句话
00:10.999三行的第四句话
请注意,可能会有换行符,因此在每个时间戳之间没有设置行数
我的计划是搜索“->”,这是每个时间戳之间的公共字符串。有人知道如何最好地实现这一点吗?您可以这样做: 00:00.999 sentence one 00:04.999 sentence two 00:07.999 third sentence with a line break 00:10.999 a fourth sentence on three lines
<?PHP
function send_reformatted($vtt_file){
// Add these headers to ease saving the output as text file
header("Content-type: text/plain");
header('Content-Disposition: inline; filename="'.$vtt_file.'.txt"');
$f = fopen($vtt_file, "r");
$line_new = "";
while($line = fgets($f)){
if (preg_match("/^(\d{2}:[\d\.]+) --> \d{2}:[\d\.]+$/", $line, $match)) {
if($line_new) echo $line_new."\n";
$line_new = $match[1];
} else{
$line = trim($line);
if($line) $line_new .= " $line";
}
}
echo $line_new."\n";
fclose($f);
}
send_reformatted("test.vtt");
?>
$subtitles = Subtitles::load('subtitles.vtt');
$blocks = $subtitles->getInternalFormat(); // array
foreach ($blocks as $block) {
echo $block['start'];
echo $block['end'];
foreach ($block['lines'] as $line) {
echo $line;
}
}
这似乎满足了我的需要,即输出开始时间和任何后续文本行。我使用的文件相当小,因此使用PHP的file()函数将所有内容读入数组似乎是可以的;不过,我不确定这是否适用于大型文件
$file = 'test.vtt';
$file_as_array = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($file_as_array as $f) {
// Find lines containing "-->"
$start_time = false;
if (preg_match("/^(\d{2}:[\d\.]+) --> \d{2}:[\d\.]+$/", $f, $match)) {
$start_time = explode('-->', $f);
$start_time = $start_time[0];
echo '<br>';
echo $start_time;
}
// It's a line of the file that doesn't include a timestamp, so it's caption text. Ignore header of file which includes the word 'WEBVTT'
if (!$start_time && (!strpos($f, 'WEBVTT')) ) {
echo ' ' . $f . ' ';
}
}
}
$file='test.vtt';
$file_as_array=file($file,file_IGNORE_NEW_line | file_SKIP_EMPTY_line);
foreach($file_as_数组as$f){
//查找包含“->”的行
$start\u time=false;
如果(preg_match(“/^(\d{2}:[\d\.]+)-->\d{2}:[\d\.]+$/”,$f,$match)){
$start_time=explode('-->',$f);
$start_time=$start_time[0];
回声“
”;
echo$start_时间;
}
//这是一行不包含时间戳的文件,所以它是标题文本。忽略包含单词“WEBVTT”的文件头
如果(!$start\u time&(!strpos($f,'WEBVTT')){
回声“.$f.”;
}
}
}
要解析文件,可以使用如下库:
00:00.999 sentence one
00:04.999 sentence two
00:07.999 third sentence with a line break
00:10.999 a fourth sentence on three lines
<?PHP
function send_reformatted($vtt_file){
// Add these headers to ease saving the output as text file
header("Content-type: text/plain");
header('Content-Disposition: inline; filename="'.$vtt_file.'.txt"');
$f = fopen($vtt_file, "r");
$line_new = "";
while($line = fgets($f)){
if (preg_match("/^(\d{2}:[\d\.]+) --> \d{2}:[\d\.]+$/", $line, $match)) {
if($line_new) echo $line_new."\n";
$line_new = $match[1];
} else{
$line = trim($line);
if($line) $line_new .= " $line";
}
}
echo $line_new."\n";
fclose($f);
}
send_reformatted("test.vtt");
?>
$subtitles = Subtitles::load('subtitles.vtt');
$blocks = $subtitles->getInternalFormat(); // array
foreach ($blocks as $block) {
echo $block['start'];
echo $block['end'];
foreach ($block['lines'] as $line) {
echo $line;
}
}
如果你能更详细地阐述你所面临的具体问题,你可能会得到更好的答案。最好的方法是什么?有数百种不同的方法,到目前为止你都尝试了什么?怎么样
strpos()
?如果您需要一些基本的开始,请在所有行上创建,使用strpos()
检查-->
,使用preg_match()
解析该行,将以下所有行读入字符串,直到到达空行,重复直到到达EOF。有关解析WebVTT文件的更多信息,请参阅。具体问题是,我正在寻找实现示例中给出的输出的最佳方法。因此,详细说明如何使用您的建议(strpos和preg_match)来实现这一点是我真正想要的信息。这似乎就是输出整个文件?如果您将我给出的示例复制到一个名为test.vtt的文件中,然后运行send_reformatted(“test.vtt”),它将按照提供的格式输出该文件。在发布上述代码之前,我已经对其进行了测试,效果良好。我必须检查一下为什么它对你不起作用。我在ubuntu上。您的操作系统是什么?在Apache、PHP5.3下的CentOS服务器上运行。事实上,我已经想出了一些几乎能满足我需要的东西,所以我会在完成后将其全部发布。你上面回复中的preg_匹配非常有用,并在我的解决方案中使用了它。谢谢。我不得不调整正则表达式来解释hh:mm:ss,但其他的原因是它按预期工作