Skip to content

Instantly share code, notes, and snippets.

/aldric.rb Secret

Created October 5, 2009 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/c60e13f3f43f22823f73 to your computer and use it in GitHub Desktop.
Save anonymous/c60e13f3f43f22823f73 to your computer and use it in GitHub Desktop.
# This program assumes that it is getting a properly formatted time string.
# This means that it'll be like 6,400 or 00:04:00,200
require 'optparse'
options = {}
optparse = OptionParser.new do |opts|
opts.banner = 'Usage: fix_timing --operation add --time 2,200 infile outfile'
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit
end
opts.on('-o', '--operation OP', [:add, :remove, :subtract], 'add or subtract') do |f|
options[:operation] = f
end
opts.on('-t', '--time TIME', String, 'format: 2,200 or 00:00:02,200') do |f|
options[:time] = f
end
end
optparse.parse!
if ARGV.size != 2
puts "There should be an infile and an oufile. Unexpected number of arguments."
Kernel.exit
end
options[:in] = ARGV[0]
options[:out] = ARGV[1]
class Integer
def sec_to_ms
self * 1000
end
def min_to_ms
(self * 60).sec_to_ms
end
def hr_to_ms
(self * 60).min_to_ms
end
end
def convert_time_to_int time
return time.to_i if ( !time.include?(',') && !time.include?(':') )
mod_time = 0
if time.include? ','
temp = time.split ','
mod_time += temp[1].to_i
time = temp[0]
end
if time.include? ':'
time = time.split(':')
mod_time += time[0].to_i.hr_to_ms
mod_time += time[1].to_i.min_to_ms
mod_time += time[2].to_i.sec_to_ms
else
mod_time += time.to_i.sec_to_ms
end
mod_time
end
# Takes two strings and converts them to an integer.
# Then, adds or subtracts as requested.
def fix_time time, mod, operation
time = convert_time_to_int time
mod = convert_time_to_int mod
new_time = (operation == :add) ? time + mod : time - mod
format_time new_time
end
# Takes an integer : time in milliseconds.
def format_time time
temp = time % 1000
# This first one is milliseconds, so justify by 3.
text = "#{temp}".rjust(3, "0")
time /= 1000
temp = time % 60
temp = temp.to_s.rjust(2, "0")
text = "#{temp},#{text}"
time /= 60
temp = time % 60
temp = temp.to_s.rjust(2, "0")
text = "#{temp}:#{text}"
time /= 60
temp = time % 60
temp = temp.to_s.rjust(2, "0")
text = "#{temp}:#{text}"
end
srt = nil
File.open(options[:in], 'r'){ |f| srt = f.readlines("\r\n\r\n") }
data = []
srt.each do |sub|
temp = sub.split("\r\n")
index = temp[0]
start_time, end_time = temp[1].split("-->")
text = temp[2..-1]
data << [index, start_time.strip, end_time.strip, text]
end
srt = ""
data.map! do |sub|
new_start = fix_time sub[1], options[:time], options[:operation]
new_end = fix_time sub[2], options[:time], options[:operation]
[sub[0], new_start, new_end, sub[3]]
end
data.each do |sub|
srt << sub[0] << "\r\n"
srt << sub[1] < " << sub[2] << "\r\n"
sub[3].each do |txt|
srt << txt << "\r\n"
end
srt << "\r\n"
end
File.open(options[:out], 'w'){|f| f << srt }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment