декодирование данных временной метки vtt - PullRequest
0 голосов
/ 30 марта 2019

У меня есть несколько сотен строк из файла субтитров VTT, пример из заголовка:

00:01:03.500 --> 00:01:03.510 align:start position:0%
<c.colorCCCCCC>fourth guess it came from a broken</c><c.colorE5E5E5> home
 </c>

00:01:03.510 --> 00:01:08.140 align:start position:0%
<c.colorCCCCCC>fourth guess it came from a broken</c><c.colorE5E5E5> home
a<00:01:04.580><c> father</c><00:01:05.580><c> not</c><00:01:05.820><c> being</c><00:01:05.880><c> there</c><00:01:06.890><c> my</c><00:01:07.890><c> mother</c></c>

00:01:08.140 --> 00:01:08.150 align:start position:0%
a<c.colorE5E5E5> father not being there my mother
 </c>

00:01:08.150 --> 00:01:13.429 align:start position:0%
a<c.colorE5E5E5> father not being there my mother</c>
<c.colorE5E5E5>getting<00:01:09.150><c> married</c><00:01:09.630><c> and</c><00:01:11.360><c> the</c><00:01:12.360><c> abuse</c></c><c.colorCCCCCC><00:01:12.659><c> started</c><00:01:13.049><c> at</c></c>

00:01:13.429 --> 00:01:13.439 align:start position:0%
<c.colorE5E5E5>getting married and the abuse</c><c.colorCCCCCC> started at
 </c>

Файлы субтитров VTT довольно запутанны, но цель состоит в том, чтобы собрать все слова внутри тегов меток времени и самих меток времени. Я думал, preg матч, но не знаю, как это сделать

$pattern = "<([^;]*)>";
preg_match_all($pattern, $lineContent, $allintag);

Это то, что я получил, но остановился там.

array(
00:01:03.510,
00:01:04.580,
00:01:05.58,
00:01:05.820,
00:01:05.880,
00:01:06.890,
00:01:07.890,
00:01:08.140,
00:01:09.150,
00:01:09.630,
00:01:11.360,
00:01:12.360,
00:01:12.659,
00:01:13.049
)
array(
'fourth guess it came from a broken home',
'father',
'not',
'being',
'there',
'my',
'mother',
'getting',
'married',
'and',
'the',
'abuse',
'started',
'at'
)

1 Ответ

0 голосов
/ 30 марта 2019

Вы можете использовать

'~<(?<time>\d{2}:\d{2}:\d{2}\.\d+)><c>\s*(?<text>.*?)</c>~'

Если группы времени и текста не используются последовательно

'~<(?<time>\d{2}:\d{2}:\d{2}\.\d+)>|<c>\s*(?<text>.*?)</c>~'

См. Демонстрационную версию regex .

Подробности

  • < - < символ
  • (?<time>\d{2}:\d{2}:\d{2}\.\d+) - группа "время": 2 цифры, :, 2 цифры, :, 2 цифры, ., а затем 1+ цифры
  • > - > char
  • <c> - литерал <c> текст
  • \s* - 0+ пробелов
  • (?<text>.*?) - «Текст» группы: любые 0+ символов, кроме символов разрыва строки, как можно меньше
  • </c> - литерал </c> текст.

См. PHP демо :

$lineContent = "<00:01:13.650><c> time</c><00:01:13.920><c> and</c> 
 <00:01:14.780><c> that's</c><00:01:15.780><c> what</c>";
if (preg_match_all('~<(?<time>\d{2}:\d{2}:\d{2}\.\d+)><c>\s*(?<text>.*?)</c>~', $lineContent, $allintag)) {
    print_r($allintag["time"]);
    print_r($allintag["text"]);
}

Вывод:

Array ( [0] => 00:01:13.650 [1] => 00:01:13.920 [2] => 00:01:14.780 [3] => 00:01:15.780 )
Array ( [0] => time [1] => and  [2] => that's  [3] => what )
...