-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebvtt2srt.php
52 lines (43 loc) · 1.1 KB
/
webvtt2srt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Example command on Linux:
* php webvtt2srt.php file.vtt [file.srt]
*/
// Check command line mode
if (empty($argv) or $argc <2)
{
echo ("usage instructions: \n");
exit ("webvtt2srt.php file.vtt [file.srt]\n");
}
// Get file paths
$webVttFile = $argv[1];
// If srt filename was passed
if ($argc > 2)
{
$srtFile = $argv[2];
}
else
{
$info = pathinfo($argv[1]);
$srtFile = ($info['dirname'] ? $info['dirname'] . DIRECTORY_SEPARATOR : '')
. $info['filename']
. '.srt';
}
// Read the WebVTT file content into an array of lines
$lines = file($webVttFile);
// Convert all timestamp lines
// The first timestamp line is 3
$length = count($lines);
for ($index = 3; $index < $length; $index++)
{
// A line is a timestamp line if the second line above it is an empty line
if ( trim($lines[$index - 2]) === '' )
{
$lines[$index] = str_replace('.', ',', $lines[$index]);
}
}
// Remove 2 first lines of WebVTT format
unset($lines[0]);
unset($lines[1]);
// Concatenate all other lines into the result file
file_put_contents($srtFile, implode('', $lines));