Language: EN FR PT ES NL DE SV RU
#!/bin/bash
 
#-----------------------------------------------------------------------------
#    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 3 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, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------------
 
#-----------------------------------------------------------------------------
# slackin's xvid encode script, with renice!       Version: 0.1
# Justin Clark -- slackin35@yahoo.com
# slackin35 on AIM
#
# See the bottom of the script for more information.
#-----------------------------------------------------------------------------
 
### PLEASE EDIT! #############################################################
# Please put all your xvid options in this line.
# This script is for xvid two pass encoding
#
 
xvid_opts="pass=2:me_quality=6:vhq=4:chroma_opt:trellis:bvhq=1:hq_ac:qpel:gmc:cartoon:bitrate=-150100"
 
#
# That concludes the inscript config!
#
### END EDIT! ################################################################
 
 
# Color echo function, for using echo with colors in bash, syntax: color_echo "color_name" "message to send"
color_echo() {
 
	# Case statement to set ascii color code from named color
	case "$1" in
		"blue" )
			color="\e[32m"
			;;
		"red" )
			color="\e[31m"
			;;
		"yellow" )
			color="\e[33m"
			;;
		"green" )
			color="\e[34m"
			;;
	esac
 
	# Do the actual echo, note the -e to use the escape codes
	# and notice the color reset at the end, always use that.
	echo -e "${color}${2}\e[0m"
}
 
# Check to see if mencoder is still running, if running retrieve last update from log
check_pid() {
    if [ $(pidof mencoder) ] 
    then
	tail -n 1 output.log
	sleep 2
	check_pid
	return
    else
        sleep 2
        if [ $(pidof mencoder) ] 
        then
            tail -n 1 output.log
            sleep 2
            check_pid
            return
        fi
	return
    fi
}
 
# renice the process so it doesn't scale up cpu frequency scaling.
renice_pid() {
    PID=$(pidof mencoder)
    color_echo "yellow" "\nRenicing mencoder with PID: ${PID}"
    renice 1 -p $PID
}
 
# Start the first pass of the encode, copy the audio and dump the output to /dev/null
first_pass() {
    color_echo "green" "\nStarting first pass."
    mencoder "${INPUT_FILES}" -vf spp=6:3,hqdn3d -vf scale -zoom -xy 624 -nosound -ovc xvid -xvidencopts pass=1:turbo -o /dev/null 1> output.log 2> first_pass_error.log &
}
 
# Start the second pass of encode, re-encoding both with options declared earlier
second_pass() {
    color_echo "green" "\nStarting second pass."
    mencoder "${INPUT_FILES}" -vf spp=6:3,hqdn3d -vf scale -zoom -xy 624 -mc 0 -oac mp3lame -lameopts aq=0:cbr:preset=128 -ovc xvid -xvidencopts ${xvid_opts} -o "${OUTPUT_FILES}" 1> output.log 2> second_pass_error.log &
}
 
# Process the file name given at run time, first grab with the * then without
#INPUT_FILES=`echo $1 | sed -e '{s/.\(.*\)/\1/}'`
#NAME=`echo $1 | sed -e '{s/\(.*\)./\1/}'`
#OUTPUT_FILES=`echo $NAME | sed -e '{s/.\(.*\)/\1/}'`
INPUT_FILES="${1}"
OUTPUT_FILES="${2}"
 
# Start of script
color_echo "red" "<===============================================================>"
color_echo "yellow" "xvid-encode.sh by slackin. Version 0.1"
color_echo "blue" "Encoding ${INPUT_FILES} as ${OUTPUT_FILES}"
color_echo "blue" "Starting mencoder."
color_echo "red" "Removing all previous logs..."
rm *.log
color_echo "red" "<===============================================================>"
sleep 1
first_pass
sleep 1
renice_pid
sleep 1
check_pid
sleep 1
second_pass
sleep 1
renice_pid
sleep 1
check_pid
sleep 1
color_echo "blue" "\nDone encoding! Output file: ${OUTPUT_FILES}"
 
 
#--------------------------------------------------------------------------------------------
# About the script:
#
# Made this script so that I could renice the encodes so that it will not cause my 
# cpu freq scaling scale up for the encode. Takes a while longer to encode, but my
# desktop generates less heat and noise while transcoding.
#
# Just another one in a collection of tools for UrbanTerror by slackin.
# Check out the Pick-Up game system, PUGBOT @ http://pugbot.com/
# To talk about this script or any other script/tool by slackin,
# try using the pugbot forums, link to the forums: http://forums.pugbot.com/
#
# Hope one of you guys finds this useful!
#--------------------------------------------------------------------------------------------