Presenting a fully automated DVD authoring rakefile

Discussion in 'Ruby' started by Bira, Oct 13, 2006.

  1. Bira

    Bira Guest

    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
    <dvdauthor dest="DVD">
    <vmgm />
    <titleset>
    <titles>
    <pgc>
    HEADER

    end

    def write_xml_footer(xml)
    xml.write <<-FOOTER
    </pgc>
    </titles>
    </titleset>
    </dvdauthor>
    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
    <vob file="#{file}" />
    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]



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



    --
    Bira
    http://compexplicita.blogspot.com
    http://sinfoniaferida.blogspot.com
    Bira, Oct 13, 2006
    #1
    1. Advertising

  2. On 10/13/06, Bira <> 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
    Richard Conroy, Oct 13, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rhino
    Replies:
    2
    Views:
    418
    Rhino
    Feb 11, 2005
  2. Jim Higson
    Replies:
    2
    Views:
    412
    Jukka K. Korpela
    Aug 23, 2004
  3. Replies:
    4
    Views:
    621
  4. Ingvald Straume (from Norway - Europe)

    Presenting a new(?) idea for free open source software development.

    Ingvald Straume (from Norway - Europe), Aug 7, 2005, in forum: C++
    Replies:
    10
    Views:
    590
    Ingvald Straume (from Norway - Europe)
    Aug 9, 2005
  5. John Gabriele
    Replies:
    1
    Views:
    164
    Suraj Kurapati
    Dec 5, 2006
Loading...

Share This Page