Skip to content

Instantly share code, notes, and snippets.

@jhollinger
Created February 5, 2023 21:06
Show Gist options
  • Save jhollinger/cad1b3260acdc3f2ea975ebc8d9dd263 to your computer and use it in GitHub Desktop.
Save jhollinger/cad1b3260acdc3f2ea975ebc8d9dd263 to your computer and use it in GitHub Desktop.
Extract photos/video from a Google Takeout tarball
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -ne 2 ]] || [[ ! -f "$1" ]] || [[ ! -d "$2" ]]; then
echo "usage: extract-google-photos [tarfile] [dest-dir]"
exit 1
fi
if ! command -v exiftool > /dev/null || ! command -v gawk > /dev/null; then
echo "Please install exiftool and gawk"
exit 1
fi
[[ "$1" == /* ]] && tarfile="$1" || tarfile="$(pwd)/$1"
[[ "$2" == /* ]] && dest="$2" || dest="$(pwd)/$2"
tmpdir=$(mktemp -d)
echo "Working dir ${tmpdir}"
cd $tmpdir
tar -xzf "$tarfile"
find . -type f -regex '.+\(\(\.jpg\)\|\(\.JPG\)\|\(\.mp4\)\|\(\.m4v\)\)' | while read src_file; do
file_name=$(basename "$src_file")
[[ "$file_name" == .* ]] && continue
subdir=$(exiftool -T -createdate "$src_file" | gawk -F: '/^[0-9]{4}:[0-9]{2}/ { printf "%s/%s", $1, $2 }')
[[ -z "$subdir" ]] && subdir="Unknown"
dest_dir="${dest}/${subdir}"
dest_file="${dest_dir}/${file_name}"
[[ ! -d "$dest_dir" ]] && mkdir -p "$dest_dir"
if [[ -f "$dest_file" ]]; then
[[ $(md5sum "$src_file") == $(md5sum "$dest_file") ]] && continue
while [[ -f "$dest_file" ]]; do
dup_file_name=$(basename "$dest_file")
dest_file="${dest_dir}/2${dup_file_name}"
done
fi
mv "$src_file" "$dest_file"
done
cd ..
rm -r $tmpdir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment