-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcropfinder
executable file
·101 lines (90 loc) · 2.39 KB
/
cropfinder
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
#!/usr/bin/env bash
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
echo "Exiting."
exit 1
}
usage() {
cat - <<EOF
Usage: $NAME -i <INPUT FILE>
-h|--help: show this help message
-i|--input: file name for input
-s|--samples: number of crop samples to take
-d|--debug: output results continuously
EOF
}
while [ $# -gt 0 ]; do
OPT="$1"
case "${OPT}" in
-i|--input)
shift
INPUT="${1}"
shift;;
-s|--samples)
shift
SAMPLES="${1}"
shift;;
-d|--debug)
DEBUG=1
shift;;
-h|--help)
usage
exit;;
*)
echo "$0: invalid option ${1}"
echo "$0 -h for usage options"
usage
exit 1;;
esac
done
# Check for tool dependencies
SELF_FULLPATH=$(realpath $0)
SELF_DIR=$(dirname $SELF_FULLPATH)
# Override default tool list
TOOLS=(ffmpeg ffprobe grep awk sort uniq head tail)
# Does not depend on any other scripts
SCRIPTS=NONE
if [ ! -r $SELF_DIR/toolchecker ]; then
echo "toolchecker not found."
exit 1
else
. $SELF_DIR/toolchecker
fi
if [ -z "$INPUT" ]; then
echo "Must supply input (-i/--input) file"
usage
exit 1
fi
if [ ! -s "$INPUT" ]; then
echo "Error reading input file: $INPUT"
exit 1
fi
if [ ! -z "$SAMPLES" ]; then
if [ ! $SAMPLES -gt 0 ]; then
echo "Invalid sample count. Using defaults."
SAMPLES=10
fi
else
SAMPLES=10
fi
DUR=$($FFPROBE "$INPUT" 2>&1 | $GREP -e DURATION -e Duration | $HEAD -n1 | $AWK -F': ' '{print $2}' | $AWK -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }' | $AWK -F. '{print $1}')
[ $DEBUG ] && echo "Duration: $DUR"
if [ $DUR -le 0 ]; then
echo "Unknown duration."
exit 1
fi
DUR_TRIM=$((DUR - 600))
SPLIT=$(($DUR_TRIM / $SAMPLES))
[ $DEBUG ] && echo "Sample length: $SPLIT"
#CROPDETECT="cropdetect=24:16:0"
CROPDETECT="cropdetect=.1:16:0"
ALL_CROPS=""
for i in $(seq 1 $SAMPLES); do
START=$(($SPLIT * ${i}))
CROP=$($FFMPEG -ss $START -i "$INPUT" -vframes 360 -vf "$CROPDETECT" -f null -max_muxing_queue_size 9999 - 2>&1 | $GREP -o crop=.* | $SORT | $UNIQ -c | $SORT -n | $TAIL -n1 | $AWK '{print $2}')
[ $DEBUG ] && echo -e "Segment start: $START\t$CROP"
ALL_CROPS="$ALL_CROPS"$'\n'"$CROP"
done
CROP=$(echo "$ALL_CROPS" | $SORT -t "=" -k 2 | $UNIQ -c | $SORT -n | $TAIL -n1 | $AWK '{print $2}')
echo "$CROP"