-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvid2webp
More file actions
executable file
·121 lines (103 loc) · 3 KB
/
Copy pathvid2webp
File metadata and controls
executable file
·121 lines (103 loc) · 3 KB
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
#!/bin/bash
if [ -z "$1" ]; then
echo
echo "Clip videos into animated webp format."
echo
echo "Usage: $(basename "$0") <input_file> [start_time] [end_time] [output_name]"
echo
echo "Parameters:"
echo " input_file - Source video file"
echo " start_time - Start time (optional, format: HH:MM:SS)"
echo " end_time - End time (optional, format: HH:MM:SS)"
echo " output_name - Output filename without extension (optional)"
echo
echo "Output will be saved as <input_file>{-1,-2,etc}.webp in the same directory."
echo
echo "Environment variables:"
echo " SAMPLE - Sample time for crop detection (default: start_time or 1 second mark)"
echo " FRAMES - Number of frames to analyze for crop detection (default: 10)"
echo " SCALE - Output scale factor (default: 1) applied to cropped area"
echo " FPS - Output framerate (default: 20)"
echo
echo "Examples:"
echo " $(basename "$0") video.mp4 05 10"
echo " $(basename "$0") video.mp4 \"00:00:55\" \"00:01:12\""
echo " SCALE=0.25 $(basename "$0") video.mp4 05 10 video-thumbnail"
echo
echo "Which Produces:"
echo " video.webp"
echo " video-1.webp"
echo " video-thumbnail.webp"
echo
echo "ENV vars persist between runs, use unset to return to default."
echo " SCALE=0.8 $(basename "$0") video.mp4 0 10"
echo " $(basename "$0") video.mp4 10 20"
echo " SCALE=0.5 $(basename "$0") video.mp4 0 10"
echo " $(basename "$0") video.mp4 10 20"
echo " unset SCALE # resets to SCALE=1"
echo " $(basename "$0") video.mp4"
echo
echo "If cropdetect fails, try overriding SAMPLE position or increasing FRAMES"
echo " $ SAMPLE=5 FRAMES=20 $(basename "$0") video.mp4"
echo
exit 1
fi
if [ ! -f "$1" ]; then
echo "$1 not found"
exit 1
fi
function get_next_filename() {
local base="${1%.*}"
local ext="${1##*.}"
local newname="$1"
local counter=1
while [[ -f "$newname" ]]; do
newname="${base}-${counter}.${ext}"
((counter++))
done
echo "$newname"
}
if [ ! -z "$4" ]; then
out="${4%.webp}.webp"
else
dir=$(dirname "$1")
out=$(basename "$1")
out="$dir/${out%.*}.webp"
out=$(get_next_filename "$out")
fi
start=""
end=""
if [ ! -z "$2" ]; then
start="-ss $2 "
fi
if [ ! -z "$3" ]; then
end="-to $3 "
fi
if [ -z "$SAMPLE" ]; then
sample="$start"
else
sample="-ss ${SAMPLE:-1}"
fi
scale=${SCALE:-1}
fps=${FPS:-20}
frames=${FRAMES:-10}
crop=$(ffmpeg $sample -i "$1" -vframes $frames -vf cropdetect -f null - 2>&1 | grep -m 1 -oP 'crop=\K[0-9:]+')
if [ -z "$crop" ]; then
echo "cropdetect failed"
exit 1
fi
width=$(echo "$crop" | cut -d':' -f1)
width=$(echo "$width * $scale" | bc)
height=$(echo "$crop" | cut -d':' -f2)
height=$(echo "$height * $scale" | bc)
if [ -z "$width" ] || [ -z "$height" ]; then
echo "scaling failed"
exit 1
fi
if [ "$height" -gt "$width" ]; then
scale="-2:$height"
else
scale="$width:-2"
fi
# NOTE: fps=fps= is not a typo
ffmpeg -i "$1" $start $end -loop 1 -an -vf crop="$crop",scale="$scale",fps=fps="$fps" "$out"