#!/bin/bash

types ()
{
    echo
    cat "$0" | grep  '()\s*{'
}

[ -z "$3" ] && echo "usage $0 [type] <in> <out>" && types && exit 1



# NTSC half size 4/3 aspect
# http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-vcd-dvd.html
# http://gentoo-wiki.com/HOWTO_mencoder_divx_to_dvd

DVD_OPTS="
	-ovc lavc -oac lavc \
        -ofps 30000/1001 \
        -of mpeg -mpegopts format=dvd:tsaf \
        -srate 48000 \
        -lavcopts \
vcodec=mpeg2video:\
vbitrate=4800:\
vrc_maxrate=9800:\
vrc_buf_size=1835:\
keyint=18:\
aspect=4/3:\
acodec=ac3:\
abitrate=192
"

DVD_OPTS_16_9="
	-ovc lavc -oac lavc \
        -ofps 30000/1001 \
        -of mpeg -mpegopts format=dvd:tsaf \
        -srate 48000 \
        -lavcopts \
vcodec=mpeg2video:\
vbitrate=4800:\
vrc_maxrate=9800:\
vrc_buf_size=1835:\
keyint=18:\
aspect=16/9:\
acodec=ac3:\
abitrate=192
"


dvd_ntsc_half () {
    mencoder \
	"$1" -vf scale=352:240,harddup $DVD_OPTS -o "$2"
}

dvd_ntsc () {
    mencoder \
	"$1" -vf scale=720:480,harddup $DVD_OPTS -o "$2"
}

# last tested with:
# mencoder 2:1.0~rc4.dfsg
dvd_ntsc_16x9 () {
    mencoder \
	"$1" -vf scale=720:400,harddup $DVD_OPTS_16_9 -o "$2"
}



cinelerra () {
    mencoder \
        "$1" -oac lavc -ovc lavc \
        -lavcopts vcodec=mjpeg \
        -o "$2"
}

# two input streams
va_dvd_ntsc() {
    mencoder \
        "$1" -vf scale=720:480,harddup $DVD_OPTS "$2" -o "$3"
}

# copy main title and en language from dvd into single .vob file this
# is the only way i was able to remove region encoding from a
# dvd. supposedly it's also possible with some .IFO editing

# what also works is to concatenate all .VOBS and feed that to vobs2iso

dvd_copy () {
    mencoder -dvd-device "$1" dvd://1 -alang en \
        -oac copy -ovc copy \
        -of mpeg -mpegopts format=dvd:tsaf \
        -o "$2"
}


# for archiving: convert from raw landscape to rotated to high bitrate
# divx : mp3. this gives about 1000 kbps, of which audio is 88 kbps

# the survivin & trivin bulk is about 3G at
# VIDEO:  [MJPG]  640x480  24bpp  30.000 fps  13716.1 kbps (1674.3 kbyte/s)
# AUDIO: 11024 Hz, 1 ch, u8, 88.2 kbit/100.00%

# i'd like to fit this on a CD, so a rate of about 3000kbps should be ok
mpeg4_rot () {
	mencoder \
		"$1" \
		-vf rotate=1,eq=50:100,denoise3d=10:5:10 \
		-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=3000 \
		-oac copy \
		-o "$2"
}

# this is 320x240, so i reduce the bitrate a bit
mpeg4_denoise () {
	mencoder \
		"$1" \
		-vf eq=50:100,denoise3d=10:5:10 \
		-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1000 \
		-oac copy \
		-o "$2"
}


# This is 320x240, so i reduce the bitrate a bit.  It just recodes the
# video, leaves audio intact.
mpeg4 () {
	mencoder \
		"$1" \
		-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1000 \
		-oac copy \
		-o "$2"
}

# Convert camcorder material (16x9) to this.  I can't get it to work
# in one go.  Conversion to VOB first, then to AVI.
camcorder () {
    dvd_ntsc_16x9 $1 $1.vob
    mpeg4 $1.vob $2
    rm $1.vob
}



# windows media player compatible using wmv2. life sucks.
wmv2_rot () {
	mencoder \
		"$1" \
		-vf rotate=1,eq=50:100,denoise3d=10:5:10 \
		-ovc lavc -lavcopts vcodec=wmv2:vbitrate=3000 \
	        -oac copy \
		-o "$2"
}

#
wmv2 () {
	mencoder \
		"$1" \
		-vf eq=50:100,denoise3d=10:5:10 \
		-ovc lavc -lavcopts vcodec=wmv2:vbitrate=3000 \
	        -oac copy \
		-o "$2"
}



# upside down, 320x240
wmv2_rot_half () {
	mencoder \
		"$1" \
		-vf rotate=2,eq=50:100,denoise3d=10:5:10 \
		-ovc lavc -lavcopts vcodec=wmv2:vbitrate=750 \
		-oac copy \
		-o "$2"
}


# For feeding to WDTV
wdtv () {
	mencoder \
		"$1" \
		 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=4000 \
                 -oac copy \
		-o "$2"
}


"$@"
