Windows command processor cmd.exe
executing batch files is designed for running commands and applications. It is not designed for making modifications in text files. There are lots of other scripting languages which have features to easily modify text files like VBScript, JScript, PowerShell, Python, Perl, ... So the usage of Windows command processor for this task is the worst decision which can be made by a someone.
However, this is nevertheless an easy to achieve task with using JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.
@echo off
if not exist "%~dp0jrepl.bat" goto :EOF
if not exist "WEBVTT" goto :EOF
call "%~dp0jrepl.bat" "(\d{2}:\d{2}:\d{2}\.\d{3} --\> \d{2}:\d{2}:\d{2}\.\d{3})(\r?\n[^\r\n]+\r?\n[^\r\n])" "$1 align:middle line:84%%$2" /M /F "WEBVTT" /O -
call "%~dp0jrepl.bat" "(\d{2}:\d{2}:\d{2}\.\d{3} --\> \d{2}:\d{2}:\d{2}\.\d{3})(\r?\n[^\r\n])" "$1 align:middle line:90%%$2" /M /F "WEBVTT" /O -
The batch file first checks if there is a file named WEBVTT
in current directory and immediately exits if this condition is not true, see Where does GOTO :EOF return to?
The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks next if JREPL.BAT really exists in directory of the batch file and exits if this condition is not true.
Then JREPL.BAT is used twice to run two regular expression replaces on file WEBVTT
to change its content to required format.
Let's look on first search expression:
(\d{2}:\d{2}:\d{2}\.\d{3} --\> \d{2}:\d{2}:\d{2}\.\d{3})(\r?\n[^\r\n]+\r?\n[^\r\n])
(
...)
... defines a marking group. The string found by the expression inside this first marking group is back-referenced with $1
in replace string to keep this part of found and matched string unmodified. The regular expression inside the marking group is used to definitely find a line with a string like 00:00:02.185 --> 00:00:04.990
anywhere inside the line.
\d{2}
... means that exactly two digits must be found for a positive match.
\.
... the dot means any character and therefore must be escaped with a backslash to be interpreted as literal character.
\d{3}
... means that exactly three digits must be found for a positive match.
\>
... also >
needs to be escaped with a backslash to be interpreted as literal character.
(
...)
... defines a second marking group. The string found by the expression inside this second marking group is back-referenced with $2
in replace string to keep this part of found string also unmodified.
\r?\n
... there must be a line-feed with optionally a carriage return before after for example 00:00:02.185 --> 00:00:04.990
.
[^\r\n]+
... the next line must have one or more characters not being a carriage return or a line-feed. So the search expression is negative on next line being an empty line. Please note that a line containing just spaces/tabs is not an empty line. It is a blank line because of containing only white space characters, but it is not an empty line.
\r?\n[^\r\n]
... and finally there must be one more DOS/Windows or UNIX line ending and next line must contain also a character for a positive match.
So the first search expression matches lines with subtitles with two lines.
Therefore the first replace string contains 84%
whereby the percent sign must be escaped with one more percent sign because otherwise the Windows command processor would interpret %
as beginning of an environment variable or batch file argument reference.
The second search expression is similar to first one, but is only positive on a line with the time values not having already more text after second time value and there is one more line below which is not an empty line.
Both regular expressions are multi-line expressions which require JREPL option /M
.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains also %~dp0
... drive and path of argument 0 being the batch file itself.
echo /?
goto /?
if /?
jrepl.bat /?