Skip to content

Instantly share code, notes, and snippets.

@gunyarakun
Last active December 30, 2015 18:39
Show Gist options
  • Save gunyarakun/0f545508a55c9c7bda20 to your computer and use it in GitHub Desktop.
Save gunyarakun/0f545508a55c9c7bda20 to your computer and use it in GitHub Desktop.
.idx/.sub形式の字幕のオフセット時間をずらすスクリプト
#!/usr/bin/env ruby
if ARGV.length != 4
puts "#{$PROGRAM_NAME} in.idx out.idx secdiff ratio"
puts "ex.) #{$PROGRAM_NAME} in.idx out.idx 15.34 1.01"
exit(1)
end
in_path, out_path, time_diff, ratio = ARGV
time_diff = time_diff.to_f * 1000
ratio = ratio.to_f
timestamp_re = /\Atimestamp: (\d\d):(\d\d):(\d\d):(\d\d\d), filepos: ([A-Fa-f0-9]+)\z/
open(in_path, 'r') do |f|
open(out_path, 'wb') do |of|
f.each_line do |line|
m = timestamp_re.match(line.chomp)
if m
hour, min, sec, msec, filepos = m[1..-1]
t = ((hour.to_i * 60 + min.to_i) * 60 + sec.to_i) * 1000 + msec.to_i
t += time_diff
t *= ratio
t = t.to_i
m_msec = t % 1000
m_sec = (t / 1000) % 60
m_min = (t / 60000) % 60
m_hour = (t / 3600000)
o = sprintf("timestamp: %02d:%02d:%02d:%03d, filepos: %s\r\n", m_hour, m_min, m_sec, m_msec, filepos)
of.puts(o)
else
of.puts(line)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment