Created
December 30, 2022 18:35
-
-
Save dclobato/6aaab578b0e02d26cb17cc1f636a2a90 to your computer and use it in GitHub Desktop.
Script para criar um bundle .myb (usado no Mylio) a partir da foto original (.heic) e do video (.mov) produzidos pelas LivePhotos dos dispositivos Apple. Nesta versão, a imagem e o video precisam estar no mesmo diretório, e com o nome foto.HEIC e foto_HEVC.MOV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contextlib | |
import os | |
import zipfile | |
from datetime import datetime | |
from pathlib import Path | |
from PIL import ExifTags | |
from PIL import Image | |
from pillow_heif import register_heif_opener | |
register_heif_opener() | |
@contextlib.contextmanager | |
def working_directory(path): | |
"""Changes working directory and returns to previous on exit.""" | |
prev_cwd = Path.cwd() | |
os.chdir(path) | |
try: | |
yield | |
finally: | |
os.chdir(prev_cwd) | |
diretoriobase = Path(".") | |
alvo = diretoriobase / "LivePhotosBundle" | |
alvo.mkdir(exist_ok = True, | |
parents = True) | |
for foto in diretoriobase.rglob("*.HEIC"): | |
local = foto.parent | |
livephoto = local / (foto.stem + "_HEVC.MOV") | |
if livephoto.is_file(): | |
problema = False | |
try: | |
image_exif = Image.open(foto).getexif() | |
exif = { ExifTags.TAGS[k]: v for k, v in image_exif.items() if k in ExifTags.TAGS and type(v) is not bytes } | |
make = "_".join(exif["Make"].replace(" ", "_").replace("\\", "-").split("_")) | |
model = "_".join(exif["Model"].replace(" ", "_").replace("\\", "-").split("_")) | |
datetimeoriginal = datetime.strptime(exif["DateTime"], '%Y:%m:%d %H:%M:%S').strftime("%Y%m%dT%H%M%S") | |
except KeyError: | |
print(f" >> A imagem {foto} nao tem metadados. Pulando...") | |
problema = True | |
if problema: | |
continue | |
nome_base = datetimeoriginal + "_" + make + "-" + model | |
bundle_file = local / "bundle.myb" | |
problema = False | |
with working_directory(local): | |
try: | |
with zipfile.ZipFile(bundle_file.name, | |
mode = "w", | |
compression = zipfile.ZIP_STORED) as bundle: | |
bundle.write(foto.name, compresslevel = zipfile.ZIP_STORED) | |
bundle.write(livephoto.name, compresslevel = zipfile.ZIP_STORED) | |
except Exception as e: | |
print(f"Problemas: {e}") | |
problema = True | |
if not problema: | |
destino = alvo / local | |
destino.mkdir(exist_ok = True, | |
parents = True) | |
tentativa = destino / (nome_base + ".myb") | |
variacao = 0 | |
print(f"Criando o bundle {tentativa}") | |
while tentativa.is_file(): | |
if variacao > 5: | |
print(">> Provavelmente um problema. Mais de 5 colisoes de nome...") | |
exit(1) | |
print(" Colisao. Tentando outro nome...") | |
variacao = variacao + 1 | |
nome_base = f"{datetimeoriginal}-{variacao:02d}" + "_" + make + "-" + model | |
tentativa = destino / (nome_base + ".myb") | |
if variacao > 0: | |
print(f" Novo nome: {tentativa}") | |
foto.rename(destino / (nome_base + ".heic")) | |
bundle_file.rename(tentativa) | |
livephoto.unlink() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment