Skip to content

Latest commit

 

History

History
166 lines (116 loc) · 9.58 KB

File metadata and controls

166 lines (116 loc) · 9.58 KB

google-photos-takeout-date-fixer

A fork of google-photos-exif by mattwilson1024.

A tool to populate missing DateTimeOriginal EXIF metadata in Google Photos takeout, using Google's JSON metadata, and update file modification time.

Table of Contents

Quick Start

Example usage:

yarn
yarn start ~/takeout

Differences with google-photos-exif

  • The updates to the files are done in place. No files are moved, copied or deleted. This is because

Background

From google-photos-exif:

I wrote this tool to help me overcome some issues that I had when trying to make use of photos exported from Google Photos using Google Takeout.

My goal was to extract all photos from my Google Photos account and incorporate them into a master photo library on my Mac. This library would be organised into a date-based folder structure, with images being automatically moved into the correct structure using Silent Sifter.

Silent Sifter provides a fast way to organise images into folders based on the timestamps embedded in the image metadata or failing that, the file modification timestamps.

Whilst it is great that I was able to use Google Takeout to extract all of my stored images from Google Photos at once, I found that some images were landing in the wrong place due to missing DateTimeOriginal EXIF timestamps.

This tool aims to eliminate some of those issues by reading the photoTakenTime timestamp from the JSON metadata files that are included in Google Takeout export and using it to:

  • set a meaningful modification date on the file itself
  • populate the DateTimeOriginal field in the EXIF metadata if this field is not already set

Structure of Google Takeout export

At the time of writing (December 2020), Google Takeout provides you with one or more zip files.

Extracting the zip, you might find something similar to this:

Extracted Takeout Zip
  Google Photos
    Photos from 2016
      IMG0305.jpg.json
    Photos from 2020
      IMG1001.jpg
      IMG1001.jpg.json
      IMG1002.jpg.json
      metadata.json
      IMG1003.jpg
      IMG1003.json
    SomeAlbumName
      IMG1004.jpg
      IMG1004.jpg.json
      IMG1005.jpg
      IMG1005.json
  archive_browser.html

There are some interesting challenges to note here:

  1. Each zip contains folders for certain dates and/or album names. These folders contain a mixture of image files and JSON metadata files. The JSON sidecar files include, amongst other things, a useful photoTakenTime property.
  2. The date based folders don't always contain perfect pairs of images and JSON files, sometimes you get JSON files without a corresponding image. In the case that the export was split across multiple zips, I'm not sure whether there is any guarantee that the images & JSON files will always be co-located within the same export
  3. The naming convention for the JSON files seems inconsistent and has some interesting edge cases. For an image named IMG123.jpg, sometimes you get IMG123.jpg.json but sometimes it's just IMG123.json
  4. From what I can tell, the embedded metadata (e.g. EXIF / IPTC) in the image files is not updated if changes are made within Google Photos, for example if the dates are updated using the Google Photos UI. Instead, Google's metadata comes out in the accompanying JSON files.
  5. Whilst most of my images contained reasonable EXIF timestamps for the time they were taken (written by the phone's camera), a small number did not. My guess is that these images originated from other sources (e.g. they were shared with me or imported into the library by other means, and the source did not include a timestamp in the EXIF metadata)
  6. Some file formats such as GIFs or MP4 videos don't have this metadata and thus also get sorted into the wrong place when run through tools that organise images based on the metadata timestamps.

How to download Google Takeout content

The first step to using this tool is to request & download a Google Takeout. At the time of writing the steps to do this are:

  1. Visit https://takeout.google.com/
  2. Deselect all products and then tick Google Photos
  3. Click All photo albums included.
  4. Keep all of the date-based albums selected. Deselect any "Hangout: *" albums unless you specifically want to include images from chats.
  5. If you have custom albums (ones with non-date names), your images will already be in the date-based albums. Therefore, you will end up with duplicates. Important: It is unclear if photos added by other users to a shared album are included in the Takeout.
  6. Click OK and move to the next step
  7. Select "Export once"
  8. Under "File type & size" I recommend increasing the file size to 50GB. Important: If your collection is larger than this (or you need to export it as multiple smaller archives) then you will need to merge the resultant folders together manually before using this tool. If you do this, be sure to merge the contents of any directories with the same name, rather than overwriting them.
  9. Click "Create Export", wait for a link to be sent by email and then download the zip file
  10. Extract the zip file into a directory. The path of this directory will be what we pass into the tool.

Supported file types

This tool currently only updates the following "media file" types.

  • .jpg
  • .jpeg
  • .heic
  • .gif
  • .mp4
  • .m4v

What does the tool do?

The tool will do the following:

  1. Find all "media files" with one of the supported extensions listed above from the (nested) takeoutDir folder structure.

  2. For each "media file":

    a. Look for a corresponding sidecar JSON metadata file (see the section below for more on this) and if found, read the photoTakenTime field.

    b. If the file supports EXIF (e.g. JPEG images), read the EXIF metadata and write the DateTimeOriginal field if it does not already have a value in this field.

    c. Update the file modification date to the photoTakenTime found in the JSON metadata or to the DateTimeOriginal field from EXIF.

  3. Display a summary of work completed

How are media files matched to JSON sidecar files?

The Google Takeout file/folder structure has some interesting inconsistencies/quirks which make it tricky to work with.

This tool makes a best-effort attempt to work with this structure. Read on to get a feel for how it works.

Images have corresponding JSON files

Imagine you have an image named foo.jpg.

In the same directory, we also expect to find a JSON file containing metadata about the image. The pattern for how this is names seems inconsistent but in general, we seem to get either: foo.json or foo.jpg.json. This tool will look for either of those two files.

Edited images (e.g. "foo-edited.jpg") don't have their own JSON files

It appears that images that were edited using Google Photos (e.g. rotated or edited on a mobile device) don't get their own JSON sidecar files.

For example, for image file foo-edited.jpg we don't get foo-edited.json nor foo-edited.jpg.json. Instead we must rely on the JSON file from the original image.

To work around this, for any images with a suffix of -edited, this tool will ignore that suffix and look for the JSON file from the original image, e.g. foo.json or foo.jpg.json.

Files with numbered suffixes (e.g. "foo(1).jpg") follow a different pattern for the naming of JSON files

I found that some of my images were named with a numeric suffix, such as foo(1).jpg.

Counter-intuitively, the corresponding JSON file for this doesn't follow the same pattern. Instead of foo(1).json or foo(1).jpg.json, it is instead named foo.jpg(1).json.

To support that, this tool will also check for files that have a number suffix in brackets and where that is the case, look for a JSON file with the pattern above.

Disclaimer

This tool was only written for the purpose of solving my own personal requirements.

I decided to make this public on GitHub because:

  • it was useful for me, so maybe it'll be useful for others in the future
  • future me might be thankful if I ever need to do this again

With that said, please bear in mind that this tool won't be actively maintained and your mileage may vary. I'm sure it's far from perfect so if you choose to use it please proceed with caution and be careful to verify the results! I hope it's helpful.