secret
anonymous / aldric.rb
Last active

  • Download Gist
aldric.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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
# 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 }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.