#!/usr/bin/env perl

use strict;
use warnings;

my $st_number = 1;
my $framerate = 25;

# If your subtitles are out of sync, use this to shift the time index however you like
my $timeshift = 0;

while (<>) {
  chomp;

  if (/^{(\d+)}{(\d+)}(.*?)$/) {
    my ($frame_start, $frame_end, $text) = ($1, $2, $3);
  
    $text =~ s/\|/\n/g;

    my $time_start = frames_to_time($frame_start);
    my $time_end   = frames_to_time($frame_end);
  
    print "$st_number\n";
    print "$time_start --> $time_end\n";
    print "$text\n\n";
  
    $st_number++;
  }
}

sub frames_to_time {
  my $frames = shift;
  my $time = $frames / $framerate;

  # Apply the arbitrary timeshift if any
  $time += $timeshift;
  
  my $h = int($time / 3600);
  $time -= $h * 3600;
  
  my $m = int($time / 60);
  $time -= $m * 60;
  
  my $s = int($time);
  $time -= $s;
  
  my $ms = $time;
  
  return sprintf("%02d:%02d:%02d,%03d", $h, $m, $s, $ms * 1000);
}

