Converting vtt to srt subtitles with a simple Perl script

This post was written by eli on September 19, 2025
Posted Under: perl

I tried to use ffmpeg to convert an vtt file to srt, but that didn’t work at all:

$ ffmpeg -i in.vtt out.srt
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)

I tried a whole lot of suggestions from the Internet, and eventually I gave up.

So I wrote a simple Perl script to get the job done. It took about 20 minutes, because I made a whole lot of silly mistakes:

#!/usr/bin/perl

use warnings;
use strict;

my $n = 1;
my $l;

my $timestamp_regex = qr/[0-9]+:[0-9]+:[0-9:\.]+/; # Very permissive

while (defined ($l = <>)) {
  my ($header) = ($l =~ /^($timestamp_regex --> $timestamp_regex)/);
  next unless (defined $header);

  $header =~ s/\./,/g;

  print "$n\n";
  print "$header\n";

  $n++;

  while  (defined ($l = <>)) {
    last unless ($l =~ /[^ \t\n\r]/); # Nothing but possibly whitespaces

    print $l;
  }
  print "\n";
}

$n--;
print STDERR "Converted $n subtitles\n";

Maybe not a piece of art, and it can surely be made more accurate, but it does the job with simply

$ ./vtt2srt.pl in.vtt > out.srt
Converted 572 subtitles

And here’s why Perl is a pearl.

Reader Comments

This is a useful minimal VTT-to-SRT approach. Beyond the header and comma/dot timing conversion, it helps to validate the output too: sequential cue numbering, ordered non-overlapping timestamps, non-zero cue durations, and text that survives the round trip without blank cues. When I need to hand the result to video editors, I usually keep a browser-based conversion and QA pass together so malformed WebVTT cues are caught before delivery.

#1 
Written By AllSubConverter on October 17th, 2026 @ 06:07

Add a Comment

required, use real name
required, will not be published
optional, your blog address