Skip to content

Instantly share code, notes, and snippets.

/mikehodgson.rb Secret

Created October 5, 2009 04:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/52cba60961f3723fddf3 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment