Created
December 18, 2009 02:51
-
-
Save lucasuyezu/259252 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
unless ARGV.size == 2 | |
puts | |
puts "Usage: fix_subtitles.rb /path/to/file miliseconds_offset" | |
puts "Example: If subtitles appear AFTER the sound, use -2000 to bring them back 2 seconds" | |
puts | |
puts "DISCLAMIER: I only tested them with srt files." | |
puts | |
exit() | |
end | |
file_in_name, offset, file_out_name = ARGV[0], ARGV[1], ARGV[0] + ".out" | |
output = "" | |
open(file_in_name).each_line do |line| | |
output << line.gsub(/\d\d:\d\d:\d\d,\d\d\d/) do |s| | |
timestamp = s[0,2].to_i * 3_600_000 | |
timestamp += s[3,2].to_i * 60_000 | |
timestamp += s[6,2].to_i * 1000 | |
timestamp += s[9,3].to_i | |
timestamp += offset.to_i | |
hours = timestamp / 3_600_000 | |
timestamp -= hours * 3_600_000 | |
minutes = timestamp / 60_000 | |
timestamp -= minutes * 60_000 | |
seconds = timestamp / 1_000 | |
timestamp -= seconds * 1_000 | |
miliseconds = timestamp | |
sprintf "%02d:%02d:%02d,%03d", hours, minutes, seconds, miliseconds | |
end | |
end | |
open(file_out_name, 'w+') do |file_out| | |
file_out.puts output | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment