class Mkv2m4v::Transcoder

Constants

UnknownLanguage

Public Class Methods

new(file, options = {}) click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 8
def initialize(file, options = {})
  @file = file
  @options = options
end

Public Instance Methods

run() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 13
def run
  setup
  extract
  transcode_avc
  transcode_aac
  transcode_ac3
  remux
  cleanup
end

Private Instance Methods

audio_basename() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 119
def audio_basename
  ::File.join(tmp_dir, "audio")
end
audio_ext() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 135
def audio_ext
  audio_format.to_s.gsub(/\W/, "").downcase
end
audio_file() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 123
def audio_file
  [audio_basename, audio_ext].join(".")
end
audio_format() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 131
def audio_format
  @file.ideal_audio_track.format
end
audio_id() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 127
def audio_id
  @file.ideal_audio_track.id.to_i - 1
end
audio_language() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 139
def audio_language
  @file.ideal_audio_track.language || @options[:languages].first || UnknownLanguage
end
cleanup() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 87
def cleanup
  FileUtils.rm_rf tmp_dir
end
dir() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 151
def dir
  @options[:dir] || ::File.dirname(@file.filename)
end
escape(str) click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 163
def escape(str)
  Shellwords.escape(str)
end
extract() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 29
def extract
  puts "==> Extracting #{video_id}:video.#{video_ext} #{audio_id}:audio.#{audio_ext} ".magenta
  command = "mkvextract tracks"
  command << " #{escape(@file.filename)}"
  command << " #{video_id}:#{escape(video_file)}"
  command << " #{audio_id}:#{escape(audio_file)}"
  sh command
end
m4v_file() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 159
def m4v_file
  ::File.join(dir, ::File.basename(@file.name, ".*") + ".m4v")
end
max_audio_bit_rate() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 147
def max_audio_bit_rate
  [@file.ideal_audio_track.bit_rate_kbps, 640].min
end
max_audio_channels() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 143
def max_audio_channels
  [@file.ideal_audio_track.channel_count, 6].min
end
remux() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 75
def remux
  puts "==> Remuxing everything into an M4V container".magenta
  command = "MP4Box"
  command << " -add #{escape(video_basename)}.h264:lang=#{video_language.alpha3_terminology}:name=\"AVC Video\""
  command << " -add #{escape(audio_basename)}.aac:lang=#{audio_language.alpha3_terminology}:group=1:delay=84:name=\"Stereo\""
  unless @skip_ac3
    command << " -add #{escape(audio_basename)}.ac3:lang=#{audio_language.alpha3_terminology}:group=1:delay=84:disable:name=\"AC3\" "
  end
  command << " -new #{escape(m4v_file)}"
  sh command
end
setup() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 25
def setup
  FileUtils.mkdir_p tmp_dir
end
sh(command) click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 167
def sh(command)
  puts command
  system command
end
tmp_dir() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 155
def tmp_dir
  ::File.join(dir, ::File.basename(@file.name, ".*") + "-tracks")
end
transcode_aac() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 46
def transcode_aac
  if audio_format == "AAC"
    puts "==> Assuming pass through for AAC audio track".yellow.on_black
  else
    puts "==> Transcoding #{audio_format} to Stereo AAC audio track".magenta
    command = "ffmpeg"
    command << " -i #{escape(audio_file)}"
    command << " -acodec libfdk_aac -ac 2 -ab 160k"
    command << " #{escape(audio_basename)}.aac"
    sh command
  end
end
transcode_ac3() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 59
def transcode_ac3
  if audio_format == "AC-3"
    puts "==> Assuming pass through for AC-3 audio track".yellow.on_black
  elsif audio_format == "AAC"
    puts "==> Skipping AC-3 surround audio track".yellow.on_black
    @skip_ac3 = true
  else
    puts "==> Transcoding #{audio_format} to Surround AC-3 audio track".magenta
    command = "ffmpeg"
    command << " -i #{escape(audio_file)}"
    command << " -acodec ac3 -ac #{max_audio_channels} -ab #{max_audio_bit_rate}k"
    command << " #{escape(audio_basename)}.ac3"
    sh command
  end
end
transcode_avc() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 38
def transcode_avc
  if video_format == "AVC"
    puts "==> Assuming pass through for h.264 video track".yellow.on_black
  else
    puts "==> ERROR: Wrong Video format".white.on_red
  end
end
video_basename() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 91
def video_basename
  ::File.join(tmp_dir, "video")
end
video_ext() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 107
def video_ext
  if video_format == "AVC"
    "h264"
  else
    video_format.to_s.gsub(/\W/, "").downcase
  end
end
video_file() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 95
def video_file
  [video_basename, video_ext].join(".")
end
video_format() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 103
def video_format
  @file.ideal_video_track.format
end
video_id() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 99
def video_id
  @file.ideal_video_track.id.to_i - 1
end
video_language() click to toggle source
# File lib/mkv2m4v/transcoder.rb, line 115
def video_language
  @file.ideal_video_track.language || @options[:languages].first || UnknownLanguage
end