-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCropVideo.sh
executable file
·174 lines (157 loc) · 6.34 KB
/
CropVideo.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
#!/bin/sh
#================================================================================
# AUTHOR
# Clint Box
# https://www.youtube.com/bearcatjamboree
#
# FUNCTION
# Crop a video
#
# DETAILS
# This script will invoke ffmpeg with parameters required to take an input
# video and a cropped version
#
# USAGE
# ${SCRIPT_NAME} "video_path" "out_width" "out_height" "cropped_X" "cropped_Y"
#
# 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
#================================================================================
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"
##############################################################################
# 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 /tmp/ 14 48)
elif [[ "$machine" == "Cygwin" ]]; then
input_file=$(dialog --title "Choose a file" --stdout --title "Please choose a file to process" --fselect /tmp/ 14 48)
elif [ "$#" -ne 1 ] || ! [ -f "$input_file" ]; then
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
##############################################################################
# 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 "")')
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 "")')
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 "")')
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 "")')
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%.*}"
############################
# Get file path information
############################
outfile="$name"_cropped
newoutfile=$outfile.$ext
ffmpeg -i "$input_file" -filter:v "crop=$out_w:$out_h:$x:$y" "$newoutfile"