secret
anonymous / FelipeGiotto
Last active

  • Download Gist
FelipeGiotto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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
shift_subtitle
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
#!/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
srt_test.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
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.