Created
August 10, 2024 21:50
-
-
Save dotysan/05d16fc997f6ada783343a7cf366291a to your computer and use it in GitHub Desktop.
After extracting Takeout/ run this to organize video & images
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 | |
# | |
# Sets the file modification time to that of the filename from a | |
# Google Takeout export of Nest videos and images. | |
# | |
set -o errexit | |
set -o pipefail | |
set -o nounset | |
#set -o xtrace | |
main() { | |
vidmtimes | |
imgmtimes | |
} | |
vidmtimes() { | |
local f l yr mo dy hr mn sc ftime mtime | |
find -type f -regextype posix-extended -regex \ | |
'.*/[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}\.mp4' \ | |
|while read -r f | |
do echo "$f" | |
l="${#f}" | |
yr="${f:l-23:4}" | |
mo="${f:l-18:2}" | |
dy="${f:l-15:2}" | |
hr="${f:l-12:2}" | |
mn="${f:l-9:2}" | |
sc="${f:l-6:2}" | |
ftime="$yr$mo$dy$hr$mn.$sc" | |
mtime=$(stat -c%Y "$f") | |
mtime=$(date -d "@$mtime" '+%Y%m%d%H%M.%S') | |
if [[ $ftime != $mtime ]] | |
then touch -t "$ftime" "$f" | |
fi | |
done | |
} | |
imgmtimes() { | |
# proper logic to lookup the mtime from the metadata | |
local js id seconds | |
local f l session_id end_time mtime d yr mo | |
find -type f -name event_sessions.json \ | |
|while read -r js | |
do echo "$js" | |
pushd "${js::-20}" | |
# speed up by running jq once into associative array | |
local -A endtimes | |
while IFS=$'\t' read -r id seconds | |
do endtimes["$id"]="$seconds" | |
done < <(jq -r '.[]|[.session_id,.end_time.seconds]|@tsv' event_sessions.json) | |
find -type f -regextype posix-extended -regex \ | |
'.*/[0-9]{10}\.jpeg' \ | |
|while read -r f | |
do echo "$f" | |
l="${#f}" | |
session_id="${f:l-15:10}" | |
#end_time=$(getendtime "$session_id") | |
# above is way to slow; use array instead | |
end_time=${endtimes["$session_id"]} | |
mtime=$(stat -c%Y "$f") | |
if [[ $end_time != $mtime ]] | |
then | |
d=$(date -d "@$end_time" '+%Y%m%d%H%M.%S') | |
touch -t "$d" "$f" | |
yr="${d::4}" | |
mo="${d:4:2}" | |
mkdir -p "$yr-$mo" | |
mv "$f" "$yr-$mo" ||: # don't care if already in subdir | |
fi | |
done | |
popd | |
done | |
} | |
getendtime() { | |
jq " | |
[ | |
.[] | |
|select(.session_id==\"$1\") | |
|.end_time.seconds | |
] | |
|unique | |
|.[0] | |
" event_sessions.json | |
} | |
oldimgmtimes() { | |
# this was broken logic as the timestamp isn't the filenam | |
local f l s m d | |
find -type f -regextype posix-extended -regex \ | |
'.*/[0-9]{10}\.jpeg' \ | |
|while read -r f | |
do echo "$f" | |
l="${#f}" | |
s="${f:l-15:10}" | |
m=$(stat -c%Y "$f") | |
if [[ $s != $m ]] | |
then | |
d=$(date -d "@$s" '+%Y%m%d%H%M.%S') | |
touch -t "$d" "$f" | |
fi | |
done | |
} | |
main | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment