This is the script I use to convert AVI files to MP4 for viewing on my Nokia N95:
Code: Select all
#!/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 Nokia N95.
# It is careful about maintaining the proper aspect ratio.
#
require 'getoptlong'
DEFAULT_ARGS = "-f mp4 -vcodec libxvid -maxrate 1000 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec mpeg4aac"
DEFAULT_AUDIO_BITRATE = 96
DEFAULT_VIDEO_BITRATE = 768
WIDTH = 320.0
HEIGHT = 240.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 <<EOF
mp4ize - encode videos to mp4 for an iPod
Usage: mp4ize file1.avi [file2.mpg [file3.asf [...]]]
Options:
-h : this help
-v : verbose
-a RATE : override default audio bitrate (#{DEFAULT_AUDIO_BITRATE})
-b RATE : override default video bitrate (#{DEFAULT_VIDEO_BITRATE})
EOF
exit
end
audio_bitrate = $options['-a'] || DEFAULT_AUDIO_BITRATE
video_bitrate = $options['-b'] || DEFAULT_VIDEO_BITRATE
ARGV.each do |infile|
outfile = infile.dup
ext = File.extname(outfile)
outfile.sub!(/#{ext}$/, '.mp4')
# open the file to figure out the aspect ratio
w, h = nil, nil
IO.popen("ffmpeg -i '#{infile}' 2>&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




