Presenting a fully automated DVD authoring rakefile

As promised, here is the script I use to change a set of AVI files
into a DVD disc image. It uses Rake and rmovie along with a whole
bunch of Linux utilities, and doesn’t require any aditional input from
the user beyond having the AVI files in the same directory as the
rakefile.

It goes through the following steps:

For each AVI file in the current directory:

  • Use transcode to convert it into a M2V video file and an AC3 audio
    track.

  • Use mplex to join these two files into a MPG file.

  • Use dvdauthor to convert the MPGs to a DVD file structure.

  • Use mkisofs to change the DVD file structure into a ISO image file,
    ready for burning.

  • Clean up by deleting everything except the AVI files, the ISO image
    and the MPG files (which take the longest time to create, and are
    useful if something goes wrong and I have to start over :)).

It’s probably too specific to be worth posting to Rubyforge or one of
its equivalents, so I’m sending it here in case someone else finds it
useful. There’s probably a lot I can improve here, so I’d be thankful
to hear any suggestions or criticisms.

=====================================================

require ‘rubygems’
require ‘rake’
require ‘rake/clean’
require ‘rmovie’

OUTPUT_RATIO = 1.6

SOURCE_FILES = FileList[’.avi’]
DESTINATION_FILES = SOURCE_FILES.gsub(‘avi’,‘mpg’)
CLEAN.include(’
.xml’,’*.cfg’,‘DVD’)

def adjust_clip(x, y)

clip_size = -1* (((x.to_i / OUTPUT_RATIO) - y.to_i) / 2).abs
int_clip = clip_size.to_i
return int_clip if int_clip % 2 == 0

return int_clip - 1

end

def transcode(filename)
nome, extensao = filename.split(’.’)
movie = RMovie::Movie.new(filename)

clip_size = adjust_clip(movie.frame_width, movie.frame_height)

bit_rate = ( movie.bit_rate / 1000 ).ceil

ratio = movie.frame_width.to_f / movie.frame_height.to_f

if ratio == ( 4.0 / 3.0 )
    aspect = 2
elsif ratio == ( 16.0 / 9.0 )
    aspect = 3
end

sh <<-FIM
transcode -i "#{filename}" -y ffmpeg --export_prof dvd-ntsc

–export_asr #{aspect} --pre_clip=#{clip_size},#{clip_size} -w
#{bit_rate} -o #{nome} -D0 -b224 -N 0x2000 -s2 -m #{nome}.ac3 -J
modfps=clonetype=3 --export_fps 29.97
FIM
end

def mplex(basename)
nome = basename.split(’.’)[0]
sh “mplex -f 8 -o #{nome}.mpg #{nome}.m2v #{nome}.ac3”
end

def write_xml_header(xml)
xml.write <<-HEADER





HEADER

end

def write_xml_footer(xml)
xml.write <<-FOOTER




FOOTER
end

rule ‘.mpg’ => [’.avi’] do |t|
nome = t.name.split(’.’)[0]
transcode(t.prerequisites[0])
mplex( nome )
rm “#{nome}.m2v”
rm “#{nome}.ac3”
end

rule ‘.mpg’ => [’.m2v’, ‘ac3’] do |t|
nome = t.name.split(’.’)[0]
mplex( nome )
rm “#{nome}.m2v”
rm “#{nome}.ac3”
end

file ‘ffmpeg.cfg’ do |t|
File.open(t.name, ‘w’) do |cfg|
cfg.write <<-FIM
[mpeg2video]
vrc_minrate=0
vrc_maxrate = 5000
vrc_buf_size = 1792
FIM
end
puts ‘ffmpeg.cfg escrito’
end

file ‘dvdauthor.xml’ => DESTINATION_FILES do |t|
File.open(t.name, ‘w’) do |xml|
write_xml_header(xml)
DESTINATION_FILES.sort.each do |file|
xml.write <<-LINHA

LINHA
end
write_xml_footer(xml)
end
end

file ‘DVD’ => [‘dvdauthor.xml’] do |t|
sh “dvdauthor -o #{t.name} -x #{t.prerequisites[0]}”
end

task ‘dvd.iso’ => [‘DVD’] do |t|
sh “mkisofs -o #{t.name} -dvd-video #{t.prerequisites[0]}”
end

task :default =>[‘ffmpeg.cfg’,‘dvd.iso’, :clean]

=======================================================

On 10/13/06, Bira [email protected] wrote:

As promised, here is the script I use to change a set of AVI files
into a DVD disc image. It uses Rake and rmovie along with a whole
bunch of Linux utilities, and doesn’t require any aditional input from
the user beyond having the AVI files in the same directory as the
rakefile.

Might be worth posting to http://www.doom9.org