Linux – Make Stop Motion Animations

This script (which I wrote) assumes that you have sequential files named IMG_XXXX.JPG, e.g. IMG_0301.JPG, IMG_0302.JPG, etc. (Gqview and Krename are good for batch renaming your files.) You can specify the range of frames to animate, the size that the animation should be, and the frame rate. Post any useful hacks back here, please.

 #!/bin/bash

if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || \
[ -z $5 ] || [ -z $6 ]
then
        echo "animate name low-range high-range new-width new-height framerate"
        exit 1
else
        NAME=$1
        RANGELOW=$2
        RANGEHIGH=$3
fi

mkdir $NAME

for i in `seq $RANGELOW $RANGEHIGH`
do
        if [ $i -lt 1000 ]; then
                if [ $i -lt 100 ]; then
                        if [ $i -lt 10 ]; then
                                FILENUM="000$i"
                        else
                                FILENUM="00$i"
                        fi
                else
                        FILENUM="0$i"
                fi
        else
                FILENUM="$i"
        fi

        FILENAME="IMG_$FILENUM.JPG"

        if [ -e "$FILENAME" ]; then
                echo "Copying $FILENAME"
                cp $FILENAME $NAME
        fi
done

cd $NAME

for COPYFILE in *.JPG
do
        echo "Resizing $COPYFILE"
        convert -resize $4x$5 $COPYFILE $COPYFILE
done

NUMFRAMES=`expr $RANGEHIGH - $RANGELOW`

jpeg2yuv -f $6 -b $RANGELOW -n $NUMFRAMES -I p -j IMG_%04d.JPG | \
yuvfps -r 25:1 | yuv2lav -o $NAME.avi

FOLDER=`pwd`
echo "Made $FOLDER/$NAME.avi"
rm *.JPG

You will need to install the MJpeg Tools if you wish to run it.