Okay, hear goes.  I hacked together this script in an hour last Chirstmas back when I was just learning how to use trancode.  I got it working and I’ve pretty much left it alone since then.  I’m in the process of rewriting it but this script is useful.

 

I took the ideal from a program, auto gordian knot, that I was using on windows.  The ideal behind autogk was you set some basic parms, such as size and if you want dvix or xvid.  Then you push go and walk away.  You can cue up a bunch of files and it will figure out the rest.

 

The script that I lovingly called agk, does pretty much the linux version of that.  There are only two parameters that you need to feed the script.  The –s <size> option to tell it the size of the file you want it to put out, and then the file(s) that you want it to work on.  This version only supports VOBs but its easy to change it to handle what ever you want it to.  Just tell it the size of the file you want and it will figure out the bitrate and the rest of it on its own.  It’s pretty accurate most of the time, hitting with in 5% to 10% of the target size.

 

There are only 3 more options that you will find useful.  The –b <bitrate> option which tells the script what bitrate you want the audio to be encoded at.  The default is 128 mp3.  The next option is the –A which tell it not to convert the audio file to mp3 and leave it as it.  This is to preserve the 5.1 audio files.  The final option is the –F which tells it to operate in the foreground and not submit the file to the batch queue, more on that later.  The –F option is good determine if transcode is going to handle the file right before you feed it to the batch queue.

 

Now the real beauty of this script is how it interacts with the linux batch queue system.  The default operation is to take a VOB file and submit a script to the batch queue to process it.  I generally us it to process large amounts of anime and TV shows in the background.  The command I use 98% of the time.

 

agk –s 600 –b 128 *.VOB

 

It tells the script that I want every avi to be 600 megabytes in size and encoded with 128bps mp3 for audio.  You can use the –a option to select which audio track.  Then the script will take every VOB file and submit a file to batch to process it.

 

After the script has been submitted to the batch queue you then walk away and let the machine do the rest.  The batch command will then execute each script in the background, with a lower priority so not to hog the machine.  The user then can go on to something else or even log off and leave the machine to do its work.  Even if the machine crashes or power is interrupted the only work lost will only be the current files that it was working on.  The rest of the files will remain in the batch queue when the machine comes back up. 

 

Under most linux machines if the work load gets above a certain point the batch command will stop processing the queue until the load gets below a certain point.  On my linux machine the work load limit is set to 1.5.  On machines that are powerful enough the batch command can be set to process several files at one time so its possible to have several copies of transcode running at one time. 

 

Another good thing about using the batch queue is each file can have different parameters.  You can submit one file to be encoded as a divx then turn around, change the script and submit another one to be encoded as a mpeg2.

 

Jeff

 

#!/bin/sh
#
#     $Id: agk,v 1.1 2005/12/22 02:44:09 jwhyche Exp jwhyche $
#
#
#     $Log: agk,v $
#     Revision 1.1  2005/12/22 02:44:09  jwhyche
#     Initial revision
#
#

_filesize=0
_progress_off="--progress_off"
_optstring="s:b:FAa:DI:"

set -- `getopt ${_optstring} $*`
if [ $? != 0 ]
    then
    exit -1
fi
while [ $1 != -- ]
  do
  case $1 in
       -s)
            _filesize=$2
            shift; shift
            ;;
       -b)
            _audio_bitrate=$2
            shift; shift;
            ;;
       -F)
            _batch_mode=1
            _progress_off=""
            shift
            ;;
       -A)
            _ac3_passthru="-A -N 0x2000"
            shift
            ;;
       -a)
            _audio_track=$2
            shift; shift;
            ;;
      -D)
            _deinterlace="-J smartdeinter=threshold=15:Blend=1:diffmode=2:cubic=1:highq=1"
            shift
            ;;
      -I)
            _deinterlace="-I $2"
            shift; shift;
            ;;
  esac
done
shift

# Translate _filesize into MB
let _filesize=_filesize*1024

for _target in $*
  do
 
  # Get Video Frame Rate and Code
  #tcscan -i ${_target} > scan.$$ 2>&1
  #_frame_rate=`awk '/sequence:/ {print $4}' scan.$$`
  _frame_rate=`tcprobe -i ${_target} | awk '/frame rate: -f/ {print $4}'`
  case ${_frame_rate} in
    23.976) _frame_code=1;;
    24) _frame_code=2;;
    25) _frame_code=3;;
    29.970) _frame_code=4;;
    30) _frame_code=5;;
  esac

  _out_file=`echo ${_target} | sed -e 's/.VOB$/.avi/g'`
 
  # Build shell script to submit to batch command
  echo _S=${_filesize} > ${_target}.sh
  echo _target_file=${_target} >> ${_target}.sh
  echo _out_file=${_out_file} >> ${_target}.sh

  # Detect Audio bit rate for 5.1
  if ${_ac3_passthru}  2> /dev/null
  then
       echo "_audio_brate=${_audio_bitrate:=128}" >> ${_target}.sh
  else
       echo "_ac3_bitrate=\`tcextract -i ${_target}  -t vob -x ac3 -a ${_audio_track:-0}| tcscan -x ac3 | awk '{print \$8}' | sort -u\`
       _audio_brate=\${_ac3_bitrate}
       case \${_ac3_bitrate} in
            192)
                _ac3_input=\"-E 48000,16,2\"
                _ac3_output=\"-e 48000,16,2\"
                ;;
            448)
                _ac3_input=\"-E 48000,16,6\"
                _ac3_output=\"-e 48000,16,6\"
                ;;
       esac " >> ${_target}.sh 
  fi
  echo "let _A=_audio_brate/8" >> ${_target}.sh

  # Get the number of frames in the VOB
  echo "tccat -i ${_target} | tcdemux -W > /dev/null 2>/tmp/frame.\$\$" \
       >> ${_target}.sh
  echo "_frame_count=\`awk '/video frame\(s\) in unit 0 detected/ {print \$2}' /tmp/frame.\$\$\`" >> ${_target}.sh
  echo "rm -f /tmp/frame.\$\$" >> ${_target}.sh
 
  # Calculate the running time of the video in seconds
  echo "_L=\`tcprobe -i ${_target} | awk -v frame=\${_frame_count} \
       '/frame rate: -f/ {print (frame/\$4)}'\`" >> ${_target}.sh

  # Get the bitrate for the correct filesize using
  # ({S}ize - ({A}udio x {L}ength)) / {L}enght = {V}ideo Bitrate
  # {S} Size of output file in KM
  # {A} Audio bitrate in KB/s (note 224 kbits/s = 224 / 8 = 28 KB/s)
  # {L} Lenght of Video Clip in Seconds
  # {V} Video bitrate in KB/s, to get kbit/s multiply 8

  # Want to calculate bitrate on the fly because of the time it
  # it takes to do so
  echo "_vid_brate=\`echo | awk -v S=\${_S} -v L=\${_L} -v A=\${_A} \
       '{printf \"%d\", ((S-(A*L))/L)*8}'\`" >> ${_target}.sh

  # Insert transcode commands to do the work
  # First Pass
  echo "transcode -C 3 -a ${_audio_track:-0} -x vob -i \${_target_file} \
       -w \${_vid_brate},50 -b \${_audio_brate},0,0\
       --a52_drc_off  ${_ac3_passthru}  \
       -M 2 -R 1,\$\$.log -y xvid4,null -o /dev/null \
       -J 32detect=force_mode=3,hqdn3d \
       --print_status 20 ${_progress_off} \
       ${_deinterlace}  && " >> ${_target}.sh

  # Second Pass
  echo "transcode -C 3 -a ${_audio_track:-0} -x vob -i \${_target_file} \
       -w \${_vid_brate},50 -b \${_audio_brate},0,0  \
       -J 32detect=force_mode=3,hqdn3d\
       --a52_drc_off  ${_ac3_passthru}  \
       -M 2 -R 2,\$\$.log -y xvid4, -o \${_out_file} \
       ${_deinterlace}  --print_status 20 ${_progress_off} " >> ${_target}.sh

  # Fix 6 Channel Surround Sound in AVI files with it
  echo "if [ \${_ac3_bitrate:=0} -eq 448 ]
  then
       avifix -i \${_out_file} -e 48000,16,6 -a ${_audio_track:-0}
  fi" >> ${_target}.sh
  # Excute in the forground or submit it to batch command
  if [ ! ${_batch_mode} ] 
  then
       echo ${_target} >> batch.log
       batch -f ${_target}.sh  >> batch.log 2>&1
  else
       /bin/sh ${_target}.sh
  fi

done




 

 

 

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Phil Ehrens
Sent: Thursday, June 29, 2006 1:06 PM
To: transcode Users Mailing List
Subject: Re: [transcode-users] Script Posting?

 

Jeff Hyche wrote:

> What are the rules here on script posting?  I've only seen little snippets

> of script and code goes through but not a real functioning script.  I

> thought I would ask because the last list that I posted a script to, well,

> lets just say it wasn't pretty.   Did you know that when you try to peal the

> tar off with the feathers huge chunks of skin want to come too?

 

Go for it. And the word is "peel". Peal is what a bell does.

Reply via email to