Files
Server-scripts/convert_heic.sh
T

156 lines
2.8 KiB
Bash
Raw Normal View History

2026-07-23 10:10:04 +02:00
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob nocaseglob
convert_one() {
local src="$1"
if [[ ! -f "$src" ]]; then
echo "ERROR: file not found: $src"
return 1
fi
local base="${src%.*}"
local dst="${base}.jpg"
local tmp="${base}.tmp.jpg"
echo "Converting: $src"
# Convert HEIC -> JPEG
if ! magick "$src" \
-auto-orient \
-quality 100 \
-sampling-factor 4:4:4 \
"jpg:$tmp"; then
echo "ERROR: conversion failed: $src"
rm -f "$tmp"
return 1
fi
if [[ ! -s "$tmp" ]]; then
echo "ERROR: output JPEG is empty: $tmp"
rm -f "$tmp"
return 1
fi
echo "Copying metadata..."
if ! exiftool \
-overwrite_original \
-TagsFromFile "$src" \
-all:all \
-unsafe \
"$tmp" >/dev/null; then
echo "ERROR: metadata copy failed: $src"
rm -f "$tmp"
return 1
fi
touch -r "$src" "$tmp"
mv -f "$tmp" "$dst"
if [[ ! -s "$dst" ]]; then
echo "ERROR: final JPEG missing: $dst"
return 1
fi
2026-07-31 13:47:29 +02:00
rm -f "$src"
echo "Done: $dst"
echo "Removed: $src"
}
convert_mov() {
local src="$1"
if [[ ! -f "$src" ]]; then
echo "ERROR: file not found: $src"
return 1
fi
local base="${src%.*}"
local dst="${base}.mp4"
local tmp="${base}.tmp.mp4"
echo "Converting: $src"
# MOV -> MP4, keep video/audio, drop unsupported data streams
if ! ffmpeg \
-hide_banner \
-loglevel error \
-i "$src" \
-map 0:0 \
-map 0:1 \
-map_metadata 0 \
-c copy \
-movflags use_metadata_tags+faststart \
"$tmp"; then
echo "ERROR: conversion failed: $src"
rm -f "$tmp"
return 1
fi
if [[ ! -s "$tmp" ]]; then
echo "ERROR: output MP4 is empty: $tmp"
rm -f "$tmp"
return 1
fi
echo "Copying metadata..."
if ! exiftool \
-overwrite_original \
-TagsFromFile "$src" \
-all:all \
-unsafe \
"$tmp" >/dev/null; then
echo "ERROR: metadata copy failed: $src"
rm -f "$tmp"
return 1
fi
touch -r "$src" "$tmp"
mv -f "$tmp" "$dst"
if [[ ! -s "$dst" ]]; then
echo "ERROR: final MP4 missing: $dst"
return 1
fi
2026-07-23 10:10:04 +02:00
rm -f "$src"
echo "Done: $dst"
echo "Removed: $src"
}
2026-07-31 13:47:29 +02:00
process_file() {
case "${1,,}" in
*.heic)
convert_one "$1"
;;
*.mov)
convert_mov "$1"
;;
*)
echo "Skipping unsupported file: $1"
;;
esac
}
2026-07-23 10:10:04 +02:00
if [[ $# -gt 0 ]]; then
2026-07-31 13:47:29 +02:00
process_file "$1"
2026-07-23 10:10:04 +02:00
else
2026-07-31 13:47:29 +02:00
files=( *.heic *.mov )
2026-07-23 10:10:04 +02:00
if [[ ${#files[@]} -eq 0 ]]; then
2026-07-31 13:47:29 +02:00
echo "No HEIC or MOV files found."
2026-07-23 10:10:04 +02:00
exit 0
fi
for f in "${files[@]}"; do
2026-07-31 13:47:29 +02:00
process_file "$f"
2026-07-23 10:10:04 +02:00
done
2026-07-31 13:47:29 +02:00
fi