#!/bin/sh

# picSeq2Video.sh is an encoding script :
# it converts a sequence of pictures into a video, using mencoder

# Copyright (C) 2006 Aymeric AUGUSTIN

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# 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.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# default parameters
BITRATE=0
CODEC="mpeg4"
CUSTOM=0
EXTENSION="png"
FPS=25
NAME="video.avi"
PLAY=0
PREFIX=""
REMOVE=0
USAGE="Usage: $0 [-b bitrate] [-c codec] [-C custom [-D custom2]] [-e extension] [-f fps] [-h] [-m mode] [-o file] [-p prefix] [-P] [-rR] [-s width:height]"

while getopts b:c:C:D:e:f:hm:o:p:PrRs: OPTION ; do
    case $OPTION in
        b) BITRATE=$OPTARG;;
        c) CODEC=$OPTARG;;
        C) let "CUSTOM++"; CUSTOM1=$OPTARG;;
        D) let "CUSTOM++"; CUSTOM2=$OPTARG;;
        f) FPS=$OPTARG;;
        m) case $OPTARG in
            web) CODEC="mpeg4"; BITRATE="1200"; SIZE="-vf scale -zoom -xy 320 -sws 9";;
            hq) CODEC="x264";;
            dv) CODEC="dv"; SIZE="-vf scale=720:576 -sws 9";;
        esac;;
        o) NAME=$OPTARG;;
        p) PREFIX=$OPTARG;;
        P) PLAY=1;;
        r) REMOVE=1;;
        R) echo "/!\ /!\   Warning : using -R can result in data loss !  /!\ /!\ "
        echo "Press Ctrl-C if you have not read and understood the documentation !"
        REMOVE=2;;
        s) SIZE="-vf scale=$OPTARG -sws 9";;
        h) echo "picSeq2Video.sh : convert a sequence of pictures into a video, using mencoder"
        echo ""
        echo "$USAGE" ;
        echo ""
        echo "Tip : you should either :"
        echo "    - choose a predefined mode,"
        echo "    - choose a codec and a bitrate,"
        echo "    - or use custom parameters if you master 'man mencoder' ;p"
        echo ""
        echo "Tip 2 : options are read sequentially, so you may override some settings"
        echo "of a given mode by specifying them *after* the mode"
        echo ""
        echo "General options:"
        echo "    -f <fps>        set source framerate to <fps>"
        echo "    -e <extension>  use only pictures with extension <extension>"
        echo "                      default is 'png'"
        echo "    -o <filename>   save the video as <filename>"
        echo "                      default is 'video.avi'"
        echo "    -p <prefix>     use only pictures whose name start with <prefix>"
        echo "    -P              play the video after encoding (using mplayer)"
        echo "    -r              remove picture files after building the video"
        echo "                      this option asks for confirmation"
        echo "    -R              remove picture files after building the video"
        echo "                      /!\ WITHOUT asking for confirmation /!\ "
        echo "                      DO NOT USE THIS UNLESS YOU KNOW WHAT IT DOES !"
        echo "                      (use -r in order to test !)"
        echo ""
        echo "Encoding options:"
        echo "    -m <mode>       use specified preset"
        echo "                      you can specify other options to override predefined settings"
        echo "                      currently available : web, hq, dv"
        echo "                      all modes may not be supported by your version of mencoder"
        echo "    -b <bitrate>    set bitrate to <bitrate>"
        echo "    -c <codec>      use specified codec"
        echo "                      currently available : mpeg4, xvid, x264, mjpeg, dv"
        echo "                      all codecs may not be supported by your version of mencoder"
        echo "    -s <w>:<h>      resize video to width <w> and height <h>"
        echo "    -C <custom>     use custom codec parameters"
        echo "                      <custom> will be passed as -ovc parameter"
        echo "                      when calling mencoder"
        echo "                      overrides all other encoding settings"
        echo "                      see source for details ;)"
        echo "    -D <custom2>    use custom codec parameters for second pass"
        echo "                      requires -C to define first pass parameters"
        exit 0;;
        \?) echo "$USAGE" ;
        exit 1;;
    esac
done


# do the encoding !

echo $CUSTOM
# custom encoding
if [ $CUSTOM -gt 0 ]; then
    if [ $CUSTOM -eq 1 ]&&[ -n "$CUSTOM1" ]; then
        mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc $CUSTOM1 -nosound -o $NAME || exit -1
    elif [ $CUSTOM -eq 2 ]&&[ -n "$CUSTOM1" ]&&[ -n "$CUSTOM2" ]; then
        mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc $CUSTOM1 -nosound -o /dev/null || exit -1
        mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc $CUSTOM2 -nosound  -o $NAME || exit -1
    else
        echo "Custom encoding failed. Check -C (and -D) options !"
        exit 1
    fi

# mpeg4
elif [ $CODEC == "mpeg4" ]; then
    if [ $BITRATE -gt 0 ]; then
        BITRATE=":vbitrate=$BITRATE"
    else
	BITRATE=""
    fi
    mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc lavc -lavcopts vcodec=mpeg4$BITRATE:vpass=1:turbo -nosound -o /dev/null || exit -1
    mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc lavc -lavcopts vcodec=mpeg4$BITRATE:vpass=2:qpel:trell -nosound -o $NAME || exit -1
    rm -f divx2pass.log

# xvid
elif [ $CODEC == "xvid" ]; then
    if [ $BITRATE -gt 0 ]; then
        BITRATE=":bitrate=$BITRATE"
    else
	BITRATE=""
    fi
    mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc xvid -xvidencopts turbo:vhq=0:pass=1 -nosound -o /dev/null || exit -1
    mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc xvid -xvidencopts chroma_opt:vhq=4:bvhq=1:quant_type=mpeg:pass=2$BITRATE -nosound -o $NAME || exit -1
    rm -f xvid-twopass.stats

# x264
elif [ $CODEC == "x264" ]; then
    if [ $BITRATE -gt 0 ]; then
        BITRATE=":bitrate=$BITRATE"
    else
	BITRATE=""
    fi
    mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc x264 -x264encopts subq=1:frameref=1:pass=1 -nosound -o /dev/null || exit -1
    mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc x264 -x264encopts subq=6:4x4mv:8x8dct:me=3:frameref=5:bframes=3:b_pyramid:weight_b:pass=2$BITRATE -nosound -o $NAME || exit -1
    rm -f __xvid-twopass.stats

elif [ $CODEC == "mjpeg" ]; then
    if [ $BITRATE -gt 0 ]; then
        BITRATE=":vbitrate=$BITRATE"
    else
	BITRATE=""
    fi
    mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc lavc -lavcopts vcodec=mjpeg$BITRATE -nosound -o $NAME || exit -1

elif [ $CODEC == "dv" ]; then
    mencoder "mf://$PREFIX*.$EXTENSION" -mf fps=$FPS $SIZE -ovc libdv -oac pcm input.avi -o $NAME 
else
    echo "Script error :'("
    echo "Check options and authorized codecs"
fi

# remove pictures
if [ $REMOVE -eq 1 ]; then
    echo ""
    echo "Do you really want to do : 'rm $PREFIX*.$EXTENSION' ?" 
    CHOIX=""
    while [ "$CHOIX" != "yes" ]&&[ "$CHOIX" != "no" ]; do
    echo "(yes/no) ?"
    read CHOIX
        if [ "$CHOIX" == "yes" ]; then
            rm $PREFIX*.$EXTENSION
        fi
    done
elif [ $REMOVE -eq 2 ]; then
    echo ""
    echo "Doing : 'rm $PREFIX*.$EXTENSION'" 
    rm -rf $PREFIX*.$EXTENSION
fi

# play video
if [ $PLAY -eq 1 ]; then
    mplayer $NAME || exit -1
fi
