Skip to content

Instantly share code, notes, and snippets.

/FelipeGiotto Secret

Created October 5, 2009 03:00
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/0d72bde27e08332c6df4 to your computer and use it in GitHub Desktop.
Save anonymous/0d72bde27e08332c6df4 to your computer and use it in GitHub Desktop.
# srt.rb
def shift(content, operation, time)
content.gsub /(.*)/ do |line|
if line.match %r{^(\d+):(\d+):(\d+),(\d+)( --> )(\d+):(\d+):(\d+),(\d+)$}
shift_time($1, $2, $3, $4, operation, time) + $5 + shift_time($6, $7, $8, $9, operation, time)
else
line
end
end
end
def shift_time(hour, minute, second, milisecond, operation, increment)
original_miliseconds = milisecond.to_i + second.to_i * 1000 + minute.to_i * 60000 + hour.to_i * 3600000
increment_miliseconds = (increment.gsub(',', '.').to_f * 1000).to_i
increment_miliseconds = -increment_miliseconds if operation == 'sub'
new_miliseconds = original_miliseconds + increment_miliseconds
Time.at(new_miliseconds / 1000).gmtime.strftime('%H:%M:%S') + ',' + (new_miliseconds % 1000).to_s.rjust(3, '0')
end
#!/usr/bin/ruby
require 'optparse'
require 'srt'
selected_options = {}
parser = OptionParser.new do |options|
options.banner = "Usage: shift_subtitle [options] input_file output_file"
options.separator ''
options.on "--operation [OP]", "OP should be 'add' or 'sub'" do |op|
selected_options[:operation] = op
end
options.on "--time TIME", "TIME should be a comma-separated decimal value (like 1,359)" do |time|
selected_options[:time] = time
end
options.on_tail "Example: shift_subtitle --operation add --time 1,234 subtitle.srt subtitle_shift.srt"
end
parser.parse!(ARGV)
unless ARGV.length == 2
puts "Missing input and/or output file"
exit
end
raise('Invalid operation. Should be "add" or "sub"') unless ['add', 'sub'].include?(selected_options[:operation])
output = shift(File.read(ARGV[0]), selected_options[:operation], selected_options[:time])
File.open ARGV[1], 'w' do |f|
f.print output
end
require 'test/unit'
require 'srt'
class SrtTest < Test::Unit::TestCase
def setup
@srt = < 01:31:54,893
the government is implementing a new policy...
646
01:31:54,928 --> 01:31:57,664
In connection with a dramatic increase
in crime in certain neighbourhoods,
SRT
end
def test_shift_time_zero
assert_equal "01:02:03,000", shift_time("01","02","03","000", 'add', '0')
assert_equal "01:02:04,000", shift_time("01","02","03","000", 'add', '1')
assert_equal "00:00:04,124", shift_time("00","00","03","001", 'add', '1,123')
assert_equal "00:00:02,000", shift_time("00","00","03","000", 'sub', '1')
assert_equal "01:02:03,123", shift_time("01","02","03","123", 'add', '0')
assert_equal "00:00:06,030", shift_time("00","00","04","980", 'add', '1,050')
assert_equal "00:00:05,980", shift_time("00","00","07","030", 'sub', '1,050')
assert_equal "00:00:05,001", shift_time("00","00","07","000", 'sub', '1,999')
assert_equal "00:00:04,124", shift_time("00","00","04","001", 'add', '0,123')
assert_equal "01:31:51,211", shift_time("01","31","51","210", 'add', '0,001')
end
def test_no_shift
srt_shift = < 01:31:54,893
the government is implementing a new policy...
646
01:31:54,928 --> 01:31:57,664
In connection with a dramatic increase
in crime in certain neighbourhoods,
SRT_SHIFT
assert_equal srt_shift, shift(@srt, 'add', '0')
end
def test_shift_more_one_milisecond
srt_shift = < 01:31:54,894
the government is implementing a new policy...
646
01:31:54,929 --> 01:31:57,665
In connection with a dramatic increase
in crime in certain neighbourhoods,
SRT_SHIFT
assert_equal srt_shift, shift(@srt, 'add', '0,001')
end
def test_shift_more_one_second
srt_shift = < 01:31:55,893
the government is implementing a new policy...
646
01:31:55,928 --> 01:31:58,664
In connection with a dramatic increase
in crime in certain neighbourhoods,
SRT_SHIFT
assert_equal srt_shift, shift(@srt, 'add', '01')
end
def test_shift_minus_one_second
srt_shift = < 01:31:53,893
the government is implementing a new policy...
646
01:31:53,928 --> 01:31:56,664
In connection with a dramatic increase
in crime in certain neighbourhoods,
SRT_SHIFT
assert_equal srt_shift, shift(@srt, 'sub', '01')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment