#!/usr/bin/ruby -w # # Copyright (C) 2007 Thomer M. Gil [http://thomer.com/] # Thanks to Brian Moore for bugfixes. # # This program is free software. You may distribute it under the terms of # the GNU General Public License as published by the Free Software # Foundation, version 2. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # # This program converts video files to mp4, suitable to be played on an iPod. # It is careful about maintaining the proper aspect ratio. # require 'getoptlong' DEFAULT_ARGS = "-f mp4 -vcodec mpeg4 -maxrate 1000 -b 700 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac -ab 192 -s 320x240 -aspect 16:9" DEFAULT_AUDIO_BITRATE = 128 DEFAULT_VIDEO_BITRATE = 400 WIDTH = 480.0 HEIGHT = 320.0 $options = {} opts = GetoptLong.new( [ "-a", GetoptLong::REQUIRED_ARGUMENT ], # audio bitrate [ "-h", GetoptLong::NO_ARGUMENT ], # help [ "-b", GetoptLong::REQUIRED_ARGUMENT ], # video bitrate [ "-v", GetoptLong::NO_ARGUMENT ] # verbose ) opts.each { |opt, arg| $options[opt] = arg } if $options['-h'] puts <&1") do |pipe| pipe.each_line do |line| puts line if line.match(/Video:.+ (\d+)x(\d+)/) w, h = $1.to_f, $2.to_f end end end begin aspect = w/h rescue puts "Couldn't figure out aspect ratio." exit end height = (WIDTH / aspect.to_f).to_i height -= 1 if height % 2 == 1 pad = ((HEIGHT - height.to_f) / 2.0).to_i pad -= 1 if pad % 2 == 1 File.unlink(outfile) if File.exists?(outfile) cmd = "ffmpeg #{DEFAULT_ARGS} -i '#{infile}' -s #{WIDTH.to_i}x#{height} -padtop #{pad} -padbottom #{pad} -ab #{audio_bitrate} -b #{video_bitrate} '#{outfile}'" puts cmd if $options['-v'] system cmd end