A bash function to make movies

I use ffmpeg often to make and modify movies. But when creating movies from image sequences, ffmpeg requires the movies to be sequentially numbered. Some time ago, I wrote a bash function that create temporary symbolic links to the image files from which ffmpeg can create a movie. You can find the function below, and an example call is

movie output.mp4 15 DD*_density*.png

that produces a movie output.mp4 at 15 frames/sec from the files included in the wildcard.

function movie() {
    if [ $# -lt 3 ]; then
        echo "usage: movie output_movie FPS files"
        kill -INT $$
    fi
    tmpdir=`mktemp -d`
    output=$1
    fps=$2
    shift 2
    files="$@"
    count=0
    suffix=`echo $1 | cut -f2 -d.`
    width=`identify -format "%w" $1`
    height=`identify -format "%h" $1`
    # Make sure that the width is even (yuv420p format for QT)
    width=`expr $width \/ 2 \* 2`
    cwd=`pwd`
    for f in $files; do
        fn=`echo $count $suffix | gawk '{printf "img%06d.%s", $1, $2}'`
        ln -s $cwd/$f ${tmpdir}/${fn}
        count=`expr $count + 1`
    done
    ffmpeg -r $fps -f image2 -i ${tmpdir}/img%06d.${suffix} -vcodec libx264 -pix_fmt yuv420p \
           -s ${width}x${height} -crf 25 -y $output
    rm -r $tmpdir
}

 

One thought on “A bash function to make movies”

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*