May 29, 2008

Automatic Rotoscoping




#!/bin/bash
# rotoscope - auto rotoscope a video file
# with openSource Software on Linux
#
# needs mplayer, autotrace and ImageMagick
#
# usage: rotoscope videofile
# -- will create videofile.rotoscoped.avi
#
# created by Honza Svasek : HonzaSvasek.nl
#

export TMPDIR=/tmp/rotoscope$$
export FILM=$1
mkdir $TMPDIR
cd $TMPDIR
mplayer $FILM -nosound -vo jpeg
for FILE in *.jpg
do
echo $FILE
autotrace $FILE -output-format svg \
-color-count 10 \
-despeckle-level 20 \
-filter-iterations 10 > ${FILE}.svg
rm $FILE
convert ${FILE}.svg $FILE
done
mencoder 'mf://*jpg' -ovc lavc \
-o $FILM.rotoscoped.avi
cd ..
rm -f $TMPDIR
mplayer
$FILM.rotoscoped.avi
Share/Save/Bookmark

4 comments:

Mike said...

This looks awesome. Would you be able to write up some type of short tutorial. I'm going to download a linux distro I can work on my pc.

could you recommend one?

finitelife said...

This is awesome! I was very impressed by the original script, but I felt it could use a few 'tweaks'. Here is the modified script I am currently working with;


#!/bin/bash
# rotoscope - auto rotoscope a video file with openSource Software on Linux
# needs mplayer(ffmpeg), autotrace and ImageMagick
# usage: rotoscope videofile //will create videofile.rotoscoped.avi
# created by Honza Svasek : HonzaSvasek.nl
# edited by Joseph Riopelle finitelife[at]hotmail.com

export TMPDIR=/tmp/rotoscope$$
export FILM=$1
export CWDIR=$PWD
mkdir $TMPDIR; cd $TMPDIR
#mplayer $CWDIR/$FILM -nosound -vo jpeg
ffmpeg -an -sameq -i $CWDIR/$FILM %09d.jpg
export FILES=`ls -l *.jpg|wc -l`
for FILE in *.jpg; do
export COUNT=$(($COUNT+1))
echo -n -e "\r$((($COUNT*100)/$FILES))% done tracing [$FILE]..."
autotrace $FILE -output-format svg -color-count 100 -despeckle-level 20 -filter-iterations 5 -output-file ${FILE}.svg
#-color-count 10 -despeckle-level 20 -filter-iterations 10
rm -f $FILE
convert -quality 100 ${FILE}.svg $FILE
rm -f ${FILE}.svg
done
echo -e "\rDone! Successfully traced $FILES files."
#mencoder 'mf://*.jpg' -ovc lavc -o $CWDIR/$FILM.rotoscoped.avi
#-mf fps=10 -lavcopts vcodec=msmpeg4v2:vbitrate=800
ffmpeg -an -sameq -b 1800 -i %09d.jpg $CWDIR/$FILM.rotoscope.mp4
cd $CWDIR; rm -rf $TMPDIR
echo "Rotoscoped file created: $CWDIR/$FILM.rotoscoped.avi"
unset CWDIR TMPDIR FILM COUNT FILES


Please feel free to contact me regarding future improvements to this script.

MArco said...

Thanks for share!

Juris said...

It's unique script... couldnt imagine that this king of efx can be done with shell script 8)
PS. im using it right now xDD