Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[HOWTO] x264 / xvid encoding - scripts
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
wipp
n00b
n00b


Joined: 27 Mar 2004
Posts: 32
Location: Zurich, Switzerland

PostPosted: Fri Dec 28, 2007 11:12 pm    Post subject: [HOWTO] x264 / xvid encoding - scripts Reply with quote

0. Introduction
I've made some scripts to encode videos to x264 or xvid using mencoder. I used and improved them since years now and I think they could begin to be useful (suggestions for improvements are welcome).

1. Features

  • x264 / xvid encoding (more codecs can be added easily)
  • Audio denoising / normalizing, encoding to ogg
  • Option to split channels
  • mkv output file
  • Files are updated if their dependencies change like using make


2. Usage
In the silmplest case the following command will do the job:
Code:
./encode video.avi

The result will be video.mkv.
Here are additional options:
Code:

./encode [ -o <out> ] [ -t <target> ] [ --2c ] [ --denoise3d ] [ --denoise ] [ -c <codec> ] [ --vopts <vopts> ] [ --aopts <aopts> ] <input>

Options
   -o <out>
         Sets the output file to <out>
   -t <target>
         Sets the target to <target>. Valid targets are wav, split_wav, ogg, log, avi, mp4, mkv.
         The scripts performs all steps to build/uptade the specified target. The default target is mkv.
   --2c
         Splits the stereo sound track of the video in two mono tracks and adds them seperately to the mkv
   --denoise3d
         Activates mplayer's denoise3d video filter (on by default)
   --denoise
         Denoises the audio tracks before encoding to ogg
   -c <codec>
         Sets the codec to <codec>. A mplayer configuration file <codec>.prf must be present in the subdirectory [i]profiles[/i].
   --vopts <vopts>
         Passes additional options to mplayer for the video encoding passes.
   --aopts <aopts>
         Passes additional options to mplayer for demuxing the audio
   <input>
         The input file


3. Scripts
The scripts have to be placed in the same directory and the prf files go to a subdirectory profiles. The following packages are necessary:

  • mplayer
  • vorbis-tools
  • mkvtoolnix
  • sox
  • normalize
  • gpac

For the sound denoising to work a directory noise has to be created and a sample of noise (noise.wav) has to be put in it together with a noise profile for sox (noise.prof). To get a profile for noise.wav:
Code:

sox noise.wav noiseprof noise.prof

The content of the directory should look like this:

  • demux_sound
  • denoise
  • encode
  • encode_video
  • mk.sh
  • split_channels
  • profiles
    - x264.prf
    - xvid.prf
  • noise
    - noise.prof
    - noise.wav


demux_sound:

#!/bin/bash
while getopts ":o:m:" opt; do
        case $opt in
       o )     out=$OPTARG ;;
       m )     opts=$OPTARG ;;
       \? )    echo "usage: demux_sound [-o file] arg"
      exit 1
   esac
done
shift $(($OPTIND - 1))
arg=$1
out=${out:-"${arg%.*}.wav"}
mplayer $opts -vc dummy -vo null -ao pcm:fast:file=$out $arg

denoise:

#!/bin/bash
while getopts ":o:" opt; do
        case $opt in
      o )     out=$OPTARG ;;
      \? )    echo "usage: denoise [-o file] arg"
         exit 1
   esac
done
shift $(($OPTIND - 1))
arg=$1
sox $arg temp.wav noisered noise/noise2.prof 0.01
mv temp.wav $arg

encode:

#!/bin/bash
eval arg=\${$#}
prefix=${arg%.*}
out="${prefix}.mkv"
prefix=${prefix##*/}
grey=""
ch=1
denoise3d=1
denoise=1
codec="x264"

source mk.sh
target=mkv

while [ ${#1} -gt 0 ] && [ ${1:0:1} == "-" ]; do
    case $1 in
   -o )      out=$2
                   shift ;;
   -t )      shift
         target=$1 ;;
   --grey )   grey="--grey " ;;
   --2c )      ch=2 ;;
   --denoise3d )   shift
                   denoise3d=$1 ;;
   --denoise )     shift
                   denoise=$1 ;;
   -c )            shift
                   codec=$1 ;;
   --vopts )       shift
                   vopts=$1 ;;
   --aopts )       shift
                   aopts=$1 ;;
   * )           echo "Syntax error in"
                   echo $1
                     echo 'Syntax: encode [ -o <out> ] [ -t <target> ] [ --2c ] [ --denoise3d ] [ --denoise ] [ -c <codec> ] [ --vopts <vopts> ] [ --aopts <aopts> ] [ -y <shift> ] <input>'
       exit 1 ;;
    esac
    shift
done

prefix=${out%.*}

arg_files="$arg"
wav_files="${prefix}.wav"
split_wav_files="${prefix}_l.wav ${prefix}_r.wav"
log_files="${prefix}.log"
mp4_files="${prefix}.mp4"
mkv_files="${prefix}.mkv"

arg_deps=""
wav_deps="arg"
split_wav_deps="wav"
log_deps="arg"
avi_deps="log"
mp4_deps="avi"

if [ $ch -eq 2 ] ; then
    ogg_files="${prefix}_l.ogg ${prefix}_r.ogg"
    ogg_deps="split_wav"
else
    ogg_files="${prefix}.ogg"
    ogg_deps="wav"
fi

case $codec in
    xvid )
   avi_files="${prefix}_xvid.avi"
   mkv_deps="avi ogg" ;;
    x264 )
   avi_files="${prefix}.264"
   mkv_deps="mp4 ogg" ;;
esac

log () { ./encode_video ${codec} ${grey}--dn3d $denoise3d -m "$vopts" $1 ; }
avi () { ./encode_video ${codec} --pass 2 ${grey}--dn3d $denoise3d -m "$vopts" -o ${avi_files} $arg ; }
wav ()
{
    ./demux_sound -m "$aopts" -o ${wav_files} $1 &&
    if [ $denoise -eq 1 ] ; then ./denoise ${wav_files}; fi &&
    normalize ${wav_files}
}
split_wav () { ./split_channels $1 ; }
ogg () { oggenc $@ ; }
mp4 () { MP4Box -add $1 ${mp4_files} ; }
mkv () { mkvmerge -o "${mkv_files}" $@ ; }

mk $target


encode_video:

#!/bin/bash
eval arg=\${$#}
pass=1
dn3d=0
codec=$1
profile=""
shift
while [ ${#1} -gt 0 ] && [ ${1:0:1} == "-" ]; do
    case $1 in
   --pass )   shift
         pass=$1
              shift ;;
   --grey )   profile=",${codec}_g"
              shift ;;
   -o )      shift
              out=$1
              shift ;;
   -m )      shift
              options=$1
              shift ;;
   --dn3d )   shift
              dn3d=$1
         shift ;;
   * )        echo "Wrong syntax in:"
              echo $1
              echo "Syntax: encode_video [ --pass=n ] [ --grey ] [ -o out ] [ -m opts ] [ --dn3d ] avifile.avi"
              exit 1 ;;
    esac
done

if [ $dn3d -eq 1 ]; then
    vf=${options#*-vf }
    if [ "$vf" != "$options" ]; then # $options contains "-vf"
   options1=${options%-vf*}
   options2=${vf#* }
   vf=${vf%% *},denoise3d
   options="$options1 $options2 -vf $vf"
    else
   options="$options -vf denoise3d"
    fi
fi
prefix=${arg%.*}
out=${out:-"${prefix}.264"}
log="-passlogfile ${prefix}.log"
if [ $pass -eq 1 ]; then
    out="/dev/null"
fi
mencoder -include profiles/${codec}.prf -profile ${codec}_${pass}${profile} $arg $options ${log} -o $out


mk.sh:

#!/bin/bash

mk () {
    while [ -n "$1" ]; do
   deps="${1}_deps"
   mk ${!deps}
   deps="${1}_deps"
   files="${1}_files"
   update=0
   dep_files=""
   for i in ${!files} ; do
       for j in ${!deps} ; do
      dep_files_id=${j}_files
      dep_files="$dep_files ${!dep_files_id}"
       done
       if ! [ -e $i ] ; then update=1; break; fi
       for k in ${dep_files} ; do
      if [ $k -nt $i ] ; then update=1 ; fi
       done
   done
   if [ $update -eq 1 ] ; then $1 $dep_files; fi
   shift
    done
}

split_channels:

#!/bin/bash
arg=$1
arg="${arg%.*}"
arg_l="${arg}_l.wav"
arg_r="${arg}_r.wav"
mplayer -vc dummy -vo null -ao pcm:file=$arg_l -af channels=1 $1
mplayer -vc dummy -vo null -ao pcm:file=$arg_r -af channels=1:1:1:0 $1


x264.prf:

[x264]
profile-desc="x264 encoding"
nosound=yes
ovc=x264=yes
of=rawvideo=yes
x264encopts=subq=7:partitions=all:8x8dct=yes:frameref=5:me=umh:bframes=3:b_pyramid=yes:weight_b=yes:chroma_me=1:qp=26

[x264_1]
profile-desc="x264 encoding - 1st pass"
profile=x264
x264encopts=pass=1

[x264_2]
profile-desc="x264 encoding - 2nd pass"
profile=x264
x264encopts=pass=2:bitrate=800

xvid.prf:

[xvid]
profile-desc="xvid encoding"
nosound=yes
ovc=xvid=yes
xvidencopts=gmc=1:qpel=1:me_quality=6:vhq=4:chroma_opt=yes:chroma_me=yes

[xvid_1]
profile-desc="xvid - 1st pass"
profile=xvid
xvidencopts=pass=1

[xvid_2]
profile-desc="xvid - 2nd pass"
profile=xvid
xvidencopts=pass=2:bitrate=1100

[xvid_g]
profile-desc="xvid - greyscale"
xvidencopts=chroma_opt=no:chroma_me=no:greyscale=yes


4. Short description
mk.sh
The function mk() recursively builds or updates its targets. Each target tgt consists of a function with the same name (tgt()), a variable tgt_deps containing the names of other targets tgt depends on, separated by spaces and a variable tgt_files containing the names of files which will be built by the function tgt().
encode
This is the main script providing the rules to build the target (mkv by default). Usage is given above.
encode_video
Encodes the video.
Code:

encode_video [ --pass=<n> ] [ --grey ] [ -o <out> ] [ -m <opts> ] [ --dn3d ] <input>

Options
   --pass=<n>
         Sets first pass (<n>=1) or second (<n>=2)
   --grey
         Enables greyscale encoding (only for xvid)
   -o <out>
         Sets the output file to <out>
   -m <opts>
         Passes the additional options <opts> to mplayer
   --dn3d
         Activates mplayer's denoise3d video filter (on by default)
   <input>
         The input file

demux_sound
Demuxes the sound to a wav file
denoise
Uses sox to denoise the wav
split_channels
Splits a stereo sound track track.wav in two mono sound tracks track_l.wav and track_r.wav

5. Remarks
Please let me know what you think. All sorts of feedback are welcome!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum