I recently used Google Takeout to get a copy of all the photos and videos I had backed up there. I discovered, to my horror, that the files’ timestamps were all wrong. I created a short script using bash and jq to rectify the situation.
- Use https://takeout.google.com/ to get a copy of your photos and videos
- Unpack the contents in a directory
- Copy the below script in it
- Run it
- That’s it.
The idea behind the script is that Google keeps the original image or video’s information in a .json file named after the file.
In it, there are two pieces of important information:
- the timestamp at which the picture/video was taken or uploaded
- the related filename
Using that information, and a little date and touch magic, it’s possible to ensure the files have a proper modification time.
This then ensures they show up properly using systems which use the file timestamp as an indication as to when it was taken.
Here’s the script, in all its glory:
#!/bin/bash
set -e
while read -r json_file; do
dirname=$(dirname "$json_file")
picture_ts=$(jq -r '.photoTakenTime|.timestamp' "$json_file")
filename=$(jq -r '.title' "$json_file")
if [[ "$filename" != "null" ]]; then
filename="$dirname/$filename"
if [[ -e "$filename" ]]; then
touch_string=$(date -d "@$picture_ts" +%Y%m%d%H%M)
ls -la "$filename"
touch -m -t "$touch_string" "$filename"
ls -la "$filename"
else
>&2 echo "Cannot find $filename referenced by $json_file"
fi
fi
done < <( find ./ -name '*.json' )