class MkvMuxer

Constants

VERSION

Attributes

command[R]

Public Class Methods

crc32_of(file) click to toggle source
# File lib/mkvmuxer/mkvmuxer.rb, line 73
def crc32_of(file)
  File.open(file, 'rb') { |f| Zlib.crc32 f.read }.to_s(16)
end
new(path, force = false) click to toggle source
# File lib/mkvmuxer/mkvmuxer.rb, line 14
def initialize(path, force = false)
  path = path.gsub('\\', '/')

  @command  = ''
  @mkv      = Dir[File.join(path, '*.mkv')].first rescue nil
  @ass      = Dir[File.join(path, '*.ass')].first rescue nil
  @chapters = Dir[File.join(path, '*.xml')].first rescue nil
  @output   = "#{@mkv}_"

  @fonts = [].tap { |fonts|
    %w(ttf ttc otf).each { |format| fonts << Dir[File.join(path, 'font*', "*.#{format}")] }
  }.flatten.compact.uniq

  raise Exception, 'No mkv or ass found'       if !@mkv || !@ass
  raise Exception, 'Target mkv already exists' if !force && File.exists?(@output)
end

Public Instance Methods

apply_crc32!() click to toggle source
# File lib/mkvmuxer/mkvmuxer.rb, line 67
def apply_crc32!
  crc32 = MkvMuxer.crc32_of @output
  FileUtils.move(@output, @output.gsub(/CRC32/, crc32)[0..-2])
end
merge!(mkvmerge = 'mkvmerge') click to toggle source
# File lib/mkvmuxer/mkvmuxer.rb, line 55
def merge!(mkvmerge = 'mkvmerge')
  Open3.popen3(mkvmerge, *@command) do |stdin, stdout, stderr, wait_thr|
    exit_code = wait_thr.value

    if exit_code != 0
      err = stderr.read.chomp
      err = stdout.read.chomp if err.strip.empty?
      raise Exception, err
    end
  end
end
prepare(language = 'Italian', fonts = true, chapters = true) click to toggle source
# File lib/mkvmuxer/mkvmuxer.rb, line 31
def prepare(language = 'Italian', fonts = true, chapters = true)
  options = []
  options << { opt: '-o',              val: @output                        }
  options << { opt: '--default-track', val: '0'                            }
  options << { opt: '--track-name',    val: "0:#{language}"                }
  options << { opt: '--language',      val: "0:#{language[0..2].downcase}" }
  options << { opt: '--no-chapters --chapters', val: chapters              } if @chapters

  @fonts.each { |font|
    options << { opt: '--attachment-mime-type', val: 'application/x-truetype-font' }
    options << { opt: '--attach-file',          val: font                          }
  } if fonts

  options << { val: @ass }
  options << { val: @mkv }

  @command = [].tap { |cmd|
    options.each { |option|
      cmd << option[:opt] if option[:opt]
      cmd << option[:val]
    }
  }
end