#!/bin/sh

function usage
{
    # Title
    echo "Title: MPEG Export"

    # Usable?
    which transcode > /dev/null
    [ $? = 0 ] && echo Status: Active || echo Status: Inactive

    # Type
    echo Flags: single-pass file-producer

    # Profiles
    echo "Profile: ffmpeg 176x144 mpeg1"
    echo "Profile: transcode 176x144 mpeg1"
    echo "Profile: transcode VCD 2.0 (352x288 mpeg1)"
    echo "Profile: transcode SVCD (480x576 mpeg2)"
    echo "Profile: transcode SVCD (480x576 mpeg2) denoise"
    echo "Profile: transcode DVD (720x576 mpeg2)"
}

function execute
{
    # Arguments
    normalisation="$1"
    length="$2"
    profile="$3"
    file="$4"

    # Determine info arguments
    target=mpeg1
    video_bitrate=""
    audio_bitrate=224
    deinterlace=""
    denoise=""

    case "$profile" in
        "0" )
            tool=ffmpeg
            target=mpeg1
            size=$([ "$normalisation" = "pal" ] && echo 176x144 || echo 176x120)
            video_bitrate=384
            audio_bitrate=192
            ;;
        "1" )
            tool=transcode
            target=mpeg1
            size=$([ "$normalisation" = "pal" ] && echo 176x144 || echo 176x120)
            video_bitrate=384
            audio_bitrate=192
            deinterlace=smartyuv
            ;;
        "2" )
            tool=transcode
            target=vcd
            size=$([ "$normalisation" = "pal" ] && echo 352x288 || echo 352x240)
            audio_bitrate=224
            deinterlace=smartyuv
            ;;
        "3" )
            tool=transcode
            target=svcd
            size=$([ "$normalisation" = "pal" ] && echo 480x576 || echo 480x480)
            deinterlace=smartyuv
            ;;
        "4" )
            tool=transcode
            target=svcd
            size=$([ "$normalisation" = "pal" ] && echo 480x576 || echo 480x480)
            deinterlace=smartyuv
            denoise="yuvdenoise=scene_thres=90%:do_reset=1"
            ;;
    esac

    audio_bitrate=$([ "$target" == "svcd" ] && echo "192")
    format=$([ "$target" == "svcd" ] && echo "mpeg2" || echo "mpeg1")

    # Run the command
    case "$tool" in
        "transcode" )
            mkfifo "$file".{video,audio}
            opt_input="-i \"$file\".video -p \"$file\".audio"
            opt_decode="-x dv,dv -H 0 -V --dv_yuy2_mode"
            opt_deinterlace=$([ "$deinterlace" != "" ] && echo "-J $deinterlace")
            opt_resize="-Z $size"
            opt_denoise=$([ "$denoise" != "" ] && echo "-J $denoise")
            opt_video_format=$([ "$format" == "mpeg1" ] && echo "-F 0,\"-a 2\"" ||
                              ([ "$format" == "vcd" ]   && echo "-F 1,\"-a 2\"" ||
                              ([ "$target" == "svcd" ]  && echo "-F 4,\"-a 2 -I 0\"" ||
                                                           echo "-F 4,\"-a 2 -I 0 -d\"" ) ) )
            opt_video_bitrate=$([ "$video_bitrate" != "" ] && echo "-w $video_bitrate")
            opt_audio_bitrate=$([ "$audio_bitrate" != "" ] && echo "-b $audio_bitrate")
            opt_encode="-y mpeg2enc,mp2enc $opt_video_format $opt_video_bitrate -E 44100 $opt_audio_bitrate"
            opt_output="-o \"$file\""
            options="$(echo $opt_input $opt_decode $opt_deinterlace $opt_resize $opt_denoise $opt_encode $opt_output)"
            echo >&2
            echo >&2 "transcode $options"
            echo $options | xargs transcode 1>&2 &
            tee "$file".video > "$file".audio
            wait
            rm "$file".{video,audio}

            video_fileext=$([ "$format" == "mpeg1" ] && echo "m1v" || echo "m2v")
            opt_input="\"$file\".$video_fileext \"$file\".mpa"
            opt_video_format=$([ "$format" == "mpeg1" ] && echo "-f 0" ||
                              ([ "$format" == "vcd" ]   && echo "-f 1" ||
                              ([ "$target" == "svcd" ]  && echo "-f 4" ||
                                                           echo "-f 5" ) ) )
            opt_video_splitsize=$([ "$target" == "svcd" ] && echo "-S 680");
            opt_video_bitrate=$([ "$video_bitrate" != "" ] && echo "-r $(( (video_bitrate + audio_bitrate) * 102 / 100))")
            opt_output="-o \"$file\".mpg"
            options="$opt_video_format $opt_video_bitrate $opt_video_splitsize $opt_output $opt_input"
            echo >&2
            echo >&2 "mplex $options"
            echo $options | xargs mplex 1>&2
            rm "$file".{$video_fileext,mpa}
            ;;
        "ffmpeg" )
            ffmpeg -f dv -i - -deinterlace \
                -r $normalisation -s $size -b $video_bitrate \
                -acodec mp2 -ab $audio_bitrate \
                -y "$file".mpeg
            ;;
    esac
}

[ "$1" = "--usage" -o "$1" = "" ] && usage $@ || execute $@

