ENH: improve flexiblity of foamCreateVideo (issue #904)
- respect trailing '-', '.', '_' for the image prefix name. For example, -image press_ to accept press_*.png - additional -mask width option (for avconv)
This commit is contained in:
parent
d9568a4b56
commit
4300f4c02f
@ -4,7 +4,7 @@
|
|||||||
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
# \\ / O peration |
|
# \\ / O peration |
|
||||||
# \\ / A nd | Copyright (C) 2015-2016 OpenFOAM Foundation
|
# \\ / A nd | Copyright (C) 2015-2016 OpenFOAM Foundation
|
||||||
# \\/ M anipulation |
|
# \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# License
|
# License
|
||||||
# This file is part of OpenFOAM.
|
# This file is part of OpenFOAM.
|
||||||
@ -31,6 +31,20 @@
|
|||||||
#
|
#
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Input defaults
|
||||||
|
dirName='.'
|
||||||
|
prefix='image'
|
||||||
|
inputMask='%04d' # (avconv only)
|
||||||
|
unset startNumber # (avconv only)
|
||||||
|
|
||||||
|
# Output defaults
|
||||||
|
outputPrefix=video
|
||||||
|
outputFormat=mp4
|
||||||
|
frameRate=10
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
usage () {
|
usage () {
|
||||||
exec 1>&2
|
exec 1>&2
|
||||||
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
|
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
|
||||||
@ -38,115 +52,163 @@ usage () {
|
|||||||
|
|
||||||
Usage: ${0##*/} [OPTIONS] ...
|
Usage: ${0##*/} [OPTIONS] ...
|
||||||
options:
|
options:
|
||||||
-d | -dir <dir> directory containing png images (default local dir)
|
-d | -dir <dir> input directory with png images (default: '.')
|
||||||
-f | -fps <fps> frames per second (default = 10)
|
-f | -fps <fps> frames per second (default = 10)
|
||||||
|
-i | -image <name> prefix for input image sequence (default: 'image')
|
||||||
|
-o | -out <name> name of output video file (default: 'video')
|
||||||
|
-start <frame> start frame number (avconv only)
|
||||||
|
-webm WebM output video file format (avconv only)
|
||||||
|
-mask <width> input mask width (avconv only, default: 4)
|
||||||
-h | -help help
|
-h | -help help
|
||||||
-i | -image <name> name of image sequence (default = image)
|
|
||||||
-o | -out <name> name of output video file (default = video)
|
|
||||||
-s | -start <frame> specify the start frame number for avconv
|
|
||||||
-w | -webm WebM output video file format
|
|
||||||
|
|
||||||
Creates a video file from a sequence of PNG images
|
Creates a video file from a sequence of PNG images.
|
||||||
- A sequence named "image" will expect files image.0000.png, image.0001.png, etc
|
For example, image.0000.png, image.0001.png, ...
|
||||||
- The default output video compression format is MPEG-4, with WebM as an option
|
|
||||||
- The default file name, using MPEG-4 compression, is video.mp4
|
- Can use -i/-image to specify other values. Eg, -i "pressure_" ...
|
||||||
|
- The output format is MPEG-4
|
||||||
|
- The output name (with mp4 format), is "video.mp4"
|
||||||
- By default the video codec is high resolution
|
- By default the video codec is high resolution
|
||||||
|
|
||||||
Requires avconv or mencoder for MPEG-4 output, avconv for WebM output
|
MPEG-4 output requires avconv or mencoder.
|
||||||
|
WebM output requires avconv.
|
||||||
|
|
||||||
USAGE
|
USAGE
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
# Default settings
|
|
||||||
DIR='.'
|
|
||||||
IMAGE='image'
|
|
||||||
VIDEO='video'
|
|
||||||
FPS=10
|
|
||||||
FMT='mp4'
|
|
||||||
START_NUMBER=''
|
|
||||||
|
|
||||||
|
# Parse options
|
||||||
|
unset optDebug optEnvName optStrip optVerbose
|
||||||
while [ "$#" -gt 0 ]
|
while [ "$#" -gt 0 ]
|
||||||
do
|
do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-h | -help*)
|
-h | -help*)
|
||||||
usage
|
usage
|
||||||
;;
|
;;
|
||||||
-d | -directory)
|
-d | -dir)
|
||||||
[ "$#" -ge 2 ] || usage "'$1' option requires an argument"
|
[ "$#" -ge 2 ] || die "'-dir' requires an argument"
|
||||||
DIR=$2
|
dirName=$2
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
-f | -fps)
|
|
||||||
[ "$#" -ge 2 ] || usage "'$1' option requires an argument"
|
|
||||||
FPS=$2
|
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
-i | -image)
|
|
||||||
[ "$#" -ge 2 ] || usage "'$1' option requires an argument"
|
|
||||||
IMAGE=$2
|
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
-o | -out)
|
|
||||||
[ "$#" -ge 2 ] || usage "'$1' option requires an argument"
|
|
||||||
VIDEO=$2
|
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
-s | -start)
|
|
||||||
[ "$#" -ge 2 ] || usage "'$1' option requires an argument"
|
|
||||||
START_NUMBER="-start_number $2"
|
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
-w | -webm)
|
|
||||||
FMT=webm
|
|
||||||
echo "Selected $FMT format, requires avconv..."
|
|
||||||
shift
|
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 "'-out' 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."
|
||||||
|
;;
|
||||||
-*)
|
-*)
|
||||||
usage "invalid option '$1'"
|
die "invalid option '$1'"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
#
|
#------------------------------------------------------------------------------
|
||||||
# MAIN
|
|
||||||
#
|
|
||||||
|
|
||||||
[ -f "$(ls -1 $DIR/$IMAGE.*.png | head -1)" ] || usage "Cannot find first file in image sequence"
|
# Add trailing '.' to the prefix if it does not already end with [-._]
|
||||||
|
case "$prefix" in
|
||||||
|
*[-_.])
|
||||||
|
: # OK, use prefix as it is
|
||||||
|
;;
|
||||||
|
(*)
|
||||||
|
prefix="$prefix."
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
if [ "$FMT" = "webm" ] ; then
|
# See how many files exist
|
||||||
if command -v avconv >/dev/null 2>&1 ; then
|
nFiles="$(\ls $dirName/$prefix*.png 2>/dev/null | wc -l)"
|
||||||
echo "Creating image with avconv..."
|
|
||||||
|
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..."
|
||||||
avconv \
|
avconv \
|
||||||
-framerate $FPS \
|
-framerate $frameRate $startNumber \
|
||||||
$START_NUMBER \
|
-i "${dirName}/${prefix}$inputMask.png" \
|
||||||
-i ${DIR}/${IMAGE}.%04d.png \
|
|
||||||
-c:v libvpx -crf 15 -b:v 1M \
|
-c:v libvpx -crf 15 -b:v 1M \
|
||||||
$VIDEO.$FMT
|
"$outputPrefix.$outputFormat"
|
||||||
else
|
else
|
||||||
usage "Please install avconv"
|
die "webm format requires avconv, which was not found."
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
if command -v avconv >/dev/null 2>&1 ; then
|
if command -v avconv >/dev/null 2>&1
|
||||||
echo "Creating image with avconv..."
|
then
|
||||||
|
echo "Creating video with avconv..."
|
||||||
avconv \
|
avconv \
|
||||||
-framerate $FPS \
|
-framerate $frameRate $startNumber \
|
||||||
$START_NUMBER \
|
-i "${dirName}/${prefix}$inputMask.png" \
|
||||||
-i ${DIR}/${IMAGE}.%04d.png \
|
|
||||||
-c:v libx264 -pix_fmt yuv420p \
|
-c:v libx264 -pix_fmt yuv420p \
|
||||||
$VIDEO.$FMT
|
"$outputPrefix.$outputFormat"
|
||||||
elif command -v mencoder >/dev/null 2>&1 ; then
|
|
||||||
echo "Creating image with mencoder..."
|
elif command -v mencoder >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
echo "Creating video with mencoder..."
|
||||||
mencoder \
|
mencoder \
|
||||||
"mf://$DIR/$IMAGE.*.png" \
|
"mf://$dirName/$prefix*.png" \
|
||||||
-mf fps=$FPS \
|
-mf fps=$frameRate \
|
||||||
-o $VIDEO.$FMT \
|
-o "$outputPrefix.$outputFormat" \
|
||||||
-ovc x264
|
-ovc x264
|
||||||
else
|
else
|
||||||
usage "Please install avconv or mencoder"
|
die "Did not find avconv or mencoder. Cannot create video."
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user