#!/bin/bash -e # Encode a TV programme recorded by MythTV into a more convenient format. # This uses the heuristics suggested in the MPlayer manual to figure out the # output frame size. # Adam Sampson ifile="not-set" ofile="not-set" tsize=$((350 * 1024 * 1024)) daspect=$(python -c "print 16.0/9") cq=0.21 aformat="none" aquality=3 cropw="" croph="" tempdir="/var/tmp" decopts="" encopts="" mplayer="mplayer" mencoder="mencoder" usage () { echo "Usage: encode-prog [OPTIONS] INPUT-FILE" echo echo "-o FILE Write output to FILE (default INPUT-FILE.mkv)" echo "-s SIZE Aim for output files of size SIZE megabytes (default 350)" echo "-q QUALITY Use quality QUALITY for video (default 0.21)" echo "-O Reencode audio to Ogg Vorbis (default leave as MP2)" echo "-Q QUALITY Use quality QUALITY for reencoded audio (default 3)" echo "-a ASPECT Input files have aspect ratio ASPECT (default 16/9)" echo "-W WIDTH Crop to WIDTH pixels wide (e.g. 540 for BBC 4:3)" echo "-H HEIGHT Crop to HEIGHT pixels tall (e.g. 432 for Film4 2.39:1)" echo "-t TEMPDIR Use TEMPDIR as temporary directory (default /var/tmp)" echo "-d EXTRA Give extra options EXTRA to mplayer/mencoder (default none)" echo "-e EXTRA Give extra options EXTRA to mencoder (default none)" } while getopts "o:s:q:OQ:a:W:H:t:d:e:" c ; do case $c in o) ofile="$OPTARG" ;; s) tsize=$(($OPTARG * 1024 * 1024)) ;; q) cq="$OPTARG" ;; O) aformat="vorbis" ;; Q) aquality="$OPTARG" ;; a) daspect=$(python -c "print $(echo "$OPTARG" | sed 's,/,.0/,')") ;; W) cropw="$OPTARG" ;; H) croph="$OPTARG" ;; t) tempdir="$OPTARG" ;; d) decopts="$OPTARG" ;; e) encopts="$OPTARG" ;; \?) usage exit 0 ;; esac done shift `expr $OPTIND - 1` if [ "$1" = "" ] ; then usage exit 1 fi ifile="$1" if [ "$ofile" = "not-set" ] ; then ofile="$ifile.mkv" fi if [ "$aformat" = "vorbis" ] ; then afile="$tempdir/audio.ogg" else afile="$tempdir/audio.mp2" fi clean () { rm -f $tempdir/audio.wav audio.log $afile lavc_stats.txt divx2pass.log $tempdir/video.avi } clean isize=$(echo q | $mplayer $decopts -ao null -vo null -v "$ifile" 2>&1 | grep ^VIDEO: | awk '{print $3}') iwidth=$(echo $isize | sed 's,x.*,,') if [ "$cropw" != "" ] ; then iwidth="$cropw" fi iheight=$(echo $isize | sed 's,.*x,,') if [ "$croph" != "" ] ; then iheight="$croph" fi if [ "$aformat" = "vorbis" ] ; then $mplayer $decopts -vc null -vo null -ao pcm:fast:file=$tempdir/audio.wav "$ifile" | tee audio.log length=$(tr '\r' '\n'