Newer
Older
Import / code / Scripts / transcode.sh
#!/bin/bash


VIDEO="$1"
TARGET="$2"
BITRATE="1000"
TWO_PASS=0
CROP=
FPS=


# Crop detection
function crop_detection
{
  if [ -f "$1" ]
  then
    V_SIZE=`ls -s --block-size=1 "$1" | cut -d ' ' -f 1`
    if [ $V_SIZE -gt "500000000" ]	# Video over 500 MB we take more care with
    then
	TWO_PASS=1
	CROP=`mplayer "$1" -vf cropdetect -sb 10000000 -sstep 60 -vo null -ao null 2>&1 < /dev/null | grep "[CROP]" | tail -n 1`
        # output:
   	#    [CROP] Crop area: X: 0..719  Y: 56..422  (-vf crop=720:352:0:64).0
    	CROP=`echo $CROP | cut -d '(' -f 2 | cut -d ')' -f 1 | cut -d ' ' -f 2`
    	echo "Detected crop of \"$CROP\""
	if [ `echo $CROP | grep -c "^crop="` = 1 ]
	then
	    CROP=",$CROP"
	else
	    CROP=
	fi
    fi
  else
    echo "transcode.sh error: \"$1\" does not exist"
    exit
  fi
}


# Transcode
function transcode
{
  echo "Transcoding \"$VIDEO\" to \"$TARGET\""
  if [ "$TWO_PASS" == "1" ]
  then
    mencoder -really-quiet "$VIDEO" -vf pullup,softskip${CROP},harddup -nosound -ovc x264 \
	-x264encopts bitrate=${BITRATE}:qcomp=0.7:subq=1:frameref=1:bframes=3:b_pyramid:weight_b:turbo=1:threads=auto:pass=1 \
	-of lavf -lavfopts format=mp4 -o /dev/null < /dev/null 2> /dev/null > /dev/null
    echo "Finished pass one"
    mencoder -really-quiet "$VIDEO" -vf pullup,softskip${CROP},harddup -nosound -ovc x264 \
	-x264encopts bitrate=${BITRATE}:qcomp=0.7:subq=5:8x8dct:frameref=3:bframes=3:b_pyramid:weight_b:threads=auto:pass=2 \
	-of lavf -lavfopts format=mp4 -o "$TARGET" < /dev/null 2> /dev/null > /dev/null
  else
    # mencoder -really-quiet "$VIDEO" -vf kerndeint,scale -nosound -ovc x264 \
    mencoder -really-quiet "$VIDEO" -vf pullup,softskip,harddup -nosound -ovc x264 \
	-x264encopts bitrate=${BITRATE}:qcomp=0.7:subq=5:8x8dct:frameref=3:bframes=3:b_pyramid:weight_b:threads=auto \
	-of rawvideo -o "$TARGET" < /dev/null 2> /dev/null > /dev/null
  fi
  if [ -f "$TARGET" ]
  then
    rm -rf "$VIDEO"
  fi
  echo "Finished transcoding \"$VIDEO\" to \"$TARGET\""
}


function input_fps
{
  FPS=`mplayer "$VIDEO" -vo null -ao null -frames 1 < /dev/null 2>&1 | grep "VIDEO:" | cut -d ' ' -f 10`
  echo "Input video FPS is -$FPS-"
}

input_fps
crop_detection
time transcode