#!/bin/bash
set -e

NICE=19
#res="480x272"
#res="320x240"
in=""
res="368x208"
title=""
out="" 
method=mencoder
subs=""
start_time=""
stop_time=""
quality_alias=low
vbitrate=768
abitrate=128
ofps="24000/1001"

show_help()
{
    echo "video2psp.sh [-m mencoder|ffmpeg] [-r resolution] [-t title] [-s subs] [-ss start -endpos stop] [-q low|medium|high] [-ab bitrate] [-vb bitrate] [-ofps output_fps]input [output]"
}

while [ x"$1" != x ]
do
    case "$1" in
        -h)
            show_help
            exit 1
        ;;

        -r)
            shift
            res="$1"
            shift
            continue
        ;;

        -t)
            shift
            title="$1"
            shift
            continue
        ;;

        -s)
            shift
            subs="$1"
            shift
            continue
        ;;

        -m)
            shift
            method="$1"
            
            if [[ $method != "mencoder" && $method != "ffmpeg" ]]
            then
                echo Metodo incorrecto
                show_help
                exit 2
            fi

            shift
            continue
        ;;

        -q)
            shift
            quality_alias="$1"
            shift
            continue
        ;;

        -ss)
            shift
            start_time="$1"
            shift
            continue
        ;;

        -endpos)
            shift
            stop_time="$1"
            shift
            continue
        ;;

        -vb)
            shift
            vbitrate="$1"
            shift
            continue
        ;;

        -ab)
            shift
            abitrate="$1"
            shift
            continue
        ;;

        -ofps)
            shift
            ofps="$1"
            shift
            continue
        ;;

        *)
            in="$1"
            shift
            out="$1"
            if [ x"$out" == x ]
            then
                out="$in.MP4"
            fi
            break
        ;;
    esac
done

if [ x"$in" == x ]
then
    echo Error: Indica un fichero para convertir.
    show_help
    exit 1
fi

if [ x"$title" == x ]
then
    title=`basename "$in"`
fi

thmbase=`dirname "$out"`/`basename "$out" .MP4`

if [ "$method" == "mencoder" ]
then
    res=`echo $res | tr x :`

    if [ "$subs" != "" ]; then
            subs="-sub $subs"
    fi

    if [ "$start_time" != "" ]; then
        if [ "$stop_time" == "" ]; then
            stop_time=60
        fi
        time_interval="-ss $start_time -endpos $stop_time"
    fi

    if [ "$quality_alias" == "high" ]; then
        quality="subq=6:partitions=all:8x8dct:me=umh:frameref=5:bframes=3:b_pyramid:weight_b"
    else if [ "$quality_alias" == "medium" ]; then
        quality="subq=5:8x8dct:frameref=2:bframes=3:b_pyramid:weight_b"
    else if [ "$quality_alias" == "low" ]; then
        quality="subq=4:bframes=2:b_pyramid:weight_b"
    fi
    fi
    fi

    echo mencoder -ac mad -aid 1 -quiet -oac lavc -ovc lavc -of lavf -lavcopts aglobal=1:vglobal=1:vcodec=h264:acodec=aac:abitrate=$abitrate:vbitrate=$vbitrate:coder=1 -af lavcresample=48000 -srate 48000 -vf scale=$res,harddup -lavfopts format=psp:i_certify_that_my_video_stream_does_not_use_b_frames -ofps 30000/1001 -x264encopts bitrate=$vbitrate:level_idc=21:$quality -info name="$title" $time_interval $subs -o "$out" "$in"
    #nice -n $NICE mencoder -ac mad -aid 1 -quiet -oac lavc -ovc lavc -of lavf -lavcopts aglobal=1:vglobal=1:vcodec=h264:acodec=aac:abitrate=$abitrate:vbitrate=$vbitrate:coder=1 -af lavcresample=48000 -srate 48000 -vf scale=$res,harddup -lavfopts format=psp:i_certify_that_my_video_stream_does_not_use_b_frames -ofps 30000/1001 -x264encopts bitrate=$vbitrate:level_idc=21:$quality -info name="$title" $time_interval $subs -o "$out" "$in"
    nice -n $NICE mencoder -ac mad -aid 1 -quiet -oac lavc -ovc lavc -of lavf -lavcopts aglobal=1:vglobal=1:vcodec=libx264:acodec=libfaac:abitrate=$abitrate:vbitrate=$vbitrate:coder=1 -af lavcresample=48000 -srate 48000 -vf scale=$res,harddup -lavfopts format=psp -ofps $ofps -x264encopts bitrate=$vbitrate:level_idc=21:$quality -faacopts br=$abitrate -info name="$title" $time_interval $subs -o "$out" "$in"

else if [ "$method" == "ffmpeg" ]
then
    res=`echo $res | tr : x`
    if [ "$start_time" != "" ]; then
        if [ "$stop_time" == "" ]; then
            stop_time=60
        fi
        time_interval="-ss $start_time -t $stop_time"
    fi

    echo /usr/local/share/pspvc/bin/ffmpeg -y -i "$in"  -acodec aac -ab ${abitrate}kb -vol 256 -vcodec h264 -b ${vbitrate}kb -ar 48000 -mbd 2 -coder 1 -cmp 2 -subcmp 2 -s $res -r 30000/1001 -title "$title"  -f psp -flags loop -trellis 2 -partitions parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 "$out"
    nice -n $NICE ffmpeg -y -i "$in"  -acodec libfaac -ab ${abitrate}kb -vol 256 -vcodec libx264 -b ${vbitrate}kb -ar 48000 -mbd 2 -coder 1 -cmp 2 -subcmp 2 -s $res -r $ofps -title "$title"  -f psp -flags loop -trellis 2 -partitions parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 "$out"
else
    echo Error: metodo desconocido.
    show_help
    exit 2
fi
fi

mplayer "$in" -ss 00:10:00 -frames 2 -vo jpeg
convert 00000002.jpg -geometry x120 "$thmbase".jpg
rm -f 0000000[12].jpg
mv -v "$thmbase".jpg "$thmbase".THM


