-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_to_webp.sh
More file actions
executable file
·64 lines (55 loc) · 1.61 KB
/
convert_to_webp.sh
File metadata and controls
executable file
·64 lines (55 loc) · 1.61 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
#!/bin/bash
# Default values
QUALITY=80
OUTPUT_DIR="webp_images"
INPUT_FILE=""
INPUT_DIR="."
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-q|--quality)
QUALITY="$2"
shift; shift
;;
-o|--output)
OUTPUT_DIR="$2"
shift; shift
;;
-f|--file)
INPUT_FILE="$2"
shift; shift
;;
-d|--directory)
INPUT_DIR="$2"
shift; shift
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [-q quality] [-o output_dir] [-f file] [-d directory]"
exit 1
;;
esac
done
mkdir -p "$OUTPUT_DIR"
echo "Converting images to WebP with quality ${QUALITY}..."
echo "Output directory: ./${OUTPUT_DIR}/"
if [ -n "$INPUT_FILE" ]; then
if [ -e "$INPUT_FILE" ]; then
output_file="${INPUT_FILE##*/}"
output_file="${output_file%.*}.webp"
echo "Converting: $INPUT_FILE -> ${OUTPUT_DIR}/${output_file}"
ffmpeg -i "$INPUT_FILE" -q:v "$QUALITY" -loglevel error "${OUTPUT_DIR}/${output_file}"
else
echo "Error: File not found: $INPUT_FILE"
exit 1
fi
else
for img in "${INPUT_DIR}"/*.jpg "${INPUT_DIR}"/*.jpeg "${INPUT_DIR}"/*.png; do
[ -e "$img" ] || continue
output_file="${img##*/}"
output_file="${output_file%.*}.webp"
echo "Converting: $img -> ${OUTPUT_DIR}/${output_file}"
ffmpeg -i "$img" -q:v "$QUALITY" -loglevel error "${OUTPUT_DIR}/${output_file}"
done
fi
echo "All images converted!"