Skip to content

Instantly share code, notes, and snippets.

@Santhin
Created March 29, 2026 12:44
Show Gist options
  • Select an option

  • Save Santhin/c763384b928533306c3990942302562d to your computer and use it in GitHub Desktop.

Select an option

Save Santhin/c763384b928533306c3990942302562d to your computer and use it in GitHub Desktop.
Degoogle
#!/bin/bash
TARGET_DIR="${1:-.}"
if [ ! -d "$TARGET_DIR" ]; then
echo "Error: '$TARGET_DIR' is not a valid directory"
exit 1
fi
processed=0
skipped=0
cd "$TARGET_DIR" || exit 1
EXTENSIONS=(jpg jpeg png gif heic heif tiff webp mp4 mov avi mkv)
find_image() {
local base="$1"
[ -f "$base" ] && echo "$base" && return 0
local name="${base%.*}"
for ext in "${EXTENSIONS[@]}"; do
[ -f "$name.$ext" ] && echo "$name.$ext" && return 0
done
for ext in "${EXTENSIONS[@]}"; do
local match=( ${base}*.$ext )
[ -f "${match[0]}" ] && echo "${match[0]}" && return 0
done
return 1
}
fix_extension() {
local filepath="$1"
local mime expected_ext actual_ext new_path
mime=$(file -b --mime-type "$filepath")
case "$mime" in
image/jpeg) expected_ext="jpg" ;;
image/png) expected_ext="png" ;;
image/gif) expected_ext="gif" ;;
image/heic) expected_ext="heic" ;;
image/heif) expected_ext="heif" ;;
image/tiff) expected_ext="tiff" ;;
image/webp) expected_ext="webp" ;;
video/mp4) expected_ext="mp4" ;;
video/quicktime) expected_ext="mov" ;;
video/x-msvideo) expected_ext="avi" ;;
video/x-matroska)expected_ext="mkv" ;;
*) return 0 ;;
esac
actual_ext="${filepath##*.}"
actual_ext=$(echo "$actual_ext" | tr '[:upper:]' '[:lower:]')
[ "$actual_ext" == "$expected_ext" ] && return 0
new_path="${filepath%.*}.$expected_ext"
if [ -f "$new_path" ]; then
echo "[WARN] Cannot rename $filepath (target exists)"
return 1
fi
mv "$filepath" "$new_path" || return 1
echo "[FIX] $filepath -> $new_path"
FIX_RESULT="$new_path"
return 0
}
for json in *.json; do
[ -f "$json" ] || continue
base="${json%.supple*.json}"
[ "$base" == "$json" ] && base="${json%.json}"
img=$(find_image "$base")
if [ -z "$img" ]; then
echo "[SKIP] No image: $base"
((skipped++))
continue
fi
FIX_RESULT=""
fix_extension "$img"
fix_rc=$?
if [ $fix_rc -eq 0 ] && [ -n "$FIX_RESULT" ]; then
img="$FIX_RESULT"
elif [ $fix_rc -eq 1 ]; then
((skipped++))
continue
fi
lat=$(grep -o '"latitude": [0-9.-]*' "$json" | head -1 | grep -o '[0-9.-]*')
lon=$(grep -o '"longitude": [0-9.-]*' "$json" | head -1 | grep -o '[0-9.-]*')
alt=$(grep -o '"altitude": [0-9.-]*' "$json" | head -1 | grep -o '[0-9.-]*')
ts=$(grep -o '"timestamp": "[0-9]*"' "$json" | sed -n '2p' | grep -o '[0-9]*')
args=("-overwrite_original" "-n")
if [ -n "$lat" ] && [ -n "$lon" ] && [ "$lat" != "0.0" ]; then
args+=("-GPSLatitude=$lat" "-GPSLongitude=$lon")
[ "$(echo "$lat >= 0" | bc)" -eq 1 ] && args+=("-GPSLatitudeRef=N") || args+=("-GPSLatitudeRef=S")
[ "$(echo "$lon >= 0" | bc)" -eq 1 ] && args+=("-GPSLongitudeRef=E") || args+=("-GPSLongitudeRef=W")
fi
if [ -n "$alt" ] && [ "$alt" != "0.0" ]; then
args+=("-GPSAltitude=$alt")
fi
if [ -n "$ts" ] && [ "$ts" -gt 0 ]; then
datetime=$(date -r "$ts" '+%Y:%m:%d %H:%M:%S' 2>/dev/null || date -d "@$ts" '+%Y:%m:%d %H:%M:%S' 2>/dev/null)
[ -n "$datetime" ] && args+=("-DateTimeOriginal=$datetime" "-CreateDate=$datetime" "-ModifyDate=$datetime")
fi
echo "[PROC] $img"
if exiftool "${args[@]}" "$img"; then
((processed++))
rm "$json"
else
((skipped++))
fi
done
echo ""
echo "================================"
echo "Processed: $processed"
echo "Skipped: $skipped"
echo "================================"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment