-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-converter.sh
More file actions
112 lines (93 loc) · 3.37 KB
/
Copy pathimage-converter.sh
File metadata and controls
112 lines (93 loc) · 3.37 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
#!/bin/bash
# Function to check and install ffmpeg if needed
ensure_ffmpeg() {
if ! command -v ffmpeg &> /dev/null; then
echo "Installing ffmpeg..."
sudo apt update
sudo apt install -y ffmpeg
echo "ffmpeg installation complete."
else
echo "ffmpeg is already installed."
fi
}
# Function to validate if file is a valid image
is_valid_image() {
local file="$1"
# Use file command to check the actual file type
local file_type
file_type=$(file -b "$file" 2>/dev/null)
# Check if it's a valid image file
if [[ "$file_type" =~ (JPEG|PNG|JPEG image|PNG image) ]]; then
return 0
else
echo "Warning: '$file' may not be a valid image file (detected as: $file_type)"
return 1
fi
}
# Function to check if file needs conversion
should_convert() {
local file="$1"
# Check if file has one of the target suffixes
if [[ "$file" =~ -[0-9]{3}\.(jpg|jpeg|png)$ ]]; then
return 1
fi
# Check if all output files already exist
local base="${file%.*}"
local ext="${file##*.}"
local lower_ext="${ext,,}"
if [[ -f "${base}-400.${lower_ext}" && -f "${base}-800.${lower_ext}" && -f "${base}-1200.${lower_ext}" ]]; then
return 1
fi
return 0
}
# Function to convert a single image
convert_image() {
local input_file="$1"
local base="${input_file%.*}"
local ext="${input_file##*.}"
local lower_ext="${ext,,}"
echo "Processing: $input_file"
# Create the three resized versions with better error handling
if ffmpeg -i "$input_file" -vf "scale='min(480,iw)':-1" "${base}-400.${lower_ext}" -nostdin -loglevel error 2>/dev/null; then
echo " Created: ${base}-400.${lower_ext}"
else
echo " Failed to create: ${base}-400.${lower_ext}"
# Remove any partial output file
[ -f "${base}-400.${lower_ext}" ] && rm "${base}-400.${lower_ext}"
fi
if ffmpeg -i "$input_file" -vf "scale='min(720,iw)':-1" "${base}-800.${lower_ext}" -nostdin -loglevel error 2>/dev/null; then
echo " Created: ${base}-800.${lower_ext}"
else
echo " Failed to create: ${base}-800.${lower_ext}"
[ -f "${base}-800.${lower_ext}" ] && rm "${base}-800.${lower_ext}"
fi
if ffmpeg -i "$input_file" -vf "scale='min(1280,iw)':-1" "${base}-1200.${lower_ext}" -nostdin -loglevel error 2>/dev/null; then
echo " Created: ${base}-1200.${lower_ext}"
else
echo " Failed to create: ${base}-1200.${lower_ext}"
[ -f "${base}-1200.${lower_ext}" ] && rm "${base}-1200.${lower_ext}"
fi
}
# Main script execution
main() {
echo "Starting image conversion process..."
# Ensure ffmpeg is available
ensure_ffmpeg
# Process image files
while IFS= read -r -d '' file; do
if [[ -f "$file" ]]; then
if should_convert "$file"; then
if is_valid_image "$file"; then
convert_image "$file"
else
echo "Skipping invalid image: $file"
fi
else
echo "Skipping: $file (already converted or is a target file)"
fi
fi
done < <(find . -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) -print0)
echo "Image conversion process completed."
}
# Run the main function
main