Bash - Convert and Consolidate Photos
Convert files to JPEG and consolidate them into one directory
A Bash script that recursively scans and consolidates photos into a single directory, converting HEIC/HEIF and LIVP files to JPEG, normalizing file extensions, preserving originals, and avoiding filename conflicts:
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
#!/usr/bin/env bash
#
# Corey Goldberg 2026 (https://github.com/cgoldberg)
#
# Recursively convert and consolidate photos
#
# Requires:
# ImageMagick, binwalk
#
# Converts:
# .jpg / .JPG / .jpeg / .JPEG -> copied unchanged
# .heif / .HEIF / .heic / .HEIC -> converted to JPEG, quality 100
# .livp -> extracts embedded JPEG, quality 100
#
# All output goes into one directory.
# File extensions are normalized to `.jpg`.
# Duplicate filenames become _1, _2, etc.
#
# SOURCE FILES ARE NEVER DELETED, MOVED, OR MODIFIED.
#
# Usage:
# ./flatten-jpegs.sh SOURCE_DIR OUTPUT_DIR
#
# Example:
# ./flatten-jpegs.sh /mnt/photos /mnt/photos-flat
set -u
SOURCE="${1:-}"
OUTPUT="${2:-}"
if [[ -z "$SOURCE" || -z "$OUTPUT" ]]; then
echo "Usage: $0 SOURCE_DIR OUTPUT_DIR"
exit 1
fi
if [[ ! -d "$SOURCE" ]]; then
echo "ERROR: source directory does not exist: $SOURCE"
exit 1
fi
mkdir -p -- "$OUTPUT" || exit 1
# Generate a unique output filename.
unique_name() {
local base="$1"
local candidate="${base}.jpg"
local n=1
while [[ -e "$OUTPUT/$candidate" ]]; do
candidate="${base}_${n}.jpg"
((n++))
done
printf '%s\n' "$candidate"
}
copied=0
converted=0
livp=0
failed=0
skipped=0
while IFS= read -r -d '' file; do
filename="$(basename "$file")"
ext="${filename##*.}"
ext="${ext,,}"
# Remove extension from original filename.
base="${filename%.*}"
case "$ext" in
jpg|jpeg)
# Existing JPEG: copy without recompression.
output_name="$(unique_name "$base")"
if cp -- "$file" "$OUTPUT/$output_name"; then
echo "COPY $file"
echo " -> $output_name"
((copied++))
else
echo "ERROR $file"
((failed++))
fi
;;
heif|heic)
# HEIF/HEIC -> JPEG at maximum JPEG quality.
output_name="$(unique_name "$base")"
if magick "$file" -quality 100 \
"$OUTPUT/$output_name" 2>/dev/null; then
echo "CONVERT $file"
echo " -> $output_name"
((converted++))
else
echo "ERROR $file"
rm -f -- "$OUTPUT/$output_name"
((failed++))
fi
;;
livp)
# LIVP files contain the JPEG beginning at byte 6.
# tail -c +7 starts at byte 6 (1-based).
# ImageMagick reads the embedded JPEG and writes a
# clean JPEG at quality 100.
output_name="$(unique_name "$base")"
if tail -c +7 -- "$file" |
magick - -quality 100 "$OUTPUT/$output_name" 2>/dev/null; then
echo "LIVP $file"
echo " -> $output_name"
((livp++))
else
echo "ERROR $file"
rm -f -- "$OUTPUT/$output_name"
((failed++))
fi
;;
*)
((skipped++))
;;
esac
done < <(find "$SOURCE" -type f -print0)
echo
echo "========================================"
echo "Finished"
echo "========================================"
echo "JPEG copied : $copied"
echo "HEIF/HEIC : $converted"
echo "LIVP : $livp"
echo "Failed : $failed"
echo "Skipped : $skipped"
echo "----------------------------------------"
echo "Output : $OUTPUT"
echo "========================================"
This post is licensed under CC BY 4.0 by the author.