#!/bin/bash # ipodconv script # Copyright (C) 2007 Greg Methvin (greg@methvin.net) # 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 script converts video files to the iPod video's mp4 format. It # attempts to maintain the aspect ratio of the video while fitting the # size of the iPod's display. IFS= # use these arguments to ffmpeg for all converted videos BASE_ARGS="-f mp4 -vcodec xvid -maxrate 1000 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac" # define dimensions of screen WIDTH=320 HEIGHT=240 # default bitrate values AUDIO_BITRATE=128 VIDEO_BITRATE=400 function usage { echo "Usage: `basename $0` [-a ] [-b ] [...]" echo "options:" echo " -a Set bitrate in kbps of output audio (default: $AUDIO_BITRATE)" echo " -b Set bitrate in kbps of output video (default: $VIDEO_BITRATE)" exit 1 } abr=$AUDIO_BITRATE vbr=$VIDEO_BITRATE while getopts ":a:b:" options; do case $options in a) abr=`echo "$OPTARG" | grep "^[0-9]\+k\?$"` if [ -z "$abr" ]; then usage; fi;; b) vbr=`echo "$OPTARG" | grep "^[0-9]\+k\?$"` if [ -z "$vbr" ]; then usage; fi;; *) usage;; esac done shift $(($OPTIND - 1)) if [ -z "$*" ]; then usage fi for infile in $*; do if [ ! -f "$infile" ]; then echo "$inf: No such file">&2 fi # adjust height to aspect ratio; make sure it's a multiple of 2 re="^.*Video:.\+[^0-9]\([0-9]\+\)x\([0-9]\+\)[^0-9].*$" let "pad = ($HEIGHT - ($WIDTH * `ffmpeg -i \"$infile\" 2>&1 | grep $re | sed \"s/$re/\2\/\1/\"`)) / 4 * 2" let "height = $HEIGHT - 2 * $pad" # replace certain file extensions with .mp4; just add .mp4 to the end if it doesn't match those extensions outfile=`echo "$infile.mp4" | sed "s/\.\(avi\|mpg\|mpeg\|asf\).mp4$/.mp4/i"` # run ffmpeg to transcode bash -c "ffmpeg $BASE_ARGS -i \"$infile\" -s ${WIDTH}x${height} -padtop $pad -padbottom $pad -ab $abr -b $vbr \"$outfile\"" done exit 0