secret
anonymous / johnmcdonald.rb
Last active

  • Download Gist
johnmcdonald.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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
# Following is class and code to run following
class Changesrt
require 'optparse'
require 'optparse/time'
require 'ostruct'
 
def initialize
@options = OpenStruct.new
@options.operation = 1 # default is add; set to -1 for subtract
@options.time = 0 # is multiplied operation (1/-1) to add or subtract time
@options.inputfile = ""
@options.outputfile = ""
end
 
def parse(args)
opts = OptionParser.new do |opts|
opts.banner = "Usage: shift_subtitle.rb [options]"
opts.on("-o", "--operation [OP]", [:add, :sub]) do |o|
if o.to_s == "add"
then
@options.operation = 1
elsif o.to_s == "sub"
@options.operation = -1
else
puts "Invalid operation argument [-o [add|sub]"
exit
end
end
# process time option. -t ss,mmm
opts.on("-t", "--time [TIME]", "Provide time to add or subtract") do |time|
if time.nil? ||time.sub(",", '') !~ /^[0-9]*$/ || time.split(",").length != 2
puts "Invalid time argument [-t seconds,milliseconds]"
exit
else
timeopt = time.split(",")
@options.time = timeopt[0].to_f*1000 + timeopt[1].to_f
end
end
 
# No argument, shows at tail. This will print an options summary.
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
 
opts.parse!(args)
end
 
def update_srt
data_in = File.new(@options.inputfile + ".srt")
data_out = File.new(@options.outputfile + ".srt","w")
#data.each do |line|
seqline = false
time = false
until data_in.eof
line = data_in.readline.chomp
# sequence number line
if !seqline
if line =~ /^[1-9]+$/
then
seqline = true
else
raise "'#{line}' is not a valid sequence number"
end
else
# start/stop times line
if time == false
if line.sub!(" --> ",",")
then
time = true
# create array of time pieces
time_array = line.split(",")
# update times: add time argument x operation (1 or -1)
start_time = Time.parse(time_array[0]).to_f*1000 + @options.time*@options.operation + time_array[1].to_f
q1,r1 = start_time.divmod 1000 # convert to secs/millisecs
stop_time = Time.parse(time_array[2]).to_f*1000 + @options.time*@options.operation + time_array[3].to_f
q2,r2 = stop_time.divmod 1000
# reformat line
line = Time.at(q1).strftime("%H:%M:%S") + "," + r1.to_i.to_s +
" --> " +
Time.at(q2).strftime("%H:%M:%S") + "," + r2.to_i.to_s
else
raise " '#{line}' missing times"
end
end
end
#write out data
do_output(data_out, line)
if line.length == 0
seqline = false
time = false
end
end
data_in.close
data_out.close
end
 
def do_output(file, data)
file.puts(data)
end
 
def print_options
puts @options.inspect
end
 
def set_files(args)
@options.inputfile = args[0]
@options.outputfile = args[1]
end
end
shifter = Changesrt.new
# call the parser to parse the options
shifter.parse(ARGV)
 
# at this point ARGV ahould contain only input and output file names
if ARGV.length != 2
then
puts "Error: missing input or output SRT file"
exit
else
# set the input and output file names
shifter.set_files(ARGV)
end
 
shifter.update_srt()
 
# Following is shift_subtitle.gemspec
require 'rubygems'
spec = Gem::Specification.new do |spec|
spec.name = 'shift_subtitle'
spec.summary = 'A library for updating movies start/stop times'
spec.description = %{This program is command line script in Ruby
that will read an SRT file, and output another one with the new
calculated times.}
spec.author = 'John McDonald'
spec.email = 'allegrojm@cox.net'
spec.executables << 'shift_subtitle'
spec.files = Dir['lib/shift_subtitle.rb'] + Dir['bin/*']
spec.version = '1.0.0'
end
 
# Following goes in bin directory of project
#!/usr/bin/env ruby
require 'shift_subtitle'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.