Created
September 22, 2010 10:00
-
-
Save sibprogrammer/591439 to your computer and use it in GitHub Desktop.
Merge two subtitles files into single one
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 | |
def load_subtitles(filename, as_hash = false) | |
subtitles = as_hash ? {} : [] | |
File.open(filename) do |file| | |
while !file.eof? do | |
record = [] | |
while (line = file.gets).strip != '' and !file.eof? do | |
record << line | |
end | |
break if 0 == record.size | |
if as_hash | |
subtitles[record[1].chomp] = { :index => record[0], :timeframe => record[1].chomp, :text => record[2,record.size-1] } | |
else | |
subtitles << { :index => record[0], :timeframe => record[1].chomp, :text => record[2,record.size-1] } | |
end | |
end | |
end | |
subtitles | |
end | |
def merge_subtitles(main_subtitles, addon_subtitles) | |
main_subtitles.each do |record| | |
if addon_subtitles.has_key?(record[:timeframe]) | |
record[:text] << addon_subtitles[record[:timeframe]][:text] | |
end | |
end | |
end | |
def print_subtitles(subtitles) | |
subtitles.each do |record| | |
puts record[:index] | |
puts record[:timeframe] | |
puts record[:text] | |
puts | |
end | |
end | |
if ARGV.length != 2 | |
puts "Usage: #{$0} main.srt addon.srt" | |
exit 0 | |
end | |
main_filename = ARGV.shift | |
addon_filename = ARGV.shift | |
main_subtitles = load_subtitles(main_filename) | |
addon_subtitles = load_subtitles(addon_filename, :as_hash => true) | |
merged_subtitles = merge_subtitles(main_subtitles, addon_subtitles) | |
print_subtitles(merged_subtitles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment