Skip to content

Instantly share code, notes, and snippets.

@RoxasShadow
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save RoxasShadow/9173247 to your computer and use it in GitHub Desktop.

Select an option

Save RoxasShadow/9173247 to your computer and use it in GitHub Desktop.
Given two .ass files, it creates a new file with the OP/ED lines of the first one replaced by the respective lines of the second one.
#! /usr/bin/env ruby
abort "usage: [ruby] #{File.basename $0} <ass> <op/ed>" if ARGV.length != 2
source = File.read(ARGV[0]).split(/\n/).reject(&:empty?)
lyrics = File.read(ARGV[1]).split(/\n/).reject { |line|
tag = line.split(?,)[3]
tag != 'OP' && tag != 'ED'
}
File.open("#{ARGV[0].split(?.)[0..-2].join}_OP_ED.ass", ?w) { |ass|
source.each { |line|
source_tag = line.split(?,)[3]
lyrics_tag = lyrics.first.split(?,)[3] if lyrics.any?
if source_tag == lyrics_tag
timing = line.split(?,)[0..8].join ?,
text = lyrics.shift.split(?,)[9..-1].join ?,
line = "#{timing},#{text}"
end
ass.puts line
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment