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:vhq=4:me_quality=6:chroma_me:chroma_opt:lumi_mask:quant_type=mpeg:bvhq=1:hq_ac:trellis:qpel:gmc:chroma_opt:bitrate=700100:threads=4"
 
#
# 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 1
	check_pid
	return
    else
	return
    fi
}
 
# renice the process so it doesn't scale up cpu frequency scaling.
renice_pid() {
    PID=$(pidof mencoder)
    color_echo "yellow" "Renicing 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" "Starting first pass."
    mencoder ${INPUT_FILES} -oac copy -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" "Starting second pass."
    mencoder ${INPUT_FILES} -oac mp3lame -ovc xvid -xvidencopts ${xvid_opts} -o ${OUTPUT_FILES}.avi 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/}'`
 
 
# 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}.avi"
color_echo "blue" "Starting mencoder."
color_echo "red" "<===============================================================>"
first_pass
renice_pid
check_pid
second_pass
renice_pid
check_pid
color_echo "blue" "Done encoding! Output file: ${OUTPUT_FILES}.avi"
 
 
#--------------------------------------------------------------------------------------------
# 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!
#--------------------------------------------------------------------------------------------