[MPlayer-users] batch script to generate fade-in/fade-out on many video clips

DaveG mplayer01 at dgmm.net
Sat Feb 3 15:10:52 CET 2007


Hi,

I'm working on a script to automatically create a fade in and fade out at each 
end of a video clip.  I have many clips right now to edit and the likelyhood 
of many more ion the future, hence the reason for scripting it.

What I have so far involves spitting out each clip as a set of frames, using 
imagemagick to adjust brightness levels on the fir n frames and the last n 
frames then re-encoding back to video.

So, first of all, am I re-inventing the wheel here?  Has anyone already done 
this?

Secondly, if it's not been done already what do you think of this first 
version of the script and is there any way to improve it?  eg could I stream 
or pipe the video so I don't fill up the hard disk with potentially 1000's of 
frames?

Or should I be asking this in comp.unix.shell?

#! /bin/bash
# fader 0.2 - 02/02/07 - Dave Gallagher
#   Auto add 1sec fade-in/fade-out to avi clips
#
tmpdir="/home1/tmp/"
pictype=png
bitrate="vbitrate=4000"

if [ -d $tmpdir ]
then
    echo tmp dir found
else
    echo creating tmpdir $tmpdir;
    mkdir $tmpdir
fi
if [ $# -ne 1 ]
then
        echo "Usage: `basename $0` /path/to/videoclip" ; exit 1
fi
if [ -f "$1" ]
then
        echo "Clip to process: $1";
        clip="$1";
        echo "Dest file:       $1.fade";
        outfile=$1.fade
else
        echo "File $1 not found" ;  exit 1
fi
if [ -f *.jpg ]
then
        echo "Delete frames in $tmpdir first" ; exit 1
fi

frames=`tcprobe -i "${clip}" | grep V: | sed -e 's/\[avi.*es=//' -e 's/,.*//'`
vbitrate=`mplayer -identify -frames 0 "${clip}" | grep ID_VIDEO_BITRATE | 
sed -e 's/.*RATE=//'`
vbitrate=$(($vbitrate / 1000))
echo Frames="${frames}"
echo Video Bitrate="${vbitrate}kbps"
echo "------------------------------"
echo " Extracting audio"
echo "------------------------------"
mplayer -msglevel all=-1 -benchmark -vc null -vo null -dumpaudio -dumpfile 
$tmpdir/zaudio.mp3 "${clip}"
echo "-------------------------------------"
echo " Calculating audio fade positions"
echo "-------------------------------------"
aud_time=`sox $tmpdir/zaudio.mp3 2>&1 -e stat | grep Length | /
      cut -d : -f 2 | cut -f 1 | sed 's/^[ \t]*//'`
echo Audio size in seconds: $aud_time
aud_frames=`sox $tmpdir/zaudio.mp3 2>&1 -e stat | grep Samples | /
      cut -d : -f 2 | cut -f 1 | sed 's/^[ \t]*//'`
echo Audio size in frames:  $aud_frames
frames_per_second=`perl -e "print ($aud_frames / $aud_time)"`
int_time=`perl -e "print int($aud_time)"`
frac_time=`perl -e "print int(($aud_time - int($aud_time)) * 
$frames_per_second)"`
stop_time=${int_time}.${frac_time}
echo "-------------------------------------"
echo " Processing audio fades"
echo "-------------------------------------"
sox $tmpdir/zaudio.mp3 $tmpdir/zaudio1.mp3 fade q 2 $stop_time 1
echo "---------------------------------------"
echo " extract frames"
echo "---------------------------------------"
if [ $pictype = jpg ] ; then
  echo Doing JPeG ; ptype="jpeg:quality=100"
else
  echo Doing PNG  ; ptype="png:z=0"
fi
#dirty hack because mplayers png output only goes to "current" dir
curr=`pwd`
cd $tmpdir
mplayer -msglevel all=-1 -benchmark -nosound -vo "${ptype}" "$curr/${clip}"
cd $curr
echo "-------------------------------------"
echo " processing video fades at both ends"
echo "-------------------------------------"
count=1
while [ $count -le 25 ] ; do
  sframe=`echo $count | awk '{printf("%8d",$0)}' | sed 's/ /0/g'`.${pictype}
  fadein=$(($count * 4))
#Might as well count backwards from the end too and fade out
  end=$(($frames - $count))
  eframe=`echo $end | awk '{printf("%8d",$0)}' | sed 's/ /0/g'`.${pictype}
  echo $sframe $eframe $fadein
  mogrify $tmpdir/$sframe -modulate $fadein $tmpdir/$sframe
  mogrify $tmpdir/$eframe -modulate $fadein $tmpdir/$eframe
  count=$(($count + 1))
done
echo "-------------------------------"
echo " Re-encoding video to mp4"
echo "-------------------------------"
# VERY HIGH QUALITY
video="-ovc lavc -lavcopts vcodec=mpeg4:mbd=2:mv0:trell:v4mv:cbp:/
last_pred=3: predia=2:dia=2:vmax_b_frames=2:vb_strategy=1:/
precmp=2:cmp=2:subcmp=2:preme=2:qns=2"
# VERY FAST
video="-ovc lavc -lavcopts vcodec=mpeg4:mbd=2"
for j in "turbo:vpass=1 -nosound" "vpass=2 ${audio}" ; do
  mencoder -oac copy -audiofile $tmpdir/zaudio1.mp3 /
  -mf fps=25:type=$pictype "mf://$tmpdir/*.$pictype" /
  ${video}:${bitrate}:${j} -ffourcc DX50 -o $outfile
done
if [ $pictype = jpg ] ; then
  find $tmpdir -name "*.jpg" -print0 | xargs -0 rm
else
  find $tmpdir -name "*.png" -print0 | xargs -0 rm
fi
rm $tmpdir/zaudio*.mp3 divx2pass.log


-- 
Dave



More information about the MPlayer-users mailing list