Mailing List archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vdr] VDR -> DivX Converter Script




Hi,

it's finally done :-)
I have made a shell script, to be precise 3 scripts, which can be used in
conjunction with VDR and mencoder (from the mplayer package) to
automatically encode edited VDR files to DivX4 2pass AVIs.

I don't have the time to write a complete documentation at the mo, so just a
short note on how it works, and what needs to be done !

Okay, first of all you need to have VDR and mencoder (from the mplayer
package) running and the divx codec (from divx.com installed (see mplayer
for details)).

Short description how it works:
One Script (nq.sh) is called from VDR after cutting (editing) a recording.
This script basically creates a new Job in a job queue. This is needed,
because we don't want multiple encoding processes at the same time !
Then there is a standalone scipt (wait2enc.sh). This is mainly an endless
loop, which checks for new jobs in the job queue, and if it finds one, it
calls the third script, which tries to guess the outoput avi name and
calculates the needed bitrate for encoding and starts the actual encoding
process...
So it's fully automatic !

How to install:
So basically copy nq.sh to a place you like and invoke it in VDR with the -r
/path/to/script/nq.sh switch. Of course you can do this in runvdr, too.

Then you copy wait2enc.sh and 2divx into a Dir you like them and change the
path of 2divx in the header of wait2enc.sh.
Maybe you like to change some of the parameters in three scripts to your
needs:
Warning, you should adjust the path for the JobQ File (Job Queue), by
default it is /video0/JobQ, maybe this doesn't fit your needs...
Also the path for the output AVIs should be changed to your needs :-)
Maybe you don't like the default CD Size of 795MB, which is fine for CD-R 90
media ;-) Try 695 or 645 instead...

When all parameters are fine, simply start wait2enc.sh and then use VDR to
edit a recording.

Everything else will be done automatically (hopefully).


If you find this scripts useful, have bugreports or any suggestions, feel
free to contact me.
Maybe i'll complete a documentation and some config stuff soon, too.


AFAIK it is not possible to use attachments, i put the shell in the mail
text ...

So, here is the 1st Script "nq.sh"

-------------------------------------------------------------
snip ------------------------------------------------
#!/bin/sh
# NQ means EnQueue into JobQ ! This is to be calles from mplayer to add a
job to
# the queue for divx conversion ...
#
# The Name and Path of the JobQ File is needed here
declare JOBFIL="/video0/JobQ"
#
#
case "$1" in
  before)
    ;;
  after)
    ;;
  edited)
    if test -f $JOBFIL.lock; # Check JobQ actually Locked !!!
      then while test -f $JOBFIL.lock; do
             sleep 1
    done;
    fi   # Add the Job to the Jobfile !
    echo $2 >> $JOBFIL
    ;;
  *)
    echo "ERROR: unknown state: $1"
    ;;
esac
-------------------------------------------------------------
snap ------------------------------------------------

Then the second one "wait2enc.sh"

-------------------------------------------------------------
snip ------------------------------------------------
#!/bin/sh
# ENCode Queue - this will actually check the JOBQ for Jobs and Encode them
...
#
# Where is the JobQ File found ? - What abou your VDR video DIR ?
declare JOBFIL="/video0/JobQ"

# Where can the Shell Script 2divx be found ?
declare ENCODE="/home/martin/vdr2divxlin/2divx"

#
#

if ! test -f $JOBFIL;
  then echo $JOBFIL not found ! Creating empty one !!!
  echo > $JOBFIL;
fi

if test -f $JOBFIL.lock; then rm $JOBFIL.lock; fi

echo "This one is waiting for Jobs in $JOBFIL to process ....."
echo

while true; do # one of my favorite endless loops ....
  declare LINES=`wc -l $JOBFIL`  #Lines ermitteln (gibt Anzahl & Filename
aus)
  declare LINES=${LINES%$JOBFIL} # Filename wieder entfernen :-)

  if ! test $LINES = 0;
    then
      echo "Found $LINES Job(s) in the Queue, Processing next one ..."
      declare CMDLIN=`head -n1 $JOBFIL`   # Job auslesen
      $ENCODE $CMDLIN     # und encoding starten ...
      echo "Job Done (somehow) - Removing Job form Queue."
      echo InProgress!!!> $JOBFIL.lock   # Job loeschen !
      declare LINES=`wc -l $JOBFIL`
      declare LINES=${LINES%$JOBFIL}
      mv $JOBFIL $JOBFIL\2
      tail -n$((LINES-1)) $JOBFIL\2 > $JOBFIL
      rm $JOBFIL\2
      rm $JOBFIL.lock
      echo "And Waiting for Next Job to process...";
    else
      sleep 60 # Just as windows does it ;-);
  fi
done
-------------------------------------------------------------
snap ------------------------------------------------

And finally the core component "2divx"

-------------------------------------------------------------
snip ------------------------------------------------
#!/bin/sh
#
# Verzeichnis fuer temporaere Dateien
# sollte leer sein ! Wird von diesem Script komplett geleert !
# Noetigenfalls als SubDir von /tmp anlegen :-)
declare WRKTMP="/worktemp"

# Wo findet man das mencoder Executable
# ggf. Mit Hart Kodierten Optionen ...
declare ENCEXE="/usr/local/bin/mencoder -pp 0x20000"

# Wo sollen die fertigen AVIs hinkopiert werden ?
declare DSTDIR="/home/martin/movies"

# Wie gross sind die zu verwendenden CD-R Medien (in MB)
declare CDSIZE=795

# Achtung: Es werden nur 3 Parameter von der Kommandozeile gelesen !
# Werden also als Encoder Optionen mehrere Parameter benoetigt (durch
Leerzeichen
# getrennt) so sind die Parameter durch " " einzuschliessen !
#


SRCDIR=${1%/} # eventuell anhnaengende / entfernen !
DSTAVI=$2
ENCOPT=$3


# Folgende Variablen finden Verwendung:
# SRCDIR = Source Directory (Das initiale Quell Verzeichnis mit den VDR
Dateien
# DSTAVI = Destination AVI Name (Name fuer das fertige AVI)
# DSTDIR = Destination Directory (ZielVerzeichnis fuer fertige AVIs)
# ENCOPT = Encoder Options (weitere zu uebergebende Optionen fuer MEncoder)
# WRKTMP = Work Temp (Temporaeres Verzeichnis zum Arbeiten)
# ONEFIL = One File [Boolean] entscheidet ob nur 001.vdr oder mehr !
# CATSTR = CAT String (Commando String fuer CAT, um 00x.vdr zusammenzubauen)
# ENCSRC = Encoder Source (Name der QuellDatei fuer mencoder)
# CURPTH = Current Path (merken des aktuellen Verzeichnisses !)
# ENCEXE = Encoder Executabe (Pfad zu MEncoder, meist
/usr/local/bin/mencoder)
# VBITRT = Video Bit Rate (Video BitRate fuer DivX Encoding)
# MARKS  = Letzte Zeile aus marks.vdr Datei !
# VIDLEN = Video Length in Sekunden (wird aus marks.vdr errechnet)
# CDSIZE = CD Size (Groesse in MB der CD Medien - zur Bitrate Calculation
...)
# FILES  = Anzahl der Video Files, die auf eine CD passen (wird errechnet)
# ERSTE  = Zaehlervariable wird beim autonaming verwendet ...
# ZWEITE = Zaehlervariable wird beim autonaming verwendet ...
echo

# Checken ob eine Variable Leer, dann USAGE ausgeben
if test -z $SRCDIR;
  then
    echo "Usage: 2divx /path/to_vdr_files [outputaviname] [\"options for
mencoder\"]";
    echo "Example:"
    echo "2divx /video0/\@Pro-7/2001-07-11.19\:55.50.50.rec pro7 \"-frames
100\""
    echo "Would convert the files 00*.vdr in the given path to pro7.avi"
    echo
    echo "If outputaviname is not given, it tries to guess it ... :-)"
    exit;
fi

# Checken, ob ueberhaupt 001.vdr vorhanden ist
if ! test -f $SRCDIR/001.vdr;
  then
    echo A fatal Error occurred... $SRCDIR/001.vdr could not be found,
exiting.
    exit;
fi

# Checken ob outputaviname gesetzt wurde, sonst einen ausdenken :-) !
if test -z $DSTAVI;
  then
    declare -i i=${#SRCDIR} #SRCDIR String parsen auf Name der Episode bzw.
Film
    declare -i ERSTE=0
    declare -i ZWEITE=0
    while i=i-1; test $i -ge 0 && test $ZWEITE = 0; do
 if test ${SRCDIR:$i:1} = "/";
   then if test $ERSTE = 0; then declare ERSTE=$i;
       else declare ZWEITE=$i;
        fi
 fi
    done
    if ! test $ERSTE = 0; # gefundenen Namen annehmen
      then declare DSTAVI=${SRCDIR:$((ZWEITE+1)):$((ERSTE-ZWEITE-1))}
      declare DSTAVI=${DSTAVI#%}; #ggf. % vom Schneiden entfernen
    fi
fi

# Falls immer noch kein Name gefunden dann OutputAVI annehmen !!!
if test -z $DSTAVI; then declare DSTAVI=OutputAVI; fi

echo "Using $DSTAVI.avi as the Output Filename ..."

# Aktuellen Pfad merken !
declare CURPTH=`pwd`

# Inst Verzeichnis $SRCDIR wechslen
cd $SRCDIR


# Checken wieviele Segmente und ggf. zusammenkopieren ...
declare CATSTR="cat 001.vdr"
declare ONEFIL=true

declare -i i=1
while i=i+1; test $i -le 9; do
  if test -f 00$i.vdr;
    then declare CATSTR="$CATSTR 00$i.vdr"; declare ONEFIL=false;
  fi
done

if ! $ONEFIL;
  then
    echo Multiple Files found, concating them into $WRKTMP ...
    $CATSTR > $WRKTMP/all.vdr
    declare ENCSRC="$WRKTMP/all.vdr";
  else
    declare ENCSRC="$SRCDIR/001.vdr";
fi

# Laenge des Quell Videos ermittlen...
if ! test -f $SRCDIR/marks.vdr;
  then echo "Warning ! $SRCDIR/marks.vdr not found. Unable to calculate
Bitrate!"
    echo "Using Bitrate = 1050 kbit/s instead ..."
    declare VBITRT=1050;
  else
    declare MARKS=`tail -1 $SRCDIR/marks.vdr`  # Einlesen,(h)h:mm:ss.ff
    if test ${MARKS:1:1} = ":";  # Format h:mm:ss.ff
      then declare
VIDLEN=$((${MARKS:0:1}*3600+${MARKS:2:2}*60+${MARKS:5:2}));
      else declare
VIDLEN=$((${MARKS:0:2}*3600+${MARKS:3:2}*60+${MARKS:6:2})); # Format
hh:mm:ss.ff
    fi  # Hier ist dann die Video Laenge ermittlet ! VIDLEN in Sekunden !
    if test $(($VIDLEN/60)) -gt 60; then declare FILES=1;
      else if test $(($VIDLEN/60)) -le 30; then declare FILES=4;
             else declare FILES=2;
    fi
    fi # Anzahl der Files pro CD anhand der Laenge des Videos ermittlet !
    declare VBITRT=$((($CDSIZE/$FILES*1024*8/$VIDLEN)-160)) # -160 fuer
Audio!
    if test $VBITRT -gt 6000; then declare VBITRT=6000; fi
    echo "The Video has a length of $VIDLEN seconds."
    echo "We will encode with a Video Bitrate of $VBITRT kbit/s and try to"
    echo "fit $FILES file(s) on a CD with a Size of $CDSIZE MB."
fi

# Hier ist jetzt auf jeden Fall eine QuellDatei vorhanden
# Name und Pfad finden sich in der Variable $ENCSRC (/path/xxx.vdr)
echo Starting Encoding for File $ENCSRC ...


# Ins WORK Temp Verzeichnis wechseln
cd $WRKTMP
# MEncoder mit entsprechenden Optionen aufrufen ... (2passes)
$ENCEXE $ENCOPT $ENCSRC -divx4opts br=$VBITRT -pass 1
$ENCEXE $ENCOPT $ENCSRC -divx4opts br=$VBITRT -pass 2

# Testen ob test.avi erzeugt wurde !
# Spaetere Versionen von mencoder / divx4linux werden wohl NICHT test.avi
# erzeugen !!!
if ! test -f $WRKTMP/test.avi;
  then echo fatal: Output File was not created, exiting.; exit;
fi

# Auf eventuell vorhandenen AVI Namen checken und ggf Zahl anhaengen,
# das verhindert jegliches Ueberschreiben von AVIs !
if test -f $DSTDIR/$DSTAVI-$VBITRT.avi;
  then declare -i i=2
    while test -f $DSTDIR/$DSTAVI$i-$VBITRT.avi; do
      i=i+1
    done
    declare DSTAVI=$DSTAVI$i;   # Zaehler anhaengen ...
fi

# Fertiges AVI verschieben
mv test.avi $DSTDIR/$DSTAVI-$VBITRT.avi
# DEBUG : echo test.avi > $DSTDIR/$DSTAVI-$VBITRT.avi
echo Output AVI can be found as $DSTDIR/$DSTAVI-$VBITRT.avi ...

# WORKDIR aufraeumen !!!
echo Removing temporary files ...
if test -f $WRKTMP/analyse.log; then rm $WRKTMP/analyse.log; fi
if test -f $WRKTMP/divx2pass.log; then rm $WRKTMP/divx2pass.log; fi
if test -f $WRKTMP/all.vdr; then rm $WRKTMP/all.vdr; fi

# Ins urspruengliche Verzeichnis wechseln
cd $CURPTH

echo "Done."
-------------------------------------------------------------
snap ------------------------------------------------

That's all ;-)

I guess i have to look for some web space, where to upload those babies in
the future ....

Martin.

EOM. (End of Mail)




Home | Main Index | Thread Index