Files
DanceVideos/program1/metadata.sh

99 lines
2.7 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# === CONFIGURATION ===
# === Utilitaires ===
esc() { printf '%s' "$1" | sed 's/"/""/g'; }
export -f esc
safe_weekday() {
local iso="$1"
python3 - <<PY
import sys
from datetime import datetime
s = "$iso"
try:
dt = datetime.fromisoformat(s)
print(dt.strftime('%A'))
except Exception:
print("Unknown")
PY
}
export -f safe_weekday
get_creation_time() {
local f="$1"
local ct=""
for tag in CreateDate MediaCreateDate TrackCreateDate; do
ct=$(exiftool -"$tag" -d "%Y-%m-%dT%H:%M:%S" -s3 "$f" 2>/dev/null || true)
[ -n "$ct" ] && break
done
if [ -z "$ct" ]; then
ct="$(date -r "$f" +"%Y-%m-%dT%H:%M:%S")"
fi
echo "$ct"
}
export -f get_creation_time
get_duration() {
local f="$1"
ffprobe -v error -show_entries format=duration \
-of default=noprint_wrappers=1:nokey=1 "$f" 2>/dev/null || echo ""
}
export -f get_duration
get_gps() {
local f="$1"
local lat lon
lat="$(exiftool -GPSLatitude -n -s3 "$f" 2>/dev/null || true)"
lon="$(exiftool -GPSLongitude -n -s3 "$f" 2>/dev/null || true)"
echo "$lat|$lon"
}
export -f get_gps
reverse_geocode() {
local lat="$1" lon="$2"
local json="" postal city district
if [ -n "$lat" ] && [ -n "$lon" ]; then
json="$(curl -s "https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lon}&zoom=16&addressdetails=1")"
# Extraction sécurisée avec jq
postal=$(echo "$json" | jq -r '.address.postcode // ""')
city=$(echo "$json" | jq -r '.address.city // .address.town // .address.village // ""')
district=$(echo "$json" | jq -r '.address.neighbourhood // .address.suburb // .address.residential // .address.village // .address.municipality // ""')
# Concaténation formatée, ignorer champs vides
formatted=""
[ -n "$postal" ] && formatted="$formatted$postal"
[ -n "$city" ] && formatted="${formatted:+$formatted, }
$city"
[ -n "$district" ] && formatted="${formatted:+$formatted, }
$district"
echo "$formatted" | tr '\n' ' ' | tr -s ' ' ' ' | sed 's/ *$//'
else
echo ""
fi
}
export -f reverse_geocode
process_video() {
local f="$1"
local ct weekday duration lat lon address
ct="$(get_creation_time "$f")"
weekday="$(safe_weekday "$ct")"
duration="$(get_duration "$f")"
IFS="|" read -r lat lon <<<"$(get_gps "$f")"
address="$(reverse_geocode "$lat" "$lon")"
echo "$ct|$weekday|$duration|$lat|$lon|$address"
# --- CSV row ---
# echo "\"$(basename "$f")\",\"$(esc "$f")\",\"$(esc "$ct")\",\"$(esc "$weekday")\",\"$(esc "$duration")\",\"$(esc "$lat")\",\"$(esc "$lon")\",\"$(esc "$formatted")\""
}
export -f process_video