Last active
October 1, 2024 15:35
-
-
Save davidwu111/ffafd1dfb601015ff6a920a3ff4675fd to your computer and use it in GitHub Desktop.
A simply Python script to extract iPhone Live photo which is .livp format into photo only, the .heic format. There is also a .mov file for live photo which I did not extract.
This file contains hidden or 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 os | |
import zipfile | |
def extract_heic_from_livp(root_dir): | |
processed_files = 0 | |
error_count = 0 | |
for foldername, subfolders, filenames in os.walk(root_dir): | |
for filename in filenames: | |
if filename.lower().endswith('.livp'): | |
livp_path = str(os.path.join(foldername, filename)) | |
zip_path = livp_path.replace('.livp', '.zip') | |
print(f"Processing file: {livp_path}") | |
try: | |
os.rename(livp_path, zip_path) | |
live_photos_dir = str(os.path.join(root_dir, 'LivePhotos')) | |
if not os.path.exists(live_photos_dir): | |
os.makedirs(live_photos_dir) | |
with zipfile.ZipFile(zip_path, 'r') as zip_ref: | |
for file in zip_ref.namelist(): | |
if file.lower().endswith('.heic'): | |
base_name = os.path.splitext(file)[0] | |
new_file_name = base_name | |
zip_ref.extract(file, live_photos_dir) | |
os.rename(os.path.join(live_photos_dir, file), os.path.join(live_photos_dir, new_file_name)) | |
os.rename(zip_path, livp_path) | |
processed_files += 1 | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
error_count += 1 | |
print(f"Processed files: {processed_files}") | |
print(f"Errors: {error_count}") | |
if __name__ == "__main__": | |
root_directory = input("Enter the root directory path: ") | |
extract_heic_from_livp(root_directory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment