File: foamCreateVideo

package info (click to toggle)
openfoam 1912.200626-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 238,956 kB
  • sloc: cpp: 1,159,641; sh: 15,902; ansic: 5,195; lex: 660; xml: 387; python: 282; awk: 212; makefile: 103; sed: 88; csh: 3
file content (205 lines) | stat: -rwxr-xr-x 5,834 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2015-2016 OpenFOAM Foundation
#     Copyright (C) 2018-2019 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='.'
prefix='image.'
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.

USAGE
    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
}


# Parse options
unset optDebug optEnvName optStrip optVerbose
while [ "$#" -gt 0 ]
do
    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 || \
            die "webm format requires avconv, which was not found."
        ;;
    -*)
        die "invalid option '$1'"
        ;;
    *)
        break
        ;;
    esac
    shift
done

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

# 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
    then
        echo "Creating video with avconv..."
        avconv \
            -framerate $frameRate $startNumber \
            -i "$dirName/$prefix$inputMask.png" \
            -c:v libvpx -crf 15 -b:v 1M \
            "$outputPrefix.$outputFormat"
    else
        die "webm format requires avconv, which was not found."
    fi
else
    if command -v avconv >/dev/null
    then
        echo "Creating video with avconv..."
        avconv \
            -framerate $frameRate $startNumber \
            -i "$dirName/$prefix$inputMask.png" \
            -c:v libx264 -pix_fmt yuv420p \
            "$outputPrefix.$outputFormat"

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

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