-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkmov
executable file
·237 lines (214 loc) · 7.14 KB
/
mkmov
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
#
if [ -n "$DEBUG" ]; then
set -x
fi
renice -n 10 $$ > /dev/null # reduce priority
START_TIME=$SECONDS
# defaults
WIDTH=1920
BITRATE=500k
QUALITY=23
TWOPASS=0
FRAMERATE=30
PRESET=${PRESET:-medium}
LOG_LEVEL=${LOG_LEVEL:-error}
VERBOSE=${VERBOSE:-0}
CODEC=x264
THREADS=4
function help {
cat <<- EOH
help is nigh
mkmov - make a movie from different sources
options:
-c CODEC (default: $CODEC) [x264, x265, hevc, vp9, iphone]
-i INPUT if empty then we'll interpret turn a directory of files into a movie
-w WIDTH (default: $WIDTH), maintains aspect ratio
-v LOG_LEVEL (default: $LOG_LEVEL) passthru to ffmpeg
-q QUALITY (default: $QUALITY) passthru for CRF to ffmpeg
-b BITRATE (default: $BITRATE) passthru for ffmpeg
-r FRAMERATE (default: $FRAMERATE) passthru for ffmpeg
-2 enables two-pass mode
-p PRESET (default:$PRESET) only used for x264
EOH
exit
}
while getopts c:w:hvq:b:2r:p:i: opt; do
case $opt in
c)
CODEC=$OPTARG
;;
i)
INPUT=$OPTARG
;;
w)
WIDTH=$OPTARG
;;
h)
help
;;
v)
LOG_LEVEL=info
VERBOSE=1
;;
q)
QUALITY=$OPTARG
;;
b)
BITRATE=$OPTARG
;;
2)
TWOPASS=1
;;
r)
FRAMERATE=$OPTARG
;;
p)
echo possible values: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow
PRESET=$OPTARG
;;
*)
help
;;
esac
done
if [ "$#" -eq 0 ]; then
help
fi
if [ "$CODEC" != "iphone" ]; then
if [ -f tl000001.jpg ]; then
FDATE=$(stat -c"%y" tl000001.jpg 2> /dev/null |cut -b1-10)
DATUM=$(stat -c"%y" tl000001.jpg 2> /dev/null |cut -b1-10|perl -npe "s/(\d+)\-(\d+)\-(\d+)/\3.\2.\1/")
else
echo "couldn't find tl000001.jpg"
echo "wrong directory?"
exit
fi
fi
if [ "$CODEC" == "iphone" ]; then
FDATE=$(stat -c"%y" $INPUT|cut -b1-10)
DATUM=$(stat -c"%y" $INPUT|cut -b1-10|perl -npe "s/(\d+)\-(\d+)\-(\d+)/\3.\2.\1/")
# echo $FDATE
# echo $DATUM
fi
if [ "$CODEC" != "x264" ] &&
[ "$CODEC" != "x265" ] &&
[ "$CODEC" != "hevc" ] &&
[ "$CODEC" != "iphone" ] && # not really a codec though
[ "$CODEC" != "vp9" ]
then
echo unknown codec
echo valid parameters are x264, x265, iphone, hevc, vp9
exit
fi
# shift
if [ "$VERBOSE" == "1" ]; then
# log effective parameters
echo input file : $INPUT
echo width : $WIDTH
echo log level : $LOG_LEVEL
echo quality for CRF: $QUALITY
echo bitrate : $BITRATE
echo framerate : $FRAMERATE fps
echo preset : $PRESET
echo two pass : $TWOPASS
fi
# movie sizing independent from picture size
SIZING="-vf scale=$WIDTH:-1"
OPT="-y -v $LOG_LEVEL -hide_banner"
MOVIE=$FDATE-$CODEC-q$QUALITY-b$BITRATE-w$WIDTH-fr$FRAMERATE
MAUTHOR="Steffen Roller <[email protected]>"
MYEAR="(c) by Steffen Roller $(date +%Y)"
MCOPY="Creative Commons License (BY)"
MDESC="codec:$CODEC:quality $QUALITY:width $WIDTH:$FRAMERATE fps"
MTITLE="$DATUM, Kitchener, ON"
METADATA="-metadata title='$MTITLE'"
case $CODEC in
iphone)
PRESET=veryslow
# MOVIE=$(basename $INPUT .MTS)-iphone.mp4
MOVIE=$(echo "$INPUT" | cut -f 1 -d '.')-iphone.mp4
ffmpeg $OPT -i $INPUT \
-c:a copy \
-preset $PRESET -c:v libx264 \
-x264-params ref=4 \
-movflags +faststart \
-vf "scale=-2:720:flags=lanczos" \
-crf $QUALITY $MOVIE
;;
x264)
if [ "$TWOPASS" = "1" ]; then
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg \
-an -preset $PRESET -c:v libx264 \
$SIZING \
-metadata title="$MTITLE" -metadata author="$MAUTHOR" -metadata year="$MYEAR" -metadata copyright="$MCOPY" -metadata description="$MDESC"\
-b:v $BITRATE -pass 1 -r $FRAMERATE -f mp4 /dev/null
MOVIE=$MOVIE-$PRESET-2pass.mp4
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg \
-an -preset $PRESET -c:v libx264 \
$SIZING \
-movflags +faststart \
-metadata title="$MTITLE" -metadata author="$MAUTHOR" -metadata year="$MYEAR" -metadata copyright="$MCOPY" -metadata description="$MDESC"\
-b:v $BITRATE -pass 2 -r $FRAMERATE $MOVIE
rm ffmpeg2pass*
else # single pass
MOVIE=$MOVIE-$PRESET.mp4
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg \
-an -preset $PRESET -c:v libx264 \
$SIZING \
-movflags +faststart \
-metadata title="$MTITLE" -metadata author="$MAUTHOR" -metadata year="$MYEAR" -metadata copyright="$MCOPY" -metadata description="$MDESC"\
-crf $QUALITY -r $FRAMERATE $MOVIE
fi
;;
vp9)
if [ "$TWOPASS" = "1" ]; then
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg \
-c:v libvpx-vp9 -pass 1 -b:v $BITRATE \
$SIZING \
-crf $QUALITY -threads $THREADS -speed 4 -tile-columns 6 -frame-parallel 1 -an -f webm /dev/null
MOVIE=$MOVIE-2pass.webm
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg -c:v libvpx-vp9 -pass 2 -b:v $BITRATE \
$SIZING \
-movflags +faststart \
-metadata title="$MTITLE" -metadata author="$MAUTHOR" -metadata year="$MYEAR" -metadata copyright="$MCOPY" -metadata description="$MDESC"\
-crf $QUALITY -threads $THREADS -speed 1 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 -an -r $FRAMERATE -f webm $MOVIE
rm ffmpeg2pass-0.log
else
MOVIE=$MOVIE-1pass.webm
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg \
$SIZING \
-movflags +faststart \
-metadata title="$MTITLE" -metadata author="$MAUTHOR" -metadata year="$MYEAR" -metadata copyright="$MCOPY" -metadata description="$MDESC"\
-an -c:v libvpx-vp9 -crf $QUALITY -b:v 0 -r $FRAMERATE $MOVIE
fi
;;
hevc|x265)
if [ "$TWOPASS" = "1" ]; then
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg \
-an -c:v libx265 \
$SIZING \
-b:v $BITRATE -preset $PRESET -threads $THREADS -x265-params pass=1:log-level=1 -r $FRAMERATE -f mp4 /dev/null
MOVIE=$MOVIE-$PRESET-2pass.mp4
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg \
-an -c:v libx265 \
$SIZING \
-movflags +faststart \
-metadata title="$MTITLE" -metadata author="$MAUTHOR" -metadata year="$MYEAR" -metadata copyright="$MCOPY" -metadata description="$MDESC"\
-b:v $BITRATE -preset $PRESET -threads $THREADS -x265-params pass=2:log-level=1 -r $FRAMERATE $MOVIE
else # single pass
MOVIE=$MOVIE-$PRESET-1pass.mp4
ffmpeg $OPT -r $FRAMERATE -f image2 -i tl%06d.jpg \
$SIZING \
-movflags +faststart \
-metadata title="$MTITLE" -metadata author="$MAUTHOR" -metadata year="$MYEAR" -metadata copyright="$MCOPY" -metadata description="$MDESC"\
-an \
-c:v libx265 -crf $QUALITY -preset $PRESET -threads $THREADS -x265-params log-level=1 -r $FRAMERATE $MOVIE
fi
;;
esac
ELAPSED_TIME=$(($SECONDS - $START_TIME))
if [ "$VERBOSE" != "0" ]; then
echo output $MOVIE
echo finished in $(($ELAPSED_TIME/60/60)) hr, $(($ELAPSED_TIME/60%60)) min and $(($ELAPSED_TIME % 60)) sec
fi