[bglug] Rototom. A video rotoscoper.

k|b|s kbs@bglug.it
Ven 20 Nov 2009 12:10:29 CET


Dovevo rotoscopizzare alcune foto, pertanto sono ricorso ad autotrace ed
imagemagick. Quindi sono passato ai filmati...

Vi propongo tre output:
-f (fill) -> la classica immagine cartoon-like
-b (borderize) -> ricordate il video degli a-ha?
-o (obamize) -> shepard fairey clone

Sample output: http://strop.ath.cx/vids/lisa_ann_rotom.avi

Enjoy rototom (x jimmy: il prossimo anno non si sa se ci sarà...):

BTW, se il filmato è lungo, aspettatevi tempi lunghi ;)

--8<-- CUT HERE --8<-- 
#!/bin/bash
# Time-stamp: <rototom, 20/11/2009 10:58:20 joe Exp>
# rototom, a video rotoscoper
# by Joe Galaxy <joegalaxy at gmail dot com>
# License: gpl2+
# Require: mplayer/mencoder, imagemagick, autotrace, (lame)

#############################
# Some configuration:
# despeckle level 1<->20
dl="15"
# light
luce="15"
# Compress audio (mp3) yes/no
acompress="yes"
# Clean tmp dir yes/no
clean="yes"
#############################

tmp="/tmp/rototom.$$"
if [ ! -d $tmp ]; then
mkdir -p $tmp
fi

#### go!
cd $tmp
vom="mplayer -vo jpeg -ao null -nosound "$2""
aom="mplayer -vo null -ao pcm "$2""
mp3="lame -h audiodump.wav audio.mp3"


trace()
{
for i in *.jpg ; do
autotrace --input-format JPG -output-file="$i.svg" --output-format=svg --despeckle-level="$dl" --corner-threshold=3 "$i"
done
}

obamize()
{
for i in *.jpg ; do
convert "$i" \( -clone 0 -colorspace gray \
-normalize -gaussian-blur 1x2 \) -colorspace RGB -delete 0 -insert 0 \
\( -size 10x2 xc:'rgb(45,60,80)' xc:'rgb(218,52,30)' xc:'rgb(150,175,175)' \
-size "$luce"x2 \
xc:'rgb(239,234,210)' +append \) -clut  "$i-ob.jpg"
done
}

obtrace()
{
for i in *-ob.jpg ; do
autotrace --color-count=4 -output-file="$i.svg" --output-format=svg --despeckle-level="$dl" --corner-threshold=3 "$i"
done
}

borderize()
{
for i in *.jpg ; do
awk '{sub(/stroke:none;/,"fill-opacity:0.3; stroke:#000000; stroke-opacity:1; stroke-width:2;")}; 1' $i.svg > $i.jpg.svg
done
}

imconvert()
{
for in in *.jpg.svg ; do
convert "$in" "$in.jpg"
done
}

aufile="audiodump.wav"
if [ "$acompress" = "yes" ] ; then
$mp3
$aufile="audio.mp3"
else
$aufile="audiodump.wav"
fi

vcr="mencoder "mf://*.jpg.svg.jpg" -ovc lavc -lavcopts vcodec=msmpeg4v2 -oac copy -audiofile "$aufile" -o "${2%.*}-rotomized.avi""

# Colors
black='\E[0;0;30m'
blue='\E[0;0;34m'
green='\E[0;0;32m'
cyan='\E[0;0;36m'
red='\E[0;0;31m'
purple='\E[0;0;35m'
brown='\E[0;0;33m'
lightgray='\E[0;0;37m'
darkgray='\E[0;1;30m'
lightblue='\E[0;1;34m'
lightgreen='\E[0;1;32m'
lightcyan='\E[0;1;36m'
lightred='\E[0;1;31m'
lightpurple='\E[0;1;35m'
yellow='\E[0;1;33m'
white='\E[0;1;37m'
Reset="tput sgr0"

cecho ()
{
  message=${1:-}
  color=${2:-}
  echo -e "$color""$message"
  $Reset
  return
}

if [ ! $# -eq 2 ] ; then
cecho "Usage:" $yellow
cecho "  rototom b|f|o /full/path/to/movie" $yellow
cecho "   -b borderize" $yellow
cecho "   -f fill colors" $yellow
cecho "   -o obamize\n" $yellow
exit 1
fi

## start!

cecho "\n**\n**  Extracting video...\n**\n" $yellow
$vom

cecho "\n**\n**  Extracting audio...\n**\n" $yellow
$aom

cecho "\n**\n**  Tracing...\n**\n" $yellow

case $1 in
b|-b)
trace
borderize
;;
f|-f)
trace
;;
o|-o)
obamize
obtrace
;;
esac

cecho "\n**\n**  Converting...\n**\n" $yellow
imconvert

cecho "\n**\n**  Creating video...\n**\n" $yellow
if [ "$acompress" = "yes" ] ; then
$mp3
$aufile="audio.mp3"
else
$aufile="audiodump.wav"
fi
$vcr

if [ "$clean" = "yes" ] ; then
rm -rf $tmp
fi

cecho "\n**\n**  Done.\n**\n" $yellow
cecho "\n**\n**  Your file is "${2%.*}-out.avi"\n**\n" $lightred
--8<-- CUT HERE --8<-- 

-- 
perl -e '$_="m2jxmoaye.og\@iaqglcla";$_.=$1,print$2while s/(..)(.)//;'


Maggiori informazioni sulla lista bglug