It's been a while since I posted my first encoding video for the Zaurus script, so here's an updated Version 2 of it. Enjoy.
#!/bin/sh
# Declare some variables
INFILE=$1
OUTFILE=$2
WIDTH=640
HEIGHT=480
# Check for input video.
if [ -z "$1" ]
then echo "#########################"
echo "# ERROR! NO INPUT FILE. #"
echo "#########################"
echo
echo "Usage"
echo --------------------
echo "$0 INPUT OUTPUT width_option height_option"
echo
echo "Example"
echo --------------------
echo "$0 Funny_Clip.avi Funny_Clip_Resized.avi 320 240"
exit
fi
# Check for output file name.
if [ -z "$2" ]
then echo "##########################"
echo "# ERROR! NO OUTPUT FILE. #"
echo "##########################"
echo
echo "Usage"
echo --------------------
echo "$0 INPUT OUTPUT width height"
echo
echo "Example"
echo --------------------
echo "$0 Funny_Clip.avi Funny_Clip_Resized.avi 320 240"
exit
fi
# Change to custom height and width
if [ -n "$3" ]
then WIDTH=$3
fi
if [ -n "$4" ]
then HEIGHT=$4
fi
# Print some information
echo Video Encoding for Sharp Zaurus Playback
echo By Vrejakti
echo http://www.lan358.com
echo
echo
echo "Default 640x480"
echo "Full screen [4:3] use 640x480 [default]."
echo "Wide screen [16:9] use 640x360."
echo --------------------
echo " Input: $1"
echo " Output: $2"
echo " Width: $WIDTH"
echo " Height: $HEIGHT"
echo --------------------
# Check responce
echo "Is this correct? [Yes/No] "
read answer
# VERY USEFUL CODE: This accepts "Yes", "yes", "Y", and "y".
if [ $answer == "Yes" ] || [ $answer == "yes" ] || [ $answer == "Y" ] || [ $answer == "y" ]
then
# Start mencoder magic
mencoder "$INFILE" -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=300:vpass=1 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac mp3lame -o $OUTFILE
mencoder "$INFILE" -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=300:vpass=2 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac mp3lame -o $OUTFILE
echo "Converted file is $OUTFILE"
else echo "Encoding canceled!!!"
fi
Please post any suggestions for improvements. I'm still learning how to write bash scripts, so there's bound to be room for improvement. Version 3 will allow for encoding multiple files.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Edit notes: *shudder* The above is version 2, that is more of a proof of concept. I'm posting version 4.0 below for a side by side comparison. Many new features have been added in version 4.0 as well as a great number of simplifications.
i) Script is easier to run. Install it as vrecoder to /usr/local/bin then run `vrecoder -i AVI_File'
ii) All options have been added as switches. See `vrecoder -h' for the details.
iii) The `-a' switch attempts to resize your video keeping the correct aspect ratio.
iv) `-s' uses an experimental smartcoder that will encode every AVI file in the current directory.
#!/bin/bash
# Encoding Video for Zaurus Playback
# Script version 4.5
# Attributions:
# Vrejakti LAN358 Inc, http://lan358.com: Head Developer
# XorA Open Zaurus Project: Produced original mpencoder code
# Da_Blitz OESF Forums: additional mencoder code, and shell code
# Xamindar OESF Forums: feature suggestions
MAX_WIDTH="640" # Default max width
WIDTH="640" # Default width
HEIGHT="480" # Default height
encoder="vrecoder" # Default encoder
BITRATE="600" # Default quality / bit rate
CODEC="mpeg4" # Default codec
help () {
cat << EOF
vrecoder: Video Encoding for Sharp Zaurus Playback
Usage: vrecoder [-h] [-a] [-s] [-i infile] [-o outfile]
[-x size] [-y size] [-b bitrate]
-h, Display this help menu
-a, Auto calculate resized video resolution
-s, Use smartcoder
-i, Input File
-o, Output File
-x, Change width
-b, Change bitrate. 300 default, 6000 would be smooth
-y, Change height
--quick No confirms, jump to encoding.
:: Encoders ::
vrecoder: Default encoder. Works on a signle input file.
smartcoder: Experimental encoder. Atempts to encode every AVI file in the
current directory.
:: Notes ::
[-a] depends on awk >= 3.1.5, file >= 4.3.0, grep >= 2.5.1.
Use at your own risk.
[-s] depends on file >= 4.18, find >= 4.3.0.
Use at your own risk.
EOF
exit
}
if [ -z "$1" -o "$1" = "-h" ]
then
help
fi
for ARG in $@; do
case $ARG in
-x) WIDTH="$2"; shift;;
-y) HEIGHT="$2"; shift;;
-i) IN="$2"; shift;;
-o) OUT="$2"; shift;;
-a) autores="yes"; shift;;
-b) BITRATE="$2"; shift;;
-s) encoder="smartcoder"; shift;;
--quick) rush="x"; shift;;
*) shift;;
esac
done
if [ "$encoder" == "vrecoder" ]
then
if [ -z "$IN" ]
then
printf "Whoops! No input found.
Did you forget to input a file with -i file.avi?\n"
exit
fi
fi
if [ -z "$OUT" ]
then
OUT="Zaurus-"$IN""
# /*
# * Why is this switch true when -i is a complex file name?
# */
fi
if [[ "$encoder" == "smartcoder" ]]
then
rush="x"
fi
if [[ "$rush" == "x" ]]
then
answer="yes"
else
#####calculate res#############################
case "$autores" in
"Yes" | "yes" | "Y" | "y" )
# /*
# * Anyone have a suggestion
# * how to better impliment this function?
# */
printf "Enter screen width (ie. 640): "
read MAX_WIDTH
height=`file $IN | awk -F, '{print $3}' | awk -F" " '{print $1}'`
width=`file $IN | awk -F, '{print $3}' | awk -F" " '{print $3}'`
first="$[$width*100/$height]" # fake float
second="$[$MAX_WIDTH*$first/100]" # restore int
WIDTH="$MAX_WIDTH"
HEIGHT="$second"
;;
esac
###############################################
echo
echo "Please confirm:"
echo "------------------------------"
echo " Input: $IN"
echo " Output: $OUT"
echo " Width: $WIDTH"
echo " Height: $HEIGHT"
echo " Encoder: $encoder"
echo " Bitrate: $BITRATE"
echo "------------------------------"
printf "Start encoding?
[Yes/No] "
read answer
fi
####functions##################################
function vrecoder ()
{
mencoder "$IN" -ovc lavc -lavcopts vcodec=$CODEC:\
vbitrate=$BITRATE:vpass=1 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
#--------------------
mencoder "$IN" -ovc lavc -lavcopts vcodec=$CODEC:\
vbitrate=$BITRATE:vpass=2 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
}
function smartcoder ()
{
i=1
while read "F"
do
T=$(file -zibkp "$F")
if [[ $T == 'video/x-msvideo' ]]
then
OUT="Zaurus-"$F""
# mencoder "$F" -ovc lavc -lavcopts vcodec=$CODEC:vbitrate=\
# $BITRATE:vpass=1 -vf scale=$WIDTH:$HEIGHT -oac copy -o "Output_"$F""
# mencoder "$F" -ovc lavc -lavcopts vcodec=$CODEC:vbitrate=\
# $BITRATE:vpass=2 -vf scale=$WIDTH:$HEIGHT -oac copy -o "Output_"$F""
mencoder "$F" -ovc lavc -lavcopts vcodec=$CODEC:\
vbitrate=$BITRATE:vpass=1 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
#--------------------
mencoder "$F" -ovc lavc -lavcopts vcodec=$CODEC:\
vbitrate=$BITRATE:vpass=2 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
# /*
# * Can someone please tell me the difference between the commented lines
# * and the uncommented lines?
# */
i=$[$i+1]
fi
done < <(find -maxdepth 1 -printf "%f\n")
}
####/functions#################################
####main#######################################
case "$answer" in
"Yes" | "yes" | "Y" | "y" )
$encoder
printf "\nEncoding completed at `date`\n"
exit;;
"op" )
printf "Minutes: [hh:mm:ss] "
read minutes
mencoder "$IN" -ovc lavc -lavcopts vcodec=$CODEC:vbitrate=\
$BITRATE -vf scale=$WIDTH:$HEIGHT -oac copy -endpos \
$minutes -o "$OUT"
exit;;
esac
###############################################
printf "\nEncoding has been cancled.\n"
Todo:
Fix script so everything will work on complex file names as well as normal file names.
Have script work on each file on the command line, not just the one file included with the `-i' switch.
Replace experimental code with real portable code.
Rewrite the entire script in x86 ASM using mencoder shared libraries. (Yeah right. ;-) )