[MPlayer-users] encode to theora

Ivan Popov pin at medic.chalmers.se
Tue Aug 10 14:39:29 CEST 2004


Hello,

I am using slightly modified theora-encoder script
by Bernhard Rosenkraenzer and another script which lets me
(not reliably though) estimate the size of the resulting theora file,
without encoding the whole thing.

As I think those changes and the script may be useful to somebody,
I am posting them here. Sorry for a possible inconvenience.

Best regards,
--
Ivan
-------------- next part --------------
#!/bin/sh
# Theora encoder -- encodes any video file playable by mplayer to Theora
# (c) 2003 Ark Linux, written by Bernhard Rosenkraenzer <bero at xxxxxxxxxxxx>
#
# Released under the GNU GPL v2 or if, and only if, the GPL v2 is ruled
# invalid in a court of law, any subsequent version of the GPL.
#
# Minor tweaks by Ivan Popov <pin> July 2004
#
ARGS=""
AUDIO=""
VIDEO=""
while [ "`echo $1 |cut -b1`" = "-" ]; do
	case $1 in
	-a|--audio-quality)
		shift
		ARGS="$ARGS -a $1"
		;;
	-A|--audio-bitrate)
		shift
		ARGS="$ARGS -A $1"
		;;
	-v|--video-quality)
		shift
		ARGS="$ARGS -v $1"
		;;
	-V|--video-bitrate)
		shift
		ARGS="$ARGS -V $1"
		;;
	-o|--output)
		shift
		DEST="$1"
		if [ "`echo $DEST |cut -b1`" != "/" ]; then
			DEST="`pwd`/$DEST"
		fi
		DEST="\"$DEST\""
		ARGS="$ARGS -o $DEST"
		;;
	-aa|--audio-arg)
		# Pass an argument to the audio decoder
		shift
		AUDIO="$AUDIO \"$1\""
		;;
	-va|--video-arg)
		# Pass an argument to the video decoder
		shift
		VIDEO="$VIDEO \"$1\""
		;;
	-aspect)
		# Aspect ratio of input file
		shift
		VIDEO="$VIDEO -aspect \"$1\""
		;;
	-s|--aspect-numerator)
		# Pixel aspect ratio numerator
		shift
		ARGS="$ARGS -s $1"
		;;
	-S|--aspect-denominator)
		# Pixel aspect ratio denominator
		shift
		ARGS="$ARGS -S $1"
		;;
	-f|--framerate-numerator)
		# Framerate ratio numerator
		shift
		ARGS="$ARGS -f $1"
		;;
	-F|--framerate-denominator)
		# Framerate ratio denominator
		shift
		ARGS="$ARGS -F $1"
		;;
	*)
		echo "WARNING: Unknown option $1 ignored!" >&2
	esac
	shift
done
if [ -z "$1" -o "$#" != "1" ]; then
	cat >&2 <<EOF
Usage: $0 
         [-a audioquality|-A audiobitrate]
         [-v videoquality|-V videobitrate]
         [-s pixel-aspect-numerator] [-S pixel-aspect-denominator]
         [-f framerate-numerator] [-F framerate-denominator]
         [-aa audio-decoder-arg [-aa audio-decoder-arg] ...]
         [-va video-decoder-arg [-va video-decoder-arg] ...]
         [-o outputfile]
           inputfile
EOF
	exit 1
fi
SRC="$1"
if [ "`echo $SRC |cut -b1`" != "/" ]; then
	SRC="`pwd`/$SRC"
fi
SRC="\"$SRC\""
DIR=`mktemp -d /tmp/theoraXXXXXX`
cat >$DIR/run.sh <<EOF
trap "rm -rf $DIR" 0 1 2 3 4 6 8 9 11 13 15
cd $DIR
mkfifo -m 0600 stream.yuv stream.wav
mplayer -ao pcm -aofile stream.wav -vo null -vc null $AUDIO $SRC &>mplayer-audio.log &
mplayer -vo yuv4mpeg -ao null -nosound $VIDEO $SRC &>mplayer-video.log &
theora_encoder_example $ARGS stream.wav stream.yuv
rm -rf $DIR
EOF
chmod +x $DIR/run.sh
exec $DIR/run.sh
-------------- next part --------------
#!/bin/sh
#
# Estimate length of a theora-encoded file
# given the length of the movie and the encoding options.
#
# Ivan Popov, pin, Aug 2004, GPL 2
#
v=0.1
# ver $v

usage(){
  echo "\
Usage: $0
         <object-to-encode>
         <length-in-minutes>
         <preprocessing-args-for-mencoder-or-a-literal-minus>
           [theora-encoder-args]"
}

case x"$3" in
x) usage; exit 1 ;;
esac

object="$1"
length=`expr "$2" '*' 60`

case x"$3" in
x-) pargs="" ;;
*)  pargs="$3" ;;
esac

shift; shift; shift

# use samples of a few seconds
# length of a sample (sec)
dl=3
# number of samples
num=10
# skip the intro in the calculations (sec)
bskip=`expr 5 '*' 60`
# skip the text at the end (sec)
eskip=`expr 10 '*' 60`

tmpdir=/tmp/thees$$

trap "rm -rf $tmpdir; exit" 0 1 2 3 15

mkdir $tmpdir || exit 1

tmpavi="$tmpdir/tmp.avi"
tmpogg="$tmpdir/tmp.ogg"

endpos=`expr "$length" - "$bskip" - "$eskip" - "$dl"`

n=0
while [ "$n" -lt "$num" ]; do
# calculate a random position
  pos=`expr "$endpos" '*' "$n" / "$num" + "$bskip"`
  echo "\
processing a $dl sec sample at `expr "$pos" / 60` min `expr "$pos" % 60` sec"
  mencoder $pargs -ss "$pos" -endpos "$dl" \
           -ovc lavc -lavcopts vbitrate=5000 -oac copy -o "$tmpavi" \
           "$object" >"$tmpdir"/pre.log 2>&1
  theora-encoder "$@" -o "$tmpogg"."$pos" "$tmpavi" >"$tmpdir"/enc.log 2>&1
  n=`expr "$n" + 1`
done

estimation=`cat "$tmpogg".* | wc -c`
estimation=`expr "$length" / "$num" '*' 10 / "$dl" '*' $estimation / 10 / 1024 / 1024`

biggest=`ls -s "$tmpogg".* | sort -rn | awk '{print $1; exit}'`
smallest=`ls -s "$tmpogg".* | sort -n | awk '{print $1; exit}'`
echo "\
biggest sample is  $biggest
smallest sample is $smallest
ratio is `echo $biggest/$smallest | bc -l`"

estimation=`ls -l "$tmpogg".* | awk '{sum += $5}END{print sum}'`
estimation=`expr "$length" / "$num" '*' 10 / "$dl" '*' $estimation / 10 / 1024 / 1024`

echo "\
Estimated length of '$object'
 encoded with preprocessing '$pargs'
 and theora options '$*'
  is $estimation MB"

# we are waiting for possible examination of /tmp/thees*/*ogg* files
# before we clean up

echo "== now you have a chance to examine '$tmpogg.*' files"
echo "== press ENTER to clean up and finish"

read a

exit


More information about the MPlayer-users mailing list