Skip to content
Snippets Groups Projects
foamCreateVideo 5.61 KiB
Newer Older
#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 2015-2016 OpenFOAM Foundation
#    \\/     M anipulation  | Copyright (C) 2018 OpenCFD Ltd.
#-------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     foamCreateVideo
#
# Description
#     Creates a video file from PNG images
#     - requires avconv or mencoder
#
#------------------------------------------------------------------------------

# Input defaults
dirName='.'
inputMask='%04d'    # (avconv only)
unset startNumber   # (avconv only)

# Output defaults
outputPrefix=video
outputFormat=mp4
frameRate=10


#------------------------------------------------------------------------------

usage () {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat <<USAGE

Usage: ${0##*/} [OPTIONS] ...
options:
  -d | -dir <dir>       input directory with png images  (default: '.')
  -f | -fps <fps>       frames per second  (default: 10)
  -i | -image <name>    input image sequence prefix  (default: 'image.')
  -o | -out <name>      output video name  (default: 'video')
  -mask <width>         input mask width for avconv  (default: 4)
  -start <frame>        start frame number for avconv
  -webm                 WebM output video file format (avconv only)
  -h | -help            help

Creates a video file from a sequence of PNG images.
With the default prefix ('image.'), from image.0000.png, image.0001.png, ...
- The output format is MPEG4
- The output name (with mp4 format), is "video.mp4"
- By default the video codec is high resolution
MPEG4 output requires avconv or mencoder.
WebM  output requires avconv.
die()
{
    exec 1>&2
    echo
    echo "Error encountered:"
    while [ "$#" -ge 1 ]; do echo "    $1"; shift; done
    echo
    echo "See '${0##*/} -help' for usage"
    echo
    exit 1
}
# Parse options
unset optDebug optEnvName optStrip optVerbose
    case "$1" in
    -h | -help*)
        usage
        ;;
    -d | -dir)
        [ "$#" -ge 2 ] || die "'-dir' requires an argument"
        dirName=$2
        shift
       ;;
    -f | -fps)
        [ "$#" -ge 2 ] || die "'-fps' requires an argument"
        frameRate=$2
        shift
        ;;
    -i | -image)
        [ "$#" -ge 2 ] || die "'-image' requires an argument"
        prefix=$2
        shift
        ;;
    -o | -out)
        [ "$#" -ge 2 ] || die "'-out' requires an argument"
        outputPrefix=$2
        shift
        ;;
    -mask)
        [ "$#" -ge 2 ] || die "'-mask' requires an argument"
        digits="$(( $2 + 0 ))"
        if [ "$digits" -gt 0 ]
        then
            inputMask="%0${digits}d"
            echo "using input mask $inputMask"
        else
            echo "input mask unchanged $inputMask"
        fi
        shift
        ;;
    -start)
        [ "$#" -ge 2 ] || die "'-start' requires an argument"
        startNumber="-start_number $2"
        shift
        ;;
    -webm)
        # webm - needs avconv
        outputFormat=webm
        command -v avconv >/dev/null 2>&1 || \
            die "webm format requires avconv, which was not found."
        ;;
    -*)
        die "invalid option '$1'"
        ;;
    *)
        break
        ;;
#------------------------------------------------------------------------------

# See how many files exist
nFiles="$(\ls $dirName/$prefix*.png 2>/dev/null | wc -l)"
echo "=============="
echo "Output file:   $outputPrefix.$outputFormat"
echo "Input files:   $prefix*.png"
echo "Detected:      $nFiles files"
echo "=============="
echo
[ "$nFiles" -gt 0 ] || die "No input files found"
# Do the conversion

if [ "$outputFormat" = webm ]
then
    if command -v avconv >/dev/null 2>&1
    then
        echo "Creating video with avconv..."
            -framerate $frameRate $startNumber \
            -i "$dirName/$prefix$inputMask.png" \
            "$outputPrefix.$outputFormat"
        die "webm format requires avconv, which was not found."
    if command -v avconv >/dev/null 2>&1
    then
        echo "Creating video with avconv..."
            -framerate $frameRate $startNumber \
            -i "$dirName/$prefix$inputMask.png" \
            "$outputPrefix.$outputFormat"

    elif command -v mencoder >/dev/null 2>&1
    then
        echo "Creating video with mencoder..."
            "mf://$dirName/$prefix*.png" \
            -mf fps=$frameRate \
            -o "$outputPrefix.$outputFormat" \
        die "Did not find avconv or mencoder. Cannot create video."

#------------------------------------------------------------------------------