Skip to content

Instantly share code, notes, and snippets.

/akshaygupta.rb Secret

Created October 5, 2009 03:40
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/b0822d661c735cd3c161 to your computer and use it in GitHub Desktop.
Save anonymous/b0822d661c735cd3c161 to your computer and use it in GitHub Desktop.
require 'optparse'
require 'time'
opts = {}
optp = OptionParser.new do |o|
o.banner = "Usage: shift.rb --operation (add/sub) --time (seconds,milliseconds) input_file output_file"
opts[:operation] = nil;o.on( '-o', '--operation ADD/SUB', 'Add/subtract time in the .srt file' ){|v|opts[:operation] = v}
opts[:time] = 0.0;o.on( '-t', '--time TIME', 'Specify time in "seconds, milliseconds" form (11,222)'){|t|opts[:time] = t}
o.on( '-h', '--help', 'Display this screen' ){puts o; exit}
end
optp.parse!
opts[:operation]=="add"? op = "+": op = "-"
File.open(ARGV[1], 'w') do |f|
f.puts(File.read(ARGV[0]).gsub(/^(.*) --> (.*)$/){[$1,$2].map{|i|(t=(Time.parse(i)).send(op,opts[:time].tr(',','.').to_f)).strftime("%H:%M:%S,#{t.usec}00")[0,12] } * ' --> '})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment