secret
anonymous / mikehodgson.rb
Last active

  • Download Gist
mikehodgson.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
require 'time'
require 'optparse'
 
# Subtitle Class
class SubTitle
attr_accessor :start_time, :end_time, :text
def initialize(id, start_time, end_time, text)
@id = id
@start_time = Time.parse(start_time)
@end_time = Time.parse(end_time)
@text = text
end
def offset(ms, op)
if op == 'sub'
@start_time = @start_time - (ms.to_f/1000.0)
@end_time = @end_time - (ms.to_f/1000.0)
else
@start_time = @start_time + (ms.to_f/1000.0)
@end_time = @end_time + (ms.to_f/1000.0)
end
end
 
def to_s
"#{@id}\n#{@start_time.strftime('%H:%M:%S')},#{sprintf('%03d',@start_time.usec / 1000)} --> #{@end_time.strftime('%H:%M:%S')},#{sprintf('%03d',@end_time.usec / 1000)}\n#{@text}\n\n"
end
end
 
# Main Program
 
def check_options
if ARGV.empty?
ARGV <\s/)
subs << SubTitle.new(data[0],times[0],times[1],data[2..-1].join("\n"))
end
rescue
abort "Invalid file format, or file does not exist."
end
subs
end
 
def write_subtitles(fn, subs)
out_data = File.open(fn,'w')
subs.each {|s| out_data << s.to_s}
out_data.close
end
 
@options = {}
 
check_options
 
in_file = ARGV[0]
out_file = ARGV[1]
 
subtitles = read_subtitles(in_file)
 
subtitles.each {|s| s.offset(@options[:time], @options[:operation])}
 
write_subtitles(out_file, subtitles)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.