-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathzoompan
executable file
·214 lines (177 loc) · 5.88 KB
/
zoompan
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
#!/bin/sh
#===============================================================================
# zoompan
# ken burns effect
#===============================================================================
# dependencies:
# ffmpeg ffprobe
#===============================================================================
# script usage
#===============================================================================
# script usage
usage ()
{
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
# zoompan, ken burns effect
$(basename "$0") -i input.(png|jpg|jpeg) -d (000) -z (in|out) -p (tl|c|tc|tr|bl|br) -o output.mp4
-i = input.(png|jpg|jpeg)
-d = duration : from 1-999
-z = zoom : in or out
-p = position : zoom to location listed below
-o = outfile.mp4 : optional argument # default is input-name-zoompan-date-time
+------------------------------+
+tl tc tr+
+ +
+ c +
+ +
+bl br+
+------------------------------+"
exit 2
}
#===============================================================================
# error messages
#===============================================================================
INVALID_OPT_ERR='Invalid option:'
REQ_ARG_ERR='requires an argument'
WRONG_ARGS_ERR='wrong number of arguments passed to script'
NOT_MEDIA_FILE_ERR='is not a media file'
#===============================================================================
# check the number of arguments passed to the script
#===============================================================================
[ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}"
#===============================================================================
# getopts check the options passed to the script
#===============================================================================
while getopts ':i:d:z:p:o:h' opt
do
case ${opt} in
i) infile="${OPTARG}";;
d) dur="${OPTARG}";;
z) zoom="${OPTARG}"
[ "${zoom}" = 'in' ] || [ "${zoom}" = 'out' ];;
p) position="${OPTARG}";;
o) outfile="${OPTARG}";;
h) usage;;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
#===============================================================================
# variables
#===============================================================================
# input, input name and extension
infile_nopath="${infile##*/}"
infile_name="${infile_nopath%.*}"
# ffprobe get image height
imgheight="$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=nw=1:nk=1 "${infile}")"
# frames per seconds
infps='30'
# zoom in positions
# zoom in top left
zi_top_left="scale=-2:10*ih,\
zoompan=z='min(zoom+0.0015,1.5)':\
fps=${infps}:d=${infps}*${dur},\
scale=-2:${imgheight}" \
# zoom in center
zi_center="scale=-2:10*ih,\
zoompan=z='min(zoom+0.0015,1.5)':\
fps=${infps}:d=${infps}*${dur}:\
x='iw/2-(iw/zoom/2)':\
y='ih/2-(ih/zoom/2)',\
scale=-2:${imgheight}" \
# zoom in top center
zi_top_center="scale=-2:10*ih,\
zoompan=z='min(zoom+0.0015,1.5)':\
fps=${infps}:d=${infps}*${dur}:\
x='iw/2-(iw/zoom/2)',\
scale=-2:${imgheight}" \
# zoom in top right
zi_top_right="scale=-2:10*ih,\
zoompan=z='min(zoom+0.0015,1.5)':\
fps=${infps}:d=${infps}*${dur}:\
x='iw/zoom-(iw/zoom/2)',\
scale=-2:${imgheight}" \
# zoom out positions
# zoom out top left
zo_top_left="scale=-2:2*ih,\
zoompan=z='if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0015))':\
fps=${infps}:d=${infps}*${dur},\
scale=-2:${imgheight}" \
zo_center="scale=-2:2*ih,\
zoompan=z='if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0015))':\
fps=${infps}:d=${infps}*${dur}:\
x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)',\
scale=-2:${imgheight}" \
# zoom out top center
zo_top_center="scale=-2:2*ih,\
zoompan=z='if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0015))':\
fps=${infps}:d=${infps}*${dur}:\
x='iw/2-(iw/zoom/2)',\
scale=-2:${imgheight}" \
# zoom out top right
zo_top_right="scale=-2:2*ih,\
zoompan=z='if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0015))':\
fps=${infps}:d=${infps}*${dur}:\
x='iw/zoom-(iw/zoom/2)',\
scale=-2:${imgheight}" \
# zoom out bottom left
zo_bottom_left="scale=-2:2*ih,\
zoompan=z='if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0015))':\
fps=${infps}:d=${infps}*${dur}:\
y='ih-ih/zoom',\
scale=-2:${imgheight}" \
# zoom out bottom right
zo_bottom_right="scale=-2:2*ih,\
zoompan=z='if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0015))':\
fps=${infps}:d=${infps}*${dur}:\
x='iw-iw/zoom':\
y='ih-ih/zoom',\
scale=-2:${imgheight}" \
# outfile file recording destination
outfile_default="${infile_name}-zoompan-$(date +"%Y-%m-%d-%H-%M-%S").mp4"
#===============================================================================
# function
#===============================================================================
# zoom function
zoom_func () {
ffmpeg \
-hide_banner \
-stats -v panic \
-r 30 \
-i "${infile}" \
-filter_complex \
"${1}" \
-y -shortest \
-c:v libx264 -crf 18 -profile:v high \
-r 30 -pix_fmt yuv420p \
-movflags +faststart -f mp4 \
"${outfile:=${outfile_default}}"
}
# check zoom and position
case "${zoom}" in
in)
case "${position}" in
tl) zoom_func "${zi_top_left}" "${infile}";;
tc) zoom_func "${zi_top_center}" "${infile}";;
c) zoom_func "${zi_center}" "${infile}";;
tr) zoom_func "${zi_top_right}" "${infile}";;
*) help;;
esac
;;
out)
case "${position}" in
tl) zoom_func "${zo_top_left}" "${infile}";;
tc) zoom_func "${zo_top_center}" "${infile}";;
c) zoom_func "${zo_center}" "${infile}";;
tr) zoom_func "${zo_top_right}" "${infile}";;
bl) zoom_func "${zo_bottom_left}" "${infile}";;
br) zoom_func "${zo_bottom_right}" "${infile}";;
*) help;;
esac
;;
*) usage "${infile} ${NOT_MEDIA_FILE_ERR}";;
esac