-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZoompanCutVideo.sh
executable file
·144 lines (128 loc) · 5.92 KB
/
ZoompanCutVideo.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
#!/bin/sh
#================================================================================
# AUTHOR
# Clint Box
# https://www.youtube.com/bearcatjamboree
#
# FUNCTION
# Zoom-pan cut a video
#
# DETAILS
# This script will invoke ffmpeg with parameters required to take an input
# video and create a video containing continuous zoom-pan cut a video
#
# USAGE
# ${SCRIPT_NAME} "input_video" "block_time" "zoom_time" "zoom_scale"
#
# PARAMETER MEANING
# block_time = time length between zooms
# zoom_time = time length of zoom
# zoom_scale = the scale of zoom (between 1 and 10)
#================================================================================
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"
width="$2"
##############################################################################
# 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 video 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)
elif [ "$#" -ne 1 ] || ! [ -f "$input_file" ]; then
echo "Usage: $0 input_file width"
exit 1
fi
fi
if ! [ -f "$input_file" ]; then
echo "Usage: $0 $input_file width"
exit 1
fi
##############################################################################
# Prompt for block_time
###############################################################################
if [[ "$block_time" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
block_time=$(osascript -e 'set T to text returned of (display dialog "Enter the block_time (seconds from begin to end of each zoom cut):" buttons {"Cancel", "OK"} default button "OK" default answer "")')
elif [[ "$machine" == "Linux" ]]; then
block_time=$(dialog --title "Enter the block_time (seconds from begin to end of each zoom cut):" --inputbox "block_time:" 8 60)
elif [[ "$machine" == "Cygwin" ]]; then
block_time=$(dialog --title "Enter the block_time (seconds from begin to end of each zoom cut):" --inputbox "block_time:" 8 60)
elif [ "$#" -ne 2 ] || ! [ -f "$input_file" ]; then
echo "Usage: $0 input_file block_time"
exit 1
fi
fi
re='^[0-9]+$'
if ! [[ $block_time =~ $re ]] ; then
echo "Error: \"$block_time\" is not a number" >&2; exit 1
fi
##############################################################################
# Prompt for zoom_time
###############################################################################
if [[ "$zoom_time" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
zoom_time=$(osascript -e 'set T to text returned of (display dialog "Enter the hold time (seconds) of each zoom cut:" buttons {"Cancel", "OK"} default button "OK" default answer "")')
elif [[ "$machine" == "Linux" ]]; then
zoom_time=$(dialog --title "Enter the hold time (seconds) of each zoom cut:" --inputbox "zoom_time:" 8 60)
elif [[ "$machine" == "Cygwin" ]]; then
zoom_time=$(dialog --title "Enter the hold time (seconds) of each zoom cut:" --inputbox "zoom_time:" 8 60)
elif [ "$#" -ne 2 ] || ! [ -f "$input_file" ]; then
echo "Usage: $0 input_file zoom_time"
exit 1
fi
fi
re='^[0-9]+$'
if ! [[ $zoom_time =~ $re ]] ; then
echo "Error: \"$zoom_time\" is not a number" >&2; exit 1
fi
##############################################################################
# Prompt for zoom_scale
###############################################################################
if [[ "$zoom_scale" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
zoom_scale=$(osascript -e 'set T to text returned of (display dialog "Enter new video zoom_scale (aspect ratio will be retained):" buttons {"Cancel", "OK"} default button "OK" default answer "")')
elif [[ "$machine" == "Linux" ]]; then
zoom_scale=$(dialog --title "Enter new video zoom_scale (aspect ratio will be retained)" --inputbox "zoom_scale:" 8 60)
elif [[ "$machine" == "Cygwin" ]]; then
zoom_scale=$(dialog --title "Enter new video zoom_scale (aspect ratio will be retained)" --inputbox "zoom_scale:" 8 60)
elif [ "$#" -ne 2 ] || ! [ -f "$input_file" ]; then
echo "Usage: $0 input_file zoom_scale"
exit 1
fi
fi
if [[ "$zoom_scale" -lt "1" ]] ; then
echo "Error: Zoom scale must be between 1 and 10" >&2; exit 1
fi
# get frame rate from ffmpeg
frame_rate=$(ffmpeg -i "$input_file" 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p")
video_res=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$input_file")
####################################
# 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"_zoomcut
newoutfile=$outfile.$ext
ffmpeg -r $frame_rate -i "$input_file" -vf "zoompan=s=$video_res:z='if(lte(mod(time,$block_time),$zoom_time),$zoom_scale,1)':d=1:x=iw/$zoom_scale-(iw/zoom/$zoom_scale):y=ih/$zoom_scale-(ih/zoom/$zoom_scale):fps=$frame_rate" "$newoutfile"