-
-
Save anonymous/d900ab61c47bdcd0edc7 to your computer and use it in GitHub Desktop.
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
# 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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment