侧边栏壁纸
博主头像
七哥的技术博客

You got a dream, you gotta to protect it!

  • 累计撰写 60 篇文章
  • 累计创建 71 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

iphone照片导出并同步到新机

admin
2024-12-30 / 0 评论 / 0 点赞 / 14 阅读 / 0 字

修正时间戳

在 mac 的 照片app 中,选择 文件 --> 导出 --> 导出照片…,可以将照片以及视频媒体导出到本地磁盘上

但是导出的媒体文件有点不正常,会出现修改时间显示为当前时间,而在 照片app中显示的时实际的修改时间

对于 jpeg 图片,使用 jhead 命令进行修正

brew install jhead  #安装jhead程序
jhead -ft ./*.jpeg  #修正jpeg文件的修改时间

对于 mov 视频文件,稍微复杂点,需要多个命令结合,这里写了个简单的脚本来处理

brew install mediainfo jq parallel  #安装必要的程序
 
for file in `ls -1 ./*.mov`
do
    mtime=`mediainfo $file --Output=JSON|jq -c '.media .track[0].extra.com_apple_quicktime_creationdate'|xargs gdate +'%m/%d/%Y %H:%M:%S' -d`
    Setfile -d "$mtime" $file
    Setfile -m "$mtime" $file
done

重命名重复文件

文件名中包含大量的 (1), (2) 这种数字表示的,实际上是重复的文件,需要把数字以及空格全部去掉

#!/usr/bin/python
# -*- coding:utf8 -*-

import os
import shutil

dir_a = "../duphoto/"
dir_b = "../iphone-2022-03-15/"

for filename in os.listdir(dir_a):

    first = filename.find("(")
    last = filename.find(")")

    if first == -1 or last == -1:
        continue

    new_filename = filename[0:first] + filename[last+1:]
    new_filename = new_filename.replace(" ", "")

    os.rename(os.path.join(dir_a, filename), os.path.join(dir_a, new_filename))
    print("    tofix >> ", new_filename)

合并处理后的文件

文件名相同并且 md5 相同的文件删除(移到 realdup),文件名相同但是 md5 值不同移动到 samename 目录

#!/usr/bin/python
# -*- coding:utf8 -*-

import os
import shutil
import hashlib

dir_a = "../duphoto/"
dir_b = "../iphone-2022-03-15/"
dir_c = "../realdup/"
dir_d = "../samename/"

def calculate_md5(file_path):
    with open(file_path, "rb") as f:
        data = f.read()
        md5 = hashlib.md5(data).hexdigest()
    return md5

for filename in os.listdir(dir_a):
    if os.path.exists(os.path.join(dir_b, filename)):
        md5_a = calculate_md5(os.path.join(dir_a, filename))
        md5_b = calculate_md5(os.path.join(dir_b, filename))
        if md5_a == md5_b:
            shutil.move(os.path.join(dir_a, filename), os.path.join(dir_c, filename))
        else:
            shutil.move(os.path.join(dir_a, filename), os.path.join(dir_d, filename))

筛选文件拷贝回 iphone

realdup 和 samename 经过检查,实际上都是重复的照片文件,需要删除

将 duphoto 中剩余的所有文件合并到 iphone-2022-03-15 目录下

HEIC,mov,JPG,PNG 为 iphone 原生生成,拷贝过程中可能会产生 jpeg 和 jpg 文件

#!/usr/bin/python
# -*- coding:utf8 -*-

import os
import shutil

dir_a = "../restore/"
dir_b = "../iphone-2022-03-15/"

exts = [ ".jpeg", ".jpg" ]


for filename in os.listdir(dir_b):
    fext = os.path.splitext(filename)[-1]
    if fext == ".HEIC" or fext == ".mov" or fext == ".JPG" or fext == ".PNG":
        shutil.copyfile(os.path.join(dir_b, filename), os.path.join(dir_a, filename))
        shutil.copystat(os.path.join(dir_b, filename), os.path.join(dir_a, filename))


for ext in exts:
    for filename in os.listdir(dir_b):
        fname = os.path.splitext(filename)[0]
        fext = os.path.splitext(filename)[-1]
        if fext != ext:
            continue
        
        HEIC = os.path.exists(os.path.join(dir_a, fname + ".HEIC"))
        JPG = os.path.exists(os.path.join(dir_a, fname + ".JPG"))
        PNG = os.path.exists(os.path.join(dir_a, fname + ".PNG"))
        jpeg = os.path.exists(os.path.join(dir_a, fname + ".jpeg"))

        if HEIC or JPG or PNG or jpeg:
            continue

        shutil.copyfile(os.path.join(dir_b, filename), os.path.join(dir_a, filename))
        shutil.copystat(os.path.join(dir_b, filename), os.path.join(dir_a, filename))

合并两部分照片

第一个将照片拷贝出来后,清空了 iphone 的内存,后续拍照的文件名可能会和前面的照片相同,需要处理这些文件名的冲突

#!/usr/bin/python
# -*- coding:utf8 -*-

import os
import shutil

dir_a = "../iphone-2022-03-15/"
dir_b = "../iphone-2023-11-13/"


for filename in os.listdir(dir_b):
    fname = os.path.splitext(filename)[0]
    fext = os.path.splitext(filename)[-1]

    HEIC = os.path.exists(os.path.join(dir_a, fname + ".HEIC"))
    mov = os.path.exists(os.path.join(dir_a, fname + ".mov"))
    JPG = os.path.exists(os.path.join(dir_a, fname + ".JPG"))
    PNG = os.path.exists(os.path.join(dir_a, fname + ".PNG"))
    jpeg = os.path.exists(os.path.join(dir_a, fname + ".jpeg"))
    jpg = os.path.exists(os.path.join(dir_a, fname + ".jpg"))

    if HEIC or mov or JPG or PNG or jpeg or jpg:
        new_file = os.path.exists(os.path.join(dir_b, fname + "_0" + fext))
        if new_file:
            print(">> rename photo dup:", filename)
        else:
            os.rename(os.path.join(dir_b, filename), os.path.join(dir_b, fname + "_0" + fext))

投送到 iphone15 中

经测试,单张 mov 投送到 iphone8 的时候,显示为 3 秒的视频,再增加投送一张 heic 或者 jpeg 则会显示为实况照片

但是在 iphone15 下则不同,两个文件分别独立显示为视频和文件,不会进行合并显示为实况

因此,上面的文件需要作进一步处理,实况的照片 mov 文件不进行导入到 iphone15

#!/usr/bin/python
# -*- coding:utf8 -*-

import os
import shutil

dir_a = "../restore/"
dir_b = "../iphone-2023-11-13/"
dir_c = "../iphone15/"

def RestoreIphone15(path):

    for filename in os.listdir(path):
        fext = os.path.splitext(filename)[-1]
        if fext.lower() != ".mov":
            shutil.copyfile(os.path.join(path, filename), os.path.join(dir_c, filename))
            shutil.copystat(os.path.join(path, filename), os.path.join(dir_c, filename))

    for filename in os.listdir(path):
        fname = os.path.splitext(filename)[0]
        fext = os.path.splitext(filename)[-1]
        if fext.lower() == ".mov":
            if not os.path.exists(os.path.join(path, fname + ".HEIC")):
                shutil.copyfile(os.path.join(path, filename), os.path.join(dir_c, filename))
                shutil.copystat(os.path.join(path, filename), os.path.join(dir_c, filename))


RestoreIphone15(dir_a)
RestoreIphone15(dir_b)

单次投送太多有点担心失败,需要设置一下单次投送数量 500 个,这里拆分文件夹的方式投送

中间确实有两次投送一直没响应,所幸的是照片数据没有出现污染的情况,最后所有照片成功投送完毕。

#!/usr/bin/python
# -*- coding:utf8 -*-

import os
import shutil

dir_a = "../iphone15/"

total_cnt = 0

path = "./0"

for filename in os.listdir(dir_a):

    if filename == ".DS_Store":
        continue

    if total_cnt % 500 == 0:
        path = "./" + str(int(total_cnt/500))
        os.makedirs(path)

    shutil.copyfile(os.path.join(dir_a, filename), os.path.join(path, filename))
    shutil.copystat(os.path.join(dir_a, filename), os.path.join(path, filename))

    total_cnt = total_cnt + 1
0

评论区