Renaming images with wrong extensions including meta data files (Google Takeout)

Started by alexander.willner, April 14, 2022, 06:29:47 PM

Previous topic - Next topic

alexander.willner

Hi there,

my goal is to rename images with wrong file extensions (which is easy) but including their meta data files (json) from Google Takeout.

To rename the files I use:





exiftool -ext jpg -ext jpeg -ext bmp -ext png -r -ignoreMinorErrors '-FileName<%f.${FileTypeExtension;$_=uc}' $1


To rename the meta data files I execute the following ugly, inefficient and error prone (but nevertheless working) code:



find $1 -iname "*.json" -exec \
  bash -c 'FILENAME="{}" ; echo $FILENAME ; \
    FILE="$(echo ${FILENAME%.*}|uconv -x any-nfc)" ; \
    EXT="$(echo ${FILE##*.}|uconv -x any-nfc)" ; \
    BASEFILE="$(echo ${FILE%.*}|uconv -x any-nfc)" ; \
    FILENAME2="$(ls "$BASEFILE".???)" ; \
    [ ! -f "$BASEFILE.$EXT" ] && mv "$BASEFILE.$EXT.JSON" "$BASEFILEFILE.${FILENAME2#*.}.json" ; ' \;




I'm sure there is a better way to do this by extending the first code sipped, but I couldn't figure it out.


Best regards, Alex

Phil Harvey

Hi Alex, you could try these 2 commands:

1. exiftool -ext jpg -ext jpeg -ext bmp -ext png -r -ignoreMinorErrors '-FileName<%f.$FileTypeExtension#.json' -srcfile %d%f.%e.json $1

2. exiftool -ext jpg -ext jpeg -ext bmp -ext png -r -ignoreMinorErrors '-FileName<%f.$FileTypeExtension#' $1

Use the first command to rename corresponding .json files before renaming the images.

Note that I'm using $FileTypeExtension# to get the upper case for the extension instead of using the advanced formatting as you have done (see the Extra Tags documentation).

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

alexander.willner

Hey Phil,

wonderful, that worked. Thanks to this and a number of similar questions/answers on this forum, I'm now using the following workflow to pre-process Google Takeout images before importing them into a photo management application (still not perfect but a good direction):


#!/usr/bin/env bash

if [ ! -r "${1:-}" ]; then
    echo >&2 "USAGE: $0 <dir or file>"
    exit 1
fi

echo -n "Fixing file names..."
echo "(In Google Takeout some files with brackets are not named correctly)"
exiftool -ext json -r \
    -if '$Filename=~/(\.[^.]+)(\(\d+\)).json$$/i' \
        '-Filename<${Filename;s/(\.[^.]+)(\(\d+\)).json$/$2$1.json/}' \
    "$1"

echo -n "Adding missing file extension to meta data files..."
echo "(In Google Takeout some meta data files do not follow the form '\$ext.json')"
find "$1" \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.png" -o -iname "*.3gp" -o -iname "*.mp4" -o -iname "*.gif" -o -iname "*.avi" \) -print0 | \
  while IFS= read -r -d '' file; do [ -f "${file%.*}.json" ] && mv "${file%.*}.json" "$file.json" ; done

echo -n "Fixing file extensions..."
echo "(In Google Takeout some files have the wrong file extensions)"
exiftool \
    -r -ext jpg -ext jpeg -ext bmp -ext 3gp -ext png -ext mp4 -ext gif -ext avi \
    '-FileName<%f.$FileTypeExtension#.json' -srcfile %d%f.%e.json \
    "$1"
exiftool \
    -r -ext jpg -ext jpeg -ext bmp -ext 3gp -ext png -ext mp4 -ext gif -ext avi \
    '-FileName<%f.$FileTypeExtension#' \
    "$1"


echo -n "Only fixing date time original if not set correctly..."
echo "(Otherwise importing files into an application might result in duplicates or with a wrong date)"
exiftool \
    -r -ext jpg -ext jpeg -ext bmp -ext 3gp -ext png -ext mp4 -ext gif -ext avi \
    -overwrite_original -ignoreMinorErrors \
    '-OffsetTimeOriginal=+02:00' \
    -tagsfromfile "%d%f.%e.json" -d "%s" -if 'not defined $datetimeoriginal or $datetimeoriginal eq "" or $datetimeoriginal eq "0000:00:00 00:00:00"' '-datetimeoriginal<${PhotoTakenTimeTimestamp#;$_+=7200}' \
    "$1"