Wednesday, July 25, 2012

Perl Script For Subtitle Conversion

This has nothing to do with Unified Communications.  But I don't have another blog for video editing.  Thus I post it here for anyone who needs it.

I've been using SVDTS plugin for Sony Vegas Pro to export the timecode from AVCHD video (.mts) to a subtitle file.  Having the timecode as a subtitle gives you the flexibility to turn it on or off at will.  If the timecode was "burned in", it'll display on the screen all the time.

With subtitle on:


With subtitle off:

However, the SVDTS plugin exports subtitle in Sony DVD Architect format, which is not a recognized format for other software (such as Corel VideoStudio, Adobe Premiere Pro, etc.).  It'd be better if it was in SubRip (.srt) format, which is more popular and well recognized.

DVD Architect format:

SubRip format:

Perl is the perfect tool for this kind of task:
if ($#ARGV != 1) {
  print "\nUsage: perl sub2srt.pl sub_filename srt_filename\n";
  exit;
 }

if (-e $ARGV[1]) {
  print "\nFile $ARGV[1] already exists!\n";
 }

open (SubFile, "< $ARGV[0]") or die "Couldn't open $ARGV[0] for reading: $!\n";
open (SrtFile, "> $ARGV[1]") or die "Couldn't open $ARGV[1] for writing: $!\n";

my($n) = 1;    #SRT subtitle index

while () {
  next if /^$/;    #Skip blank lines
  my($line) = $_;
  my($start) = substr($line, 5, 8);
  my($end) = substr($line, 17, 8);
  my($content) = substr($line, 29);
  print SrtFile "$n\n";
  print SrtFile "$start,000 --> $end,000\n";
  print SrtFile "$content\n";
  $n++;
 }

close (SubFile);
close (SrtFile);

 Save above codes as sub2srt.pl.  The command line syntax is "perl sub2srt.pl myfile.sub myfile.srt".



1 comment:

  1. Thank you so much. I had been trying to make this work for almost half a day. It took 1/2 hour after reading this.

    ReplyDelete