How to batch convert .vtt subtitles into .srt?
February 26, 2022 9:44 PM   Subscribe

I have several videos that have subtitles encoded in .vtt format. I'd like to convert these to .srt format. Is there a (preferably free) tool that will batch convert the files for me? I've only managed to find tools that can process a single file at a time. In case it matters, each video/subtitle pair is in a separate folder and I'd prefer the output to go into the same folder as the source file.
posted by NoneOfTheAbove to Computers & Internet (12 answers total)
 
What operating system do you need to do this under, and what's going to be the easiest way to identify a batch of files for processing? For example, each video/subtitle pair is in a separate folder but are all of the folders containing the .vtt files you want to process themselves grouped within a single outer folder?

Reason I ask: I haven't looked it up specifically, but based on past experience I am absolutely certain that ffmpeg will do what you need to, one file at a time, quickly and efficiently. Ffmpeg is a command-line tool, and as such it's easily scriptable. So if your task were my task, the way I'd approach it would be to write a tiny script that loops over all the .vtt files you want to process, feeding each one into ffmpeg as an input file and telling ffmpeg to make an output .srt next to the input .vtt. But the most convenient language in which to write such a script and the most convenient way to tell it where to find its input files are going to depend on the OS.
posted by flabdablet at 10:58 PM on February 26, 2022 [2 favorites]


For example, on a Mac OS or Linux box with ffmpeg properly installed, opening a Terminal window and entering the following command lines would work:
cd /path/to/outermost/video/collection/folder
find . -name '*.vtt' | while IFS= read -r f; do ffmpeg -i "$f" "${f%vtt}srt"; done

posted by flabdablet at 11:19 PM on February 26, 2022 [3 favorites]


By the way, I just downloaded a random .vtt file and confirmed that the commands above do indeed get the job done on a Debian box with ffmpeg installed.
posted by flabdablet at 11:23 PM on February 26, 2022 [3 favorites]


Sorry, missed a subtlety: ffmpeg will want to consume data from the standard input stream unless it's told not to, and that will interfere with the while read loop's consumption of a pathname list from that same input stream. Corrected commands are
cd /path/to/outermost/video/collection/folder
find . -name '*.vtt' | while IFS= read -r f; do ffmpeg -nostdin -i "$f" "${f%vtt}srt"; done
If this recipe is indeed going to be saved as a script, it would be worth breaking the loop into separate lines and adding a few more options so that the list of pathnames piped from find into while read is delimited by NUL codes instead of newlines. This allows the script to keep working properly even if the pathnames of the .vtt files are pathological enough to contain embedded newline characters.
find . -name '*.vtt' -print0 |
while IFS= read -rd "" f
do ffmpeg -nostdin -i "$f" "${f%vtt}srt"
done

posted by flabdablet at 11:43 PM on February 26, 2022 [2 favorites]


Elgindy's VTT to SRT converter, works on a group of files.
posted by kschang at 12:26 AM on February 27, 2022


Meh, need '-maxdepth 1' and '-type f' unless you want to go recursive or accidentally hit a directory named 'something.vtt'. :P

find . -maxdepth 1 -type f -name '*.vtt' | perl -lne 'system qw(ffmpeg -nostdin -i),$_,s/vtt$/srt/r and die "$!"'

posted by zengargoyle at 12:57 AM on February 27, 2022


If you want to go wild.....

perl -e 'for(<*.vtt>){next unless -f; system qw(ffmpeg -nostdin -i),$_,s/vtt$/srt/r and die"$!"}'

posted by zengargoyle at 1:20 AM on February 27, 2022 [1 favorite]


It's always 'ffmpeg' for audio/visual things. It's a Swiss Army Knife of handling almost everything imaginable. flabdablet is correct in just asking what operating system you are using and other little specifics and comfort level level. Converting .vtt to .srt is pretty easy. It all depends.....
posted by zengargoyle at 1:26 AM on February 27, 2022


Best answer: Here's how I'd do it on Windows:

First, download the latest BtbN Windows build of ffmpeg.

This comes as a Zip file, not a Windows installer, so there's a bit of manual installation work to do. Open the downloaded Zip file with the Windows file explorer, which will show it as a compressed folder. Inside the compressed folder you'll see another folder also named ffmpeg-master-latest-win64-gpl-shared. Right-click on that and choose Copy. Open a second file explorer window, navigate to C:\Program Files, right-click on a blank spot inside that folder and choose Paste. Give the administrative permission that Windows will ask for in order to complete that operation.

Next, copy the following script and paste it into a new Notepad window. The last line of the script is the one beginning with %ffmpeg%. Please use copy and paste for this; do not attempt to type it in by hand, as script interpreters are not the slightest bit forgiving about punctuation errors.
set src=vtt
set dst=srt

for /f "delims=" %%F in ('dir /s /b "%ProgramFiles%\ffmpeg.exe"') do set ffmpeg="%%~F"

for %%F in (%*) do call :convert "%%~F"
goto :eof

:convert
pushd "%~1" 2>nul && goto convert-folder
if "%~x1" == ".%src%" goto convert-file
goto :eof

:convert-folder
for /f "delims=" %%F in ('dir /s /b "*.%src%"') do call :convert-file "%%~F"
popd
goto :eof

:convert-file
%ffmpeg% -y -nostdin -loglevel warning -i "%~1" "%~dpn1.%dst%"
Save this to your desktop as vtt-srt.cmd. You will need to change the file type option in Notepad's "Save as" box from Text to All Files before finally clicking Save; if you don't, Notepad will save it as vtt-srt.cmd.txt instead, which won't work. If all goes well, you will see a new icon labelled vtt-srt and containing a couple of gear wheels appear on your desktop.

To use the script, drag any .vtt file, or any folder containing .vtt files, or any folder containing subfolders that contain .vtt files, or any multi-selected combination of the above, and drop it onto the vtt-srt desktop icon. You will see a black window open with a bunch of furiously scrolling text inside, after which a converted .srt file will appear next to each of the original .vtt files.
posted by flabdablet at 11:12 AM on February 27, 2022 [1 favorite]


Response by poster: flabdablet: You are fantastic and you've made my day. That worked beautifully! Thank you so much.
posted by NoneOfTheAbove at 7:57 PM on February 27, 2022


Dragging and dropping files and/or folders onto a little script is quite often a good way to get batch processing done, and despite the howlingly terrible design of the cmd scripting language, cmd scripts are still the least fiddly way to make this happen in Windows.

Glad to find out that this one worked for you - thanks for letting me know.
posted by flabdablet at 2:14 AM on February 28, 2022 [1 favorite]


By the way, the best online resources I know of for learning about cmd scripting are Rob van der Woude's scripting pages and the reference guide at ss64.com.

If you're interested in the gory details of what the script above is doing, or might want to write modified versions of it as a basis for further batch processing tasks, those are good places to start. I'm also happy to answer questions about it here; AskMe threads stay open for a year.
posted by flabdablet at 2:36 AM on February 28, 2022 [1 favorite]


« Older Online music jam sites?   |   Wearable panic device for elderly. Do these work? Newer »
This thread is closed to new comments.