Adds vtt2srt script

- Original: https://github.com/tunggnu/WebVTT-to-SRT
This commit is contained in:
Nemo 2017-10-23 11:22:54 +05:30
parent 7da622f9e2
commit 58f93216b9
1 changed files with 52 additions and 0 deletions

52
vtt2srt Executable file
View File

@ -0,0 +1,52 @@
#!/bin/php
<?php
/**
* Example command on Linux:
* php webvtt2srt.php file.vtt [file.srt]
*/
// Check command line mode
if (empty($argv) or $argc <2)
{
exit ("vtt2srt 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));