PetrVys / MotionPhoto2

Mux HEIC and JPG Live Photos into Google/Samsung Motion Photos
MIT License
43 stars 6 forks source link

MotionPhoto2

A small script to create Motion Photo v2/v3 from HEIC or JPG files and videos. Resulting files appear to be compatible with Google Photos and Samsung Gallery as a motion/live photo.

In case the source is an iPhone Live Photo, the presentation timestamp will be migrated as well, thus the photo will start from the same keyframe.

Photos are created to mimic the way Galaxy S23 Ultra and Tab S9 phones create HEIC and JPG motion photos. This format internally refers to itself as mpv2 (and recently also mpv3), thus the name of this script.

Installation

The script requires ExifTool and Python 3.7+ on your computer.

Windows

Please install ExifTool so that it is added to your path. The easiest is to use installer by Oliver Betz. Use the file "ExifTool_install_nn.nn_64.exe" and accept all defaults.

For Python, please use official installer and make sure to check "Install pip" and "Add Python to environment variables" during installation.

Unix and MacOS

Both Python and ExifTool should be in your package managers or installed already.

Dependencies

Once you have Python and ExifTool installed on your OS, navigate to the script's directory and run

> pip install -r requirements.txt

Usage

Individual photos

To convert image and video pair to Motion Photo v2, run:

> python motionphoto2.py --input-image ImageFile.HEIC --input-video VideoFile.MP4

Directory mode

The script will automatically search file in directory that have the same name but different extension, for example: IMG_1496.HEIC, IMG_1496.MP4

> python motionphoto2.py --input-directory /your/directory

Notes

Limitations

If the source image is an Apple HDR HEIC image, Google Photos will say that the resulting photo is not HDR. This is not true - if you save the photo back to iPhone camera roll, you'll see the photo is HDR.

Google Photos will actually show it too, but only when it is stored in local photos on an iPhone/iPad.

The reason is probably directly related to Motion Photos - the same place where Motion Photos are defined (in XMP object GCamera - http://ns.google.com/photos/1.0/camera/) is also the place where Google/Android stores JPEG/R HDR information.

It appears that the server-side processing of Google Photos does not check for Apple HDR once it finds Google Camera header in XMP tags. For JPG files, a conversion is possible by adjusting metadata and is on the roadmap. Unfortunately there is no support for Gainmap HDR HEIF images in libultrahdr or any modern Android devices at the moment.

Hopefully, as Gainmap HDR matures, both Google and Apple will converge on ISO/CD 21496-1 and things will just start working. On Apple side this has happened already - as of iOS18 RC on iPhone 15(pro), iOS stores HDR in ISO "urn:iso:std:iso:ts:21496:-1" format for both JPG and HEIF. Unfortunately iPhones 12-14 are stuck with Apple Gainmaps (for JPGs, it should be possible to remux them to ISO format, though). On Google side keep an eye on libultrahdr used in Android and also most likely in GPhotos backend. It currently only supports JPEG/R, but HEIC support is on the way.

Credits

Huge thanks to @Tkd-Alex for porting the original PowerShell script to Python. It is now much faster and easier to adjust to boot.

Thanks to @tribut, @Tkd-Alex, @4Urban, @IamRysing and @NightMean for providing sample Motion Photo pictures (check them out here)

Documentation

Google official documentation of the format

Samsung trailer tags are well explained in doodspav's repo

HEIC muxing is similar to doodspav's work, but additionally uses MP4 top-level boxes "mpvd" and "sefd" to add the MP data into heic and mp4 in standard-compliant way (see source in this repo).