Created
January 30, 2021 01:36
-
-
Save pirate/b5444b1b0de5870e92a43e9708130232 to your computer and use it in GitHub Desktop.
Rename a bunch of movie files to include the GPS location and recorded date from EXIF metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Requires: exiftool, mediainfo (install via apt/brew first) | |
# Usage: | |
# $ cd ~/Videos | |
# $ ./rename_videos.sh | |
# GP013838.MP4 -> 2018-10-12_08-59-59__Montreal_QC_Canada__GP013838.mp4 | |
# ... | |
GOOGLE_MAPS_API_KEY="yourkeyhere" | |
ls *.{mov,MOV,mp4,MP4} \ | |
| while read oldname; do | |
# lowercase the extensions and replace spaces with underscores | |
newname="$(echo "$oldname" | perl -pe 's/ /_/gm' | perl -pe 's/MOV/mov/gm' | perl -pe 's/MP4/mp4/gm')" | |
# get the recording date from EXIF metadata | |
recorded_date="$( | |
mediainfo "$oldname" \ | |
| grep "Encoded date" \ | |
| sort -u \ | |
| perl -pe 's/:/-/gm' \ | |
| awk '{print $5"__"$6}' | |
)" | |
# get the created date from filesystem attributes | |
created_date="$( | |
stat "$oldname" \ | |
| grep 'Modify:' \ | |
| perl -pe 's/Modify: (....-..-.. ..:..:..)\..+ .+$/$1/gm' \ | |
| perl -pe 's/ /__/gm' \ | |
| perl -pe 's/:/-/gm' | |
)" | |
# Get the GPS coordinates from EXIF metadata | |
latlng="$( | |
exiftool -n -p '$GPSLatitude,$GPSLongitude' $oldname 2>&1 \ | |
| grep -v 'not defined' \ | |
| grep -v 'Warning' \ | |
| grep -v 'image files read' | |
)" | |
# Get the city_province_country from the GPS coordinates | |
if [[ "$latlng" ]]; then | |
location="$( | |
curl -X GET -s \ | |
-H "Content-Type: application/json" \ | |
"https://maps.googleapis.com/maps/api/geocode/json?latlng=${latlng}&key=$GOOGLE_MAPS_API_KEY" \ | |
| grep 'compound_code' \ | |
| head -n 1 \ | |
| perl -pe 's/(.+) (.+), (..), (.+).,/$2_$3_$4/gm' | |
)" | |
fi | |
# build the new $DATE__$LOCATION__$OLDNAME filename | |
if [[ "$location" ]]; then | |
newname="${location}__${newname}" | |
fi | |
if [[ "$recorded_date" ]]; then | |
newname="${recorded_date}__${newname}" | |
elif [[ "$created_date" ]]; then | |
newname="${created_date}__${newname}" | |
fi | |
if [[ "$newname" ]] && [[ "$newname" != "$oldname" ]]; then | |
echo "$oldname -> $newname" | |
# mv "$oldname" "$newname" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment