-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathpan-scan
executable file
·124 lines (100 loc) · 3.92 KB
/
pan-scan
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
#!/bin/sh
#===============================================================================
# pan-scan
# pan scan over an image
#===============================================================================
# dependencies:
# ffmpeg
#===============================================================================
# script usage
#===============================================================================
usage ()
{
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
# pan scan image
$(basename "$0") -i input.(png|jpg|jpeg) -d (000) -p (l|r|u|d) -o output.mp4
-i = input.(png|jpg|jpeg)
-d = duration : from 1-999
-p = position : left, right, up, down
-o = output.mp4 : optional argument # default is input-name-pan-date-time"
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:p:o:h' opt
do
case ${opt} in
i) infile="${OPTARG}";;
d) dur="${OPTARG}";;
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%.*}"
# check if tile is null
if [ -z "${infile}" ]; then
: # tile variable not set : = pass
else
# ffprobe get image height
imgsize="$(ffprobe -v error -select_streams v:0 -show_entries stream=height,width -of csv=s=x:p=0 "${infile}")"
img_w=$(echo "${imgsize}" | awk -F'x' '{print $1}')
img_h=$(echo "${imgsize}" | awk -F'x' '{print $2}')
fi
# pan
left="scale=w=-2:h=3*${img_h},crop=w=3*${img_w}/1.05:h=3*${img_h}/1.05:x=t*(in_w-out_w)/${dur},scale=w=${img_w}:h=${img_h},setsar=1" \
right="scale=w=-2:h=3*${img_h},crop=w=3*${img_w}/1.05:h=3*${img_h}/1.05:x=(in_w-out_w)-t*(in_w-out_w)/${dur},scale=w=${img_w}:h=${img_h},setsar=1" \
up="scale=w=-2:h=3*${img_h},crop=w=3*${img_w}/1.2:h=3*${img_h}/1.2:y=t*(in_h-out_h)/${dur},scale=w=${img_w}:h=${img_h},setsar=1" \
down="scale=w=-2:h=3*${img_h},crop=w=3*${img_w}/1.2:h=3*${img_h}/1.2:y=(in_h-out_h)-t*(in_h-out_h)/${dur},scale=w=${img_w}:h=${img_h},setsar=1" \
# output file recording destination
outfile_default="${infile_name}-pan-$(date +"%Y-%m-%d-%H-%M-%S").mp4"
#===============================================================================
# functions
#===============================================================================
# zoom function
pan_func () {
ffmpeg \
-hide_banner \
-stats -v panic \
-r 30 \
-y -loop 1 \
-i "${infile}" -ss 0 -t "${dur}" \
-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 "${position}" in
l) pan_func "${left}";;
r) pan_func "${right}" "${infile}";;
u) pan_func "${up}" "${infile}";;
d) pan_func "${down}" "${infile}";;
*) usage "${infile} ${NOT_MEDIA_FILE_ERR}";;
esac