lib/mkv2m4v/transcoder.rb in mkv2m4v-0.2.0 vs lib/mkv2m4v/transcoder.rb in mkv2m4v-0.3.0
- old
+ new
@@ -1,7 +1,9 @@
require "fileutils"
require "colorize"
+require "shellwords"
+require "iso639/language"
module Mkv2m4v
class Transcoder
def initialize(file, options = {})
@file = file
@@ -24,11 +26,15 @@
FileUtils.mkdir_p tmp_dir
end
def extract
puts "==> Extracting #{video_id}:video.#{video_ext} #{audio_id}:audio.#{audio_ext} ".magenta
- system "mkvextract tracks #{@file.filename.inspect} #{video_id}:#{video_file} #{audio_id}:#{audio_file}"
+ command = "mkvextract tracks"
+ command << " #{escape(@file.filename)}"
+ command << " #{video_id}:#{escape(video_file)}"
+ command << " #{audio_id}:#{escape(audio_file)}"
+ system command
end
def transcode_avc
if video_format == "AVC"
puts "==> Assuming pass through for h.264 video track".yellow.on_black
@@ -40,11 +46,15 @@
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
- system "ffmpeg -i #{audio_file.inspect} -acodec libfaac -ac 2 -ab 160k #{audio_basename}.aac"
+ command = "ffmpeg"
+ command << " -i #{escape(audio_file)}"
+ command << " -acodec libfaac -ac 2 -ab 160k"
+ command << " #{escape(audio_basename)}.aac"
+ system command
end
end
def transcode_ac3
if audio_format == "AC-3"
@@ -52,23 +62,28 @@
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
- system "ffmpeg -i #{audio_file.inspect} -acodec ac3 -ac #{max_audio_channels} -ab #{max_audio_bit_rate}k #{audio_basename}.ac3"
+ 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"
+ system command
end
end
def remux
puts "==> Remuxing everything into an M4V container".magenta
command = "MP4Box"
- command << " -add #{video_basename.inspect}.h264:lang=en:name=\"AVC Video\" "
- command << " -add #{audio_basename.inspect}.aac:lang=en:group=1:delay=84:name=\"Stereo\" "
+ 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 #{audio_basename.inspect}.ac3:lang=en:group=1:delay=84:disable:name=\"AC3\" "
+ command << " -add #{escape(audio_basename)}.ac3:lang=#{audio_language.alpha3_terminology}:group=1:delay=84:disable:name=\"AC3\" "
end
- command << " -new #{m4v_file.inspect}"
+ command << " -new #{escape(m4v_file)}"
+ puts command
system command
end
def cleanup
FileUtils.rm_rf tmp_dir
@@ -96,10 +111,14 @@
else
video_format.gsub(/\W/, "").downcase
end
end
+ def video_language
+ @file.ideal_video_track.language || @options[:languages].first || UnknownLanguage
+ end
+
def audio_basename
::File.join(tmp_dir, "audio")
end
def audio_file
@@ -116,10 +135,14 @@
def audio_ext
audio_format.gsub(/\W/, "").downcase
end
+ def audio_language
+ @file.ideal_audio_track.language || @options[:languages].first || UnknownLanguage
+ end
+
def max_audio_channels
[@file.ideal_audio_track.channel_count, 6].min
end
def max_audio_bit_rate
@@ -135,7 +158,13 @@
end
def m4v_file
::File.join(dir, ::File.basename(@file.name, ".*") + ".m4v")
end
+
+ def escape(str)
+ Shellwords.escape(str)
+ end
+
+ UnknownLanguage = Iso639::Language.new("", "", "", "", "")
end
end