Skip to content

Instantly share code, notes, and snippets.

@gwang
Last active August 29, 2015 21:53
Show Gist options
  • Select an option

  • Save gwang/59be90835ab5d941324b to your computer and use it in GitHub Desktop.

Select an option

Save gwang/59be90835ab5d941324b to your computer and use it in GitHub Desktop.
Adjust the time stamps of the subtitles (srt) file
require 'pp'
require 'time'
class Item
attr_accessor :index, :start_time, :end_time, :text
def initialize(list)
@index = list[0]
@start_time, @end_time = list[1].split(' --> ').map{|a1| Time.parse(a1)}
@text = list[2..-1].join(' ')
end
# d in seconds, can be a negative value
def adjust(d)
@start_time = @start_time + d
@end_time = @end_time + d
end
def info
ret = "#{@index}\n"
ret += "#{@start_time.strftime("%H:%M:%S,%L")} --> #{@end_time.strftime("%H:%M:%S,%L")}\n"
ret += "#{@text}\n\n"
end
end
class Subtitles
attr_accessor :data
def initialize(filename)
@data = []
parse_file(filename)
end
def parse_file(filename)
tmp = File.open(filename).read.split("\n\n")
tmp.each {|t| @data << Item.new(t.split("\n"))}
end
def adjust_time_stamps
@data.each do |d|
d.adjust(-8.1)
end
end
def output(filename)
fp = File.open(filename, "w")
@data.each do |d|
fp.write(d.info)
end
fp.close
end
end
b = Subtitles.new('Wrong.In.Time.2011.srt')
b.adjust_time_stamps
b.output('In.Time.2011.srt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment