# Presenting a fully automated DVD authoring rakefile

**URL:** <https://www.ruby-forum.com/t/presenting-a-fully-automated-dvd-authoring-rakefile/72685>\
**Category:** Ruby\
**Created:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/presenting-a-fully-automated-dvd-authoring-rakefile/72685 "2006-10-18T10:40:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bira](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c89c15/32.png) [@Bira](https://www.ruby-forum.com/u/Bira)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/presenting-a-fully-automated-dvd-authoring-rakefile/72685/1 "2006-10-18T10:40:38Z")

</div>

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]

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

---

<div class="post-metadata">

**Author:** ![Bira](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c89c15/32.png) [@Bira](https://www.ruby-forum.com/u/Bira)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/presenting-a-fully-automated-dvd-authoring-rakefile/72685/2 "2006-10-18T10:40:56Z")

</div>

On 10/13/06, Bira [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.doom9.org)
