Skip to content

Instantly share code, notes, and snippets.

@msg7086
Last active August 29, 2015 14:21
Show Gist options
  • Save msg7086/8d68018cb0b33a98c849 to your computer and use it in GitHub Desktop.
Save msg7086/8d68018cb0b33a98c849 to your computer and use it in GitHub Desktop.
require 'fileutils'
def probe_audio eps
puts "Probing audio"
tracks = `mkvmerge -i \"#{eps}.mkv\"`
@a = tracks.lines.select{|t| t['audio']}.map do |t|
tid = t[/ID (\d+)/, 1]
type = t[/\((.*)\)$/, 1]
ext = nil
ext = 'flac' if type['FLAC']
ext = 'ac3' if type['AC3']
puts "#%-4s %-6s" % [tid, type]
next if ext.nil?
{:id => tid, :type => type, :ext => ext}
end.compact.first
puts "Using ##{@a[:id]}"
# build task chain
# MKV -> Demuxed file
demux_file = "#{eps}.#{@a[:ext]}"
file demux_file => "#{eps}.mkv" do |t|
sh "mkvextract tracks \"#{t.source}\" #{@a[:id]}:\"_#{t.name}\""
FileUtils.mv '_'+t.name, t.name
end
# Demuxed file -> aac
encode_file = "#{eps}.m4a"
file encode_file => demux_file do |t|
encode_audio t.source, t.name
end
@audio = encode_file
end
def encode_audio src, dst
sh "#{@qaac} -o \"_#{dst}\" \"#{src}\"" if src['flac']
sh "muxer -i \"#{src}\" -o \"_#{dst}\"" if src['aac'] || src['ac3']
FileUtils.mv '_'+dst, dst
end
def probe_video eps
# We are going to demux mkv into mp4
tracks = `mkvmerge -i \"#{eps}.mkv\"`
t = tracks.lines.select{|t| t['video']}.first
tid = t[/ID (\d+)/, 1]
type = t[/\((.*)\)$/, 1]
ext = nil
ext = '264' if type['264']
ext = '265' if type['265']
puts "#%-4s %-6s" % [tid, type]
@v = {:id => tid, :type => type, :ext => ext}
puts "Using ##{@v[:id]}"
# build task chain
# MKV -> chapter
@chap = "#{eps}.chap"
file @chap => "#{eps}.mkv" do |t|
sh "mkvextract chapters \"#{t.source}\" -s > \"#{t.name}\""
end
# MKV -> Demuxed file
demux_file = "#{eps}.#{@v[:ext]}"
file demux_file => "#{eps}.mkv" do |t|
sh "mkvextract tracks \"#{t.source}\" #{@v[:id]}:\"_#{t.name}\""
FileUtils.mv '_'+t.name, t.name
end
# Demuxed file -> Muxed file
muxed_file = "#{eps}.x.mp4"
file muxed_file => demux_file do |t|
sh "muxer -i \"#{t.source}\"?fps=24000/1001 -o \"#{t.name}\""
end
# MKV -> TC
tc_file1 = "#{eps}.tc1"
file tc_file1 => "#{eps}.mkv" do |t|
sh "mkvextract timecodes_v2 \"#{t.source}\" #{@v[:id]}:\"#{t.name}\""
end
# TC -> TC
tc_file2 = "#{eps}.tc2"
file tc_file2 => tc_file1 do |t|
sh "tctool -v 1 \"#{t.source}\" > \"#{t.name}\""
end
# Muxed file + TC -> Video
@video = "#{eps}.m4v"
file @video => [muxed_file, tc_file2] do |t|
v = t.source
tc = t.sources[1]
sh "timelineeditor --timecode \"#{tc}\" \"#{v}\" \"#{@video}\""
end
end
Process.setpriority(Process::PRIO_PROCESS, 0, 12) rescue nil
INI = 'defch.ini'
@type = :mkv
@qaac = "qaac --tvbr 100"
if File.exists? INI
File.readlines(INI).each do |l|
@type = :mp4 if l[/mp4/i]
@type = :mkv if l[/mkv/i]
@qaac = l.chomp if l[/qaac/i]
end
end
@type = :mp4 if File.exists? 'mp4'
@type = :mkv if File.exists? 'mkv'
dir = Rake.original_dir
Dir.chdir(dir)
puts "Executing in (#{dir})..."
if Rake.application.top_level_tasks.first != 'all'
eps = ENV['eps'] || '_DUMMY_'
eps = eps[0..-5]if eps[/.mkv$/]
eps[/\[(x26.)_/]
output = eps.gsub(/\[(x26.)_\w+\]/, "[#{$1}_aac]").ext(".#{@type}")
raise "File not found" if !File.exists? "#{eps}.mkv"
puts "%s.mkv => %s" % [eps, output]
probe_audio eps
if @type == :mp4
probe_video eps
task :video => @video
file output => [@audio, @video, @chap] do |t|
chapters = File.read(@chap).chomp
if chapters.empty?
sh "remuxer -o \"#{t.name}\" -i \"#{@video}\" -i \"#{@audio}\""
else
sh "remuxer -o \"#{t.name}\" -i \"#{@video}\" -i \"#{@audio}\" --chapter \"#{@chap}\""
end
end
else
task :video => []
file output => [@audio] do |t|
sh "mkvmerge -o \"#{t.name}\" --no-audio --no-subtitles --no-track-tags \"#{eps}.mkv\" \"#{@audio}\""
end
end
task :default => output
task :audio => @audio
end
task :all do
files = Dir["*flac*.mkv"]
files.each do |f|
sh "rake eps=\"#{f}\""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment