#!/usr/bin/ruby -w def time_from_str(str) if str =~ /^(\d+):(\d+):(\d+)(,\d+)?$/ h, m, s = $1, $2, $3 Time.local(2006, 1, 1, h.to_i, m.to_i, s.to_i) else raise ArgumentError, "Invalid syntax for time: #{str}" end end if ARGV.size != 3 $stderr.puts "I need three parameters" $stderr.puts "#{$0} file.srt ref_srt_time ref_movie_time" exit(1) end filename = ARGV.shift srt_time_str = ARGV.shift movie_time_str = ARGV.shift # Convert reference times to proper Time objects start_time = Time.local(2006, 1, 1) srt_time = time_from_str(srt_time_str) movie_time = time_from_str(movie_time_str) factor = (srt_time - start_time) / (movie_time - start_time) # For performance h1, m1, s1, us1 = nil, nil, nil, nil h2, m2, s2, us2 = nil, nil, nil, nil t1, t2 = nil, nil line, count = nil, nil File.readlines(filename).each_with_index do |line,count| if line =~ /^(\d\d):(\d\d):(\d\d),(\d+) --> (\d\d):(\d\d):(\d\d),(\d+)/ h1, m1, s1, us1, h2, m2, s2, us2 = $1, $2, $3, $4, $5, $6, $7, $8 h1, m1, s1, us1 = h1.to_i, m1.to_i, s1.to_i, us1.to_i * 1_000 h2, m2, s2, us2 = h2.to_i, m2.to_i, s2.to_i, us2.to_i * 1_000 t1 = Time.local(2006, 1, 1, h1, m1, s1, us1) t2 = Time.local(2006, 1, 1, h2, m2, s2, us2) t1 = Time.at((t1.to_i - start_time.to_i) * factor) t2 = Time.at((t2.to_i - start_time.to_i) * factor) line = "#{t1.hour.to_s.rjust(2, '0')}:#{t1.min.to_s.rjust(2, '0')}:#{t1.sec.to_s.rjust(2, '0')},#{t1.usec / 1000} --> #{t2.hour.to_s.rjust(2, '0')}:#{t2.min.to_s.rjust(2, '0')}:#{t2.sec.to_s.rjust(2, '0')},#{t2.usec / 1000}\r\n" end $stderr.puts "#{count} lines..." if count % 100 == 0 and count != 0 print line end