transition.sh

Download transtion.sh here


#!/bin/sh
#
# Ian Portsmouth (ian at psys.co.uk)
# 24 June 2003
#
# combine two video clips with a smooth transition
#
# Usage:
# transition.sh [-d] infile1.avi infile2.avi outfile.avi
#
# -d   pass through yuvdenoise filter
#
# Output file is Motion JPEG (mjpeg) format. 
# Change the mencoder line to use mpeg4 instead to 
# generate a DivX format video.
#
######################################################################
#
# Warning: If this script works for you, then great. If not, 
# and everything comes crashing down about your ears, you're 
# on your own.
#
######################################################################
#set -x

if [ $# -lt 3 ] ; then
	echo "Usage:"
	echo "$0 [-d]   "
	echo "Flags:"
	echo "-d: Use yuvdenoise"
	echo ""
	exit
fi

tmpdir=/tmp

denoise=0
if [ "$1" == "-d" ]; then
	denoise=1
	shift
fi

file1=$1 # first file
file2=$2 # second file
ofile=$3 # output filename
tlen1=20 # transition length in frames

trans=$tmpdir/trans.avi
avi1=$tmpdir/1.avi        # trimmed file1
avi2=$tmpdir/2.avi        # trimmed file2
wav1=$tmpdir/1.wav        # audio from file1
wav2=$tmpdir/2.wav        # audio from file2
raw1=$tmpdir/1.raw        # faded audio file1
raw2=$tmpdir/2.raw        # trimmed faded audio file2
raw3=$tmpdir/3.raw        # combined audio raw format
wav3=$tmpdir/3.wav        # combined audio inc fade
com1=$tmpdir/combined.avi # combined video with transition (no audio)

# get number of frames from first clip
nframes1=`lavinfo +p $file1 | grep video_frames | cut -d = -f 2`
lensec1=`dc -e "2 k $nframes1 20.0 / p"`

# length of first clip, minus transition frames
tlen2=`expr $nframes1 - $tlen1`

# save off main segments of video
# -f is number of frames to pass
# -o is offset, ie start frame to pass
# yuvdenoise will enhance the video particularly
# in poor light conditions
if [ $denoise == 1 ]; then
	lav2yuv +p -f $tlen2 $file1 | \
		yuvdenoise | \
		yuv2lav -f a -q 80 -o $avi1
	
	lav2yuv +p -o $tlen1 $file2 | \
		yuvdenoise | \
		yuv2lav -f a -q 80 -o $avi2
else
	lav2yuv +p -f $tlen2 $file1 | \
		yuv2lav -f a -q 80 -o $avi1
	
	lav2yuv +p -o $tlen1 $file2 | \
		yuv2lav -f a -q 80 -o $avi2
fi

# save off audio
lav2wav -I +p $file1 > $wav1
lav2wav -I +p $file2 > $wav2
# fade last 0.25 sec of 1st clip
sox $wav1 $raw1 fade 0 $lensec1 0.25
# trim off first second of second clip, and fade
sox $wav2 $raw2 trim 1.0 fade 0.25
cat $raw1 $raw2 > $raw3
sox -u -b -r 11024 $raw3 $wav3

# create transition
ypipe "lav2yuv +p -o -$tlen1 $file1" "lav2yuv +p -f $tlen1 $file2" | \
	transist.flt -o 0 -O 255 -d $tlen1 | yuvdenoise | \
	yuv2lav -f a -q 80 -o $trans

# join video together
lavtrans +p -o $com1 $avi1 $trans $avi2

# merge audio into video and output to MJPEG
mencoder $com1 -oac copy -audiofile $wav3 -ovc lavc -lavcopts vcodec=mjpeg -o $3

# clean up
/bin/rm -f $trans
/bin/rm -f $avi1
/bin/rm -f $avi2
/bin/rm -f $wav1
/bin/rm -f $wav2
/bin/rm -f $wav3
/bin/rm -f $raw1
/bin/rm -f $raw2
/bin/rm -f $raw3
/bin/rm -f $com1