This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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