-
Notifications
You must be signed in to change notification settings - Fork 0
/
over
executable file
·238 lines (206 loc) · 7.44 KB
/
over
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
236
237
238
#!/bin/zsh
# over <reaction-video-file> <show-video-file>
# Take two videos ("reaction" and "show") and overlay "show" along the bottom of
# the reaction. There are options to:
#
# - add margins
# - set the size of the reaction (default is 1/4 of the screen — i.e. 0.5 scale)
# - set location left/center/right (default center)
# - only process the first X seconds (to test the video)
# - start the show X seconds after the beginning of the reaction
#
#set -x
prog=`basename $0`
usage() {
cat <<-EOF
Usage: $prog [-? | -n | -l loc | -t dur | -e trim | -m margin | -h hmargin | -v vmargin | -s scale] reaction show [offset [left/centre/right [duration [trim]]]]
-? - show this help
-o. - open output file after completion
-n - show what would happen but do not actually encode
-l loc - where to put the show at the bottom of the screen: left, center/centre, right
-t dur - how long to clip for (useful for testing to see if you got the offset right)
-m margin - margin at left/bottom/right edges between the show and the edge of the reaction
-h hmargin - horizontal margin at left/right edges between the show and the edge of the reaction
-v vmargin - vertical margin at bottom edge between the show and the edge of the reaction
+h hborder - horizontal border
+v vborder - vertical border
-s scale - the size of the show relative to the reaction (default: 0.5)
-e dur - how many seconds to trim off the end of the movie
-b dur - how many seconds to trim off the beginning of the reaction?
defaults: $prog -s 0.5 -l center reaction show 0
EOF
exit 0
}
dry_run=false
open_when_done=false
srcDelay=0
duration=""
location=centre
start_trim=0
end_trim=0
play_time=0
## spacing between the show and the edge of the screen
# vertically
integer vmargin=0
integer vborder=0
# horizontally
integer hmargin=0
integer hborder=0
# how big the overlay is
overlayScale=.5
while getopts '?'nol:t:e:b:m:s:h:v: opt; do
case $opt in
'?') usage ;;
n) dry_run=true ;;
o) open_when_done=true ;;
l) location="${OPTARG}" ;;
t) duration=(-t "${OPTARG}"); play_time="${OPTARG}" ;;
e) end_trim="${OPTARG}" ;;
b) start_trim="${OPTARG}" ;;
m) vmargin="${OPTARG}"; hmargin="${OPTARG}" ;;
h) hmargin="${OPTARG}" ;;
v) vmargin="${OPTARG}" ;;
+h) hborder="${OPTARG}" ;;
+v) vborder="${OPTARG}" ;;
s) overlayScale="${OPTARG}" ;;
esac
done
# remove flags from the paramater list
shift $((OPTIND-1))
echo "## ARGS are" "${@}"
# require at least two arguments: the reaction and the show
if [ $# -lt 2 ]; then
usage
exit 1
fi
# first the reaction, then the show
reaction="$1"
show="$2"
reactionWidth=0
reactionHeight=0
reactionDuration=0
showWidth=0
showHeight=0
showDuration=0
reactionDurationExpr="$(ffmpeg -hide_banner -i "${reaction}" |& grep Duration: | perl -pe 's/.*Duration:\s*([0-9:.]+),.*/reactionDuration=${1}/')"
eval "$reactionDurationExpr"
reactionSeconds=$(echo "${reactionDuration}" | perl -pe 's#(\d+):(\d+):(\d+)\.(\d+)#$4/100 + $3 + $2*60 + $1*3600#e')
showDurationExpr="$(ffmpeg -hide_banner -i "${show}" |& grep Duration: | perl -pe 's/.*Duration:\s*([0-9:.]+),.*/showDuration=${1}/')"
eval "$showDurationExpr"
# 00:00:06.68
showSeconds=$(echo "${showDuration}" | perl -pe 's#(\d+):(\d+):(\d+)\.(\d+)#$4/100 + $3 + $2*60 + $1*3600#e')
[ -z "$3" ] || srcDelay="$3"
[ -z "$4" ] || location="$4"
[ -z "$5" ] || duration=(-t "$5")
[ -z "$6" -o "x$6" = x0 ] || end_trim="${6}"
# convert min:sec to seconds if it's formatted that way
end_trim=$(echo "${end_trim}" | perl -pe 's#(\d+):(\d+)#$1*60 + $2#e')
start_trim=$(echo "${start_trim}" | perl -pe 's#(\d+):(\d+)#$1*60 + $2#e')
play_time=$(echo "${play_time}" | perl -pe 's#(\d+):(\d+)#$1*60 + $2#e')
out="${reaction%.*}-Reid.${reaction##*.}"
echo "##"
echo "## show = ${show}"
echo "## - duration = ${showDuration} [$((${showSeconds} - end_trim)) s]"
echo "## reaction = ${reaction}"
echo "## - duration = ${reactionDuration} [${reactionSeconds} s]"
echo "## delay = $srcDelay"
echo "## location = $location"
echo "## duration = $duration"
echo "## beg trim = ${start_trim}"
echo "## end trim = ${end_trim}"
echo "## dry run = ${dry_run}"
echo "## borders = h: $hborder / v: $vborder"
echo "##"
echo "## / out = $out"
echo ""
reactionExpr="$(ffmpeg -hide_banner -i "${reaction}" |& grep 'Video:' | head -1 | perl -pe 's/^.* (\d+)x(\d+).*/reactionWidth=${1}; reactionHeight=${2}/')"
echo "reactionExpr=$reactionExpr"
eval "$reactionExpr"
showExpr="$(ffmpeg -hide_banner -i "${show}" |& grep 'Video:' | head -1 | perl -pe 's/^.* (\d+)x(\d+).*/showWidth=${1}; showHeight=${2}/')"
echo "showExpr=$showExpr"
eval "$showExpr"
overlayWidth=$(dc -e "$reactionWidth $overlayScale * p")
overlayHeight=$(dc -e "$reactionHeight $overlayScale * p")
srcScale=$(($overlayWidth / $showWidth))
integer hoffset
integer voffset
case $location in
right) hoffset=$(dc -e "$reactionWidth - $showWidth $srcScale * - $hmargin - 0.1 + p") ;;
left) hoffset=$hmargin ;;
*) hoffset=$(dc -e "$reactionWidth 2 / - $showWidth 2 / $srcScale * - $hmargin + 0.1 + p") ;;
esac
echo "# show size: ${showWidth}x${showHeight}: $show"
echo "#reaction duration: ${reactionDuration} [${reactionSeconds} seconds]"
echo "# show duration: ${showDuration} [${showSeconds} seconds]"
echo "# rection size: ${reactionWidth}x${reactionHeight}: $reaction"
echo "# overlay size: ${overlayWidth}x${overlayHeight} @ ${overlayScale}X"
voffset=$((reactionHeight - showHeight*srcScale - $vmargin))
echo "# `date` -- starting @ srcScale = $srcScale (dx=$hoffset, dy=$voffset)]"
echo ""
qq='"'
q="'"
# Arguments to -filter_complex for various situations
# ---------------------------------------------------
#
# Mixing audio from 2 sources:
# amix=inputs=2:duration=longest
#
# for different volumes:
# [0:0]volume=0.09[a];[1:0]volume=1.8[b];[a][b]amix=inputs=2:duration=longest
# [a]adelay=${srcDelay}|${srcDelay}[a];[a][b]amix=inputs=2:dropout_transition=0
#
# Audio delay
# [a]adelay=2000|2000[a];[a][b]amix=inputs=2:dropout_transition=0
#
if ((end_trim != 0 || start_trim != 0)); then
if ((end_trim != 0)); then
duration=(-ss $start_trim -to $(($showSeconds - $end_trim)))
elif ((play_time != 0)); then
duration=(-ss $start_trim -to $((play_time)))
else
duration=(-ss $start_trim -to $showSeconds)
fi
fi
((totalHeight = showHeight + 2*vborder))
((totalWidth = showWidth + 2*hborder))
filter=$(
cat <<-EOF
amix=inputs=2:duration=longest;
[1] pad=${totalWidth}:${totalHeight}:(ow-iw)/2:(oh-ih)/2,setsar=1 [bordered];
[bordered] scale=iw*${srcScale}:-1 [scaled];
[0][scaled] overlay=${hoffset}:${voffset}
EOF
)
echo ffmpeg -y -hide_banner -loglevel warning -stats \\
echo \ -i $qq"${reaction}"$qq \\
echo \ -itsoffset "${srcDelay}" \\
echo \ -i $qq"${show}"$qq \\
echo \ -filter_complex $q"$filter"$q \\
echo \ -async 1 \\
echo \ -map 0:a \\
echo \ ${duration[*]} \\
echo \ $qq"$out"$qq
echo ""
if $dry_run; then
exit 0
fi
ffmpeg -y -hide_banner -loglevel warning -stats \
-i "${reaction}" \
-itsoffset "${srcDelay}" \
${trim[*]} \
-i "${show}" \
-filter_complex "${filter}" \
-async 1 \
-map 0:a \
${duration[*]} \
"$out"
echo "# `date` -- done: $(echo "file://${PWD}/${out}" | sed -e 's/ /%20/g')"
! $dry_run && $open_when_done && open "$out"
# -vf subtitles="${show}"
# -t 00:10:00 \
# -t 00:02:00 \
# "$out" && open "$out"
# -map 0:0 -map 1:1 -shortest \
# To copy the video from input_0.mp4 and audio from input_1.mp4:
#
# $ ffmpeg -i input_0.mp4 -i input_1.mp4 -c copy -map 0:0 -map 1:1 -shortest out.mp4