doersino / apple-photos-export

An Apple Photos export script.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

apple-photos-export

An Apple Photos export script.

Please note that apple-photos-export.py has been written to fit my (perhaps uncommon) use case. It's only been tested for photos imported into an Apple Photos library via USB from an iPhone – I haven't yet tried how importing media from other sources or using iCloud changes things. I don't use Apple Photos for anything else. My iPhone has always been set to use the HEIC format, Live Photos have always been enabled and for HDR photos, the non-HDR variant is also stored. Lastly, I've got an iPhone 7 – so the script might not work for Portrait mode photos. Not much care was taken to make it particularly useful to anyone else. Most notably, it's not an all-purpose backup tool (I don't think one exists). Continue reading to find out what exactly it does.

Note that I'm stuck on Mojave, i.e. Photos 4.0, for the time being – this script won't on Catalina or Big Sur without significant modifications. I recommend using Rhet Turnbull's osxphotos instead (in fact, I might switch to it once I upgrade).

πŸ› Since I've done exactly that after finally updating my operating system, I've archived this repository. Perhaps it'll continue to be useful as a reference. Cheers!


Setup and usage

  1. Install exiftool and python3.
  2. Make sure sips is working (this should be included in your macOS installation).
  3. pip3 install configfile.
  4. Copy apple-photos-export.ini.example to apple-photos-export.ini in your desired target path (and fill in the details).

With this setup work out of the way, all that's left is to plug your iPhone into your MacBook, import all (or some) photos and run:

python3 apple-photos-export.py TARGET [-v]

This will read the config file, export all media [to a location within the depths of /tmp and only upon your confirmation copy them] to the TARGET, structured as shown below. Additionally, a cache file TARGET/apple-photos-export.json containing a record of already-exported photos and some metadata will be created.

TARGET
β”œβ”€β”€ 2018/
β”‚   β”œβ”€β”€ 09_September/
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-02-04_3_selfie_IMG_0001.heic                        # Taken with front-facing camera and...
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-02-04_3_selfie_IMG_0001.jpg                         # ...generated JPEG version and...
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-02-04_3_selfie_jpegvideocomplement_1.mov            # ...matching Live Photo video.
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-19-47_6_slomo_IMG_0004.mov                          # Slomo (recorded at O(many) fps, played at same frame rate, so doesn't appear to be a slomo) and...
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-19-47_6_slomo_rendered_fullsizeoutput_35d.mov       # ...rendered version (part of the video is actually slowed down).
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-20-12_7_IMG_0005.mov                                # Video.
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-20-39_8_panorama_IMG_0008.heic                      # Panorama and...
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-20-39_8_panorama_IMG_0008.jpg                       # ...generated JPEG.
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-20-47_9_panorama_IMG_0009.heic
β”‚   β”‚   β”œβ”€β”€ 2018-09-22_09-20-47_9_panorama_IMG_0009.jpg
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_15-47-58_24_IMG_0025.heic                              # Normal photo and...
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_15-47-58_24_IMG_0025.jpg                               # ...generated JPEG version and...
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_15-47-58_24_jpegvideocomplement_e.mov                  # ...matching Live Photo video.
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_15-48-33_25_slomo_IMG_0026.mov
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_15-48-33_25_slomo_rendered_fullsizeoutput_363.mov
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_15-49-15_26_slomo_IMG_0027.mov
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_15-49-15_26_slomo_rendered_fullsizeoutput_36b.mov
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_17-31-12_27_IMG_0028.heic
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_17-31-12_27_IMG_0028.jpg
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_17-31-12_27_jpegvideocomplement_f.mov
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_17-31-50_28_IMG_0029.heic
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_17-31-50_28_IMG_0029.jpg
β”‚   β”‚   β”œβ”€β”€ 2018-09-23_17-31-50_28_jpegvideocomplement_10.mov
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-33-44_53_IMG_0056.heic
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-33-44_53_IMG_0056.jpg
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-33-44_53_jpegvideocomplement_23.mov
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-25_54_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0057.jpg  # Burst...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-25_55_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0058.jpg  # ...photo...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-25_56_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0059.jpg  # ...from...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-25_57_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0060.jpg  # ...same...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-26_58_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0061.jpg  # ...burst...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-26_59_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0062.jpg  # ...as...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-26_60_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0063.jpg  # ...all...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-26_61_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0064.jpg  # ...of...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-26_62_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0065.jpg  # ...these...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-26_63_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0066.jpg  # ...other...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-26_64_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0067.jpg  # ...burst...
β”‚   β”‚   β”œβ”€β”€ 2018-09-28_04-34-26_65_burst_EAycz5MaTtGjHW7d3PEvXw_IMG_0068.jpg  # ...photos.
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ 10_October/
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”‚   β”œβ”€β”€ 2018-10-21_15-51-15_269_IMG_0271.heic
β”‚   β”‚   β”œβ”€β”€ 2018-10-21_15-51-15_269_IMG_0271.jpg
β”‚   β”‚   β”œβ”€β”€ 2018-10-21_15-51-15_269_jpegvideocomplement_b3.mov
β”‚   β”‚   β”œβ”€β”€ 2018-10-21_15-51-15_270_instagram_IMG_0272.jpg                    # Edited using Instagram.
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   └── ...
β”œβ”€β”€ 2019/
β”‚   └── ...
β”œβ”€β”€ apple-photos-export.ini   # Settings.
└── apple-photos-export.json  # Cache.

The (not) very interesting backstory

Back in the olden days, when I was using an Android-powered Nexus 5 and Dropbox's "Camera Uploads" feature, everything was great:

  1. The phone would save photos (whether HDR or not) as .jpg, videos as .mp4 and screenshots as .png.
  2. Dropbox would continuously collect them and they'd end up on a folder on my laptop (with more or less sensible, time-based filenames), from where I could periodically archive them to a big external disk (and a backup, of course).

Then I got myself an iPhone, which – in addition to "normal" photos and videos – takes Live Photos, for which Dropbox only uploads the "base" photo. To make matters worse, some apps such as WhatsApp store received images in the camera roll, which messes everything up unless some filtering is done. In a futile attept to future-proof things (and for portability), I thought it'd be neat to generate a JPEG version of all HEIC files.

Wanting to keep my previous archival scheme running (and having it be complete, i.e. also containing the short videos corresponding to live photos), I've come up with the following workflow:

  1. Connect the iPhone to my MacBook via USB.
  2. Import all new photos into Photos.app.
  3. ??? (this is where apple-photos-export.py comes in).
  4. PROFIT!!!

Notes on photos.db and the directory structure of ~/Pictures/Apple Photos.photoslibrary

Current as of February 2019 (iOS 12.1.2, macOS 10.14.2 Mojave, Photos 4.0).

In order to write apple-photos-export.py, I needed to reverse-engineer how Apple Photos stores and keep track of photos. Initially, this promised to be a piece of cake since Photos, inside the Photos Library.photoslibrary, uses an SQLite database Photos Library.photoslibrary/database/photos.db to keep track of the run-of-the-mill files it imports.

Upon further investigation, this proved a bit frustrating since the database really doesn't seem to contain much of the detail needed to get to the Live Photo videos and, to a lesser degree, discern different types of media – and what's more, it's not consistent in terms of setting a consistent set attributes to consistent values for a given kind of media. No idea how Apple Photos itself deals with this – but my solutions to these issues are encoded in apple-photos-export.py. The following SQL query gives sort of an overview:

SELECT modelId,               -- ID
       imagePath,             -- Absolute path to the base image.
       fileModificationDate,  -- Useful for matching slomo videos with rendered variants auto-generated by Photos.
       mediaGroupId,          -- Corresponds to the ContentIdentifier EXIF key in Live Photo videos, required for matching.
       groupingUuid           -- NULL for panoramas and squares, among others.
       burstUuid,             -- If set, we're dealing with a photo taken in burst mode (this allows grouping of bursts; also RKVersion contains a column burstPickType which I think indicates the best picture of a given burst).
       UTI,                   -- File type, commonly one of: public.heic, public.jpeg (WhatsApp/burst/panorama), com.apple.quicktime-movie, public.png, public.mpeg-4 (WhatsApp videos).
       importGroupUuid,       -- The import group (each time you import some pictures into Photos, an import group is created) the picture is part of. Allows trivially ignoring previously-exported imports for a significant speedup.
       hasAttachments         -- Indicates whether Photos has created a rendered slomo video or if you've performed any edits to the photo.
FROM RKMaster                 -- Most important table, also worth taking a look at: RKVersion, RKAttachment.

A commented tree view of the directory structure of Photos Library.photoslibrary:

Photos Library.photoslibrary
β”œβ”€β”€ Attachments/          # Not-really-useful metadata for adjustments.
β”‚   └── ...
β”œβ”€β”€ Masters/              # Master (original, un-edited) photos, organized in subdirectories according to import group dates.
β”‚   β”œβ”€β”€ 2018/
β”‚   β”‚   └── 12/
β”‚   β”‚       └── 28/
β”‚   β”‚           └── 20181228-132551/
β”‚   β”‚               β”œβ”€β”€ IMG_0001.HEIC
β”‚   β”‚               β”œβ”€β”€ IMG_0002.HEIC
β”‚   β”‚               └── ...
β”‚   └── 2019/
β”‚       └── ...
β”œβ”€β”€ database/             # Database and database-related files.
β”‚   β”œβ”€β”€ photos.db
β”‚   └── ...
β”œβ”€β”€ private/
β”‚   └── ...
└── resources/
    β”œβ”€β”€ media/
    β”‚   β”œβ”€β”€ face/         # Extracted (and partially somewhat distorted) faces in image form (Photos might take a few days of background processing to populate this directory).
    β”‚   β”‚   └── ...
    β”‚   β”œβ”€β”€ master/       # Live Photo videos (among some other stuff, like JPEG versions of screenshots).
    β”‚   β”‚   β”œβ”€β”€ 00/
    β”‚   β”‚   β”‚   └── 00/
    β”‚   β”‚   β”‚       β”œβ”€β”€ fullsizeoutput_fe.jpeg
    β”‚   β”‚   β”‚       β”œβ”€β”€ ...
    β”‚   β”‚   β”‚       β”œβ”€β”€ jpegvideocomplement_1.mov
    β”‚   β”‚   β”‚       β”œβ”€β”€ jpegvideocomplement_10.mov
    β”‚   β”‚   β”‚       β”œβ”€β”€ jpegvideocomplement_11.mov
    β”‚   β”‚   β”‚       β”œβ”€β”€ ...
    β”‚   β”‚   β”‚       └── jpegvideocomplement_ff.mov
    β”‚   β”‚   └── ...
    β”‚   β”œβ”€β”€ t/
    β”‚   β”‚   └── ...
    β”‚   └── version/      # Rendered slomo videos and rendered versions of edited photos.
    β”‚       β”œβ”€β”€ 00/
    β”‚       β”‚   └── 00/
    β”‚       β”‚       β”œβ”€β”€ fullsizeoutput_16.jpeg
    β”‚       β”‚       └── fullsizeoutput_22.jpeg
    β”‚       β”œβ”€β”€ 03/
    β”‚       β”‚   └── 00/
    β”‚       β”‚       β”œβ”€β”€ fullsizeoutput_35d.mov
    β”‚       β”‚       β”œβ”€β”€ fullsizeoutput_363.mov
    β”‚       β”‚       └── fullsizeoutput_36b.mov
    β”‚       └── 05/
    β”‚           └── 00/
    β”‚               β”œβ”€β”€ fullsizeoutput_521.jpeg
    β”‚               └── videocomplementoutput_522.mov
    β”œβ”€β”€ moments/          # Some .plist files, nothing much useful.
    β”‚   └── ...
    β”œβ”€β”€ projects/
    β”œβ”€β”€ proxies/
    β”‚   └── derivatives/  # Thumbnails.
    β”‚       └── ...
    β”œβ”€β”€ recovery/         # Database backups in some weird format, I think.
    β”‚   β”œβ”€β”€ Info.plist
    β”‚   β”œβ”€β”€ RKAdjustmentData/
    β”‚   β”‚   └── 0000000000.lij
    β”‚   └── ...
    └── segments/
        └── ...

Related work

Future work

About

An Apple Photos export script.

License:MIT License


Languages

Language:Python 89.7%Language:Makefile 10.3%