-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateMobileCrossclip.sh
executable file
·253 lines (221 loc) · 9.09 KB
/
CreateMobileCrossclip.sh
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/sh
#================================================================================
# AUTHOR
# Clint Box
# https://www.youtube.com/bearcatjamboree
#
# FUNCTION
# Create a crossclip style video for mobile devices
#
# DETAILS
# This script will invoke ffmpeg with parameters required to take an input
# video and produce a crossclip style video
#
# USAGE
# ${SCRIPT_NAME} "video_path" "out_width" "out_height" "cropped_X" "cropped_Y" "style"
#
# PARAMETER MEANING
# out_width = width of new video
# out_height = height of new video
# cropped_X = X-value of top left corner to start crop
# cropped_Y = Y-value of top left corner to start crop
# style = "Basic" or "Bubble"
#================================================================================
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
echo "${machine}"
input_file="$1"
out_w="$2"
out_h="$3"
x="$4"
y="$5"
style="$6"
##############################################################################
# Check for file was passed. Show open file dialog if no argument and on Mac
###############################################################################
if ! [ -f "$input_file" ]; then
if [[ "$machine" == "Mac" ]]; then
input_file=$(osascript -e 'tell application (path to frontmost application as text)
set input_file to choose file with prompt "Please choose a file to process"
POSIX path of input_file
end')
elif [[ "$machine" == "Linux" ]]; then
input_file=$(dialog --title "Choose a file" --stdout --title "Please choose a file to process" --fselect ~/ 14 48)
elif [[ "$machine" == "Cygwin" ]]; then
input_file=$(dialog --title "Choose a file" --stdout --title "Please choose a file to process" --fselect ~/ 14 48)
else
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
fi
if ! [ -f "$input_file" ]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
# Get video resolution for switch w/h to h/w
video_res=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$input_file")
array=($(sed 'y/x/ /' <<<"$video_res"))
new_width="${array[2]}"
new_height="${array[1]}"
echo "$new_width"
echo "$new_height"
##############################################################################
# Prompt for out_w
###############################################################################
if [[ "$out_w" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
out_w=$(osascript -e 'set T to text returned of (display dialog "Enter the width of the _output rectangle: " buttons {"Cancel", "OK"} default button "OK" default answer "720")')
elif [[ "$machine" == "Linux" ]]; then
out_w=$(dialog --title "Enter the width of the output rectangle: " --inputbox "out_w:" 8 60)
elif [[ "$machine" == "Cygwin" ]]; then
out_w=$(dialog --title "Enter the width of the output rectangle: " --inputbox "out_w:" 8 60)
elif [ "$#" -ne 2 ] || ! [ -f "$input_file" ]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
fi
if [[ "$out_w" == "" ]]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
##############################################################################
# Prompt for out_h
###############################################################################
if [[ "$out_h" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
out_h=$(osascript -e 'set T to text returned of (display dialog "Enter the height of the _output rectangle:" buttons {"Cancel", "OK"} default button "OK" default answer "200")')
elif [[ "$machine" == "Linux" ]]; then
out_h=$(dialog --title "Enter the height of the output rectangle:" --inputbox "out_h:" 8 60)
elif [[ "$machine" == "Cygwin" ]]; then
out_h=$(dialog --title "Enter the height of the output rectangle:" --inputbox "out_h:" 8 60)
elif [ "$#" -ne 2 ] || ! [ -d "$output_folder" ]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
fi
if [[ "$out_h" == "" ]]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
##############################################################################
# Prompt for x
###############################################################################
if [[ "$x" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
x=$(osascript -e 'set T to text returned of (display dialog "Enter the top left corner X-position:" buttons {"Cancel", "OK"} default button "OK" default answer "880")')
elif [[ "$machine" == "Linux" ]]; then
x=$(dialog --title "Enter the top left corner X-position:" --inputbox "x:" 8 60)
elif [[ "$machine" == "Cygwin" ]]; then
x=$(dialog --title "Enter the top left corner X-position:" --inputbox "x:" 8 60)
elif [ "$#" -ne 2 ] || ! [ -d "$output_folder" ]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
fi
if [[ "$x" == "" ]]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
##############################################################################
# Prompt for y
###############################################################################
if [[ "$y" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
y=$(osascript -e 'set T to text returned of (display dialog "Enter the top left corner Y-position:" buttons {"Cancel", "OK"} default button "OK" default answer "320")')
elif [[ "$machine" == "Linux" ]]; then
y=$(dialog --title "Enter the top left corner Y-position:" --inputbox "y:" 8 60)
elif [[ "$machine" == "Cygwin" ]]; then
y=$(dialog --title "Enter the top left corner Y-position:" --inputbox "y:" 8 60)
elif [ "$#" -ne 2 ] || ! [ -d "$output_folder" ]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
fi
if [[ "$y" == "" ]]; then
echo "Usage: $0 input_file out_w out_h x y"
exit 1
fi
re='^[0-9]+$'
input=( "$out_w" "$out_h" "$x" "$y" )
for i in "${input[@]}"
do
if ! [[ $i =~ $re ]] ; then
echo "Error: \"$i\" is not a number" >&2; exit 1
fi
done
####################################
# Remove backlashes from filepaths
####################################
file=$(echo "$input_file"|tr -d '\\')
####################################
# Separate file name from extension
####################################
ext="${file##*.}"
name="${file%.*}"
outfile="$name"_test
newoutfile=$outfile.$ext
####################################
# Make temp directory
####################################
tmp_dir=$(mktemp -d -t ci-$(date +%Y-%m-%d-%H-%M-%S)-XXXXXXXXXX)
##############################################################################
# Check for voice to use
###############################################################################
if [[ "$style" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
style=$(osascript -e 'return choose from list { "Basic", "Bubble" }')
elif [[ "$machine" == "Linux" ]]; then
style=$(dialog --title "Select video style" --stdout --title "Please choose a file to process" --menubox ~/ 14 48)
elif [[ "$machine" == "Cygwin" ]]; then
style=$(dialog --title "Select video style" --stdout --title "Please choose a file to process" --menubox ~/ 14 48)
else
echo "Unknown platform"
exit 1
fi
fi
if [[ "$style" == "Basic" ]]; then
# Create top clip
ffmpeg -i "$input_file" -filter:v "crop=$out_w:$out_h:$x:$y" "$tmp_dir/tmp1.mp4"
ffmpeg -i "$tmp_dir/tmp1.mp4" -vf scale=$new_width:-2 "$tmp_dir/top.mp4"
# Create bottom clip
#ffmpeg -i "$input_file" -vf pad="in_w:in_h+200:0:0" "$tmp_dir/tmp1.mp4"
ffmpeg -i "$input_file" -vf scale=-2:$new_height "$tmp_dir/tmp2.mp4"
ffmpeg -i "$tmp_dir/tmp2.mp4" -vf "crop=$new_width:$new_height" "$tmp_dir/bottom.mp4"
ffmpeg -i "$tmp_dir/top.mp4" -i "$tmp_dir/bottom.mp4" -filter_complex vstack=inputs=2 "$newoutfile"
rm -rf $tmp_dir
elif [[ "$style" == "Bubble" ]]; then
ffmpeg -i "$input_file" -filter:v "crop=$out_w:$out_h:$x:$y" "$tmp_dir/top.mp4"
# Create bottom clip
ffmpeg -i "$input_file" -vf pad="in_w:in_h:0:0+200" "$tmp_dir/tmp1.mp4"
ffmpeg -i "$tmp_dir/tmp1.mp4" -vf scale=-2:$new_height "$tmp_dir/tmp2.mp4"
ffmpeg -i "$tmp_dir/tmp2.mp4" -vf "crop=$new_width:$new_height" "$tmp_dir/bottom.mp4"
ffmpeg \
-i "$tmp_dir/bottom.mp4" \
-i "$tmp_dir/top.mp4" \
-filter_complex "\
[1]format=yuva444p,geq=lum='p(X,Y)':a='st(1,pow(min(W/2,H/2),2))+st(3,pow(X-(W/2),2)+pow(Y-(H/2),2));if(lte(ld(3),ld(1)),255,0)'[circular shaped video];\
[circular shaped video]scale=w=-1:h=300[circular shaped video small];\
[0][circular shaped video small]overlay" \
-filter_complex_threads 1 \
-map 0:a \
-metadata:s:a:0 title="Sound main movie" \
-disposition:a:0 default \
-map 1:a \
-metadata:s:a:1 title="Sound overlayed movie" \
-disposition:a:1 none \
-c:v libx264 \
-preset ultrafast \
-shortest \
"$newoutfile"
else
echo "Unknown style"
exit 1
fi
rm -rf $tmp_dir
exit 1