最近 Gmail 收到一封邮件说是 7 月份就不能用 takeout 批量导出 Archive Albums 了, 我之前 Blogger 上存的图片不多, 绝大部分都在 Google Photos 里面.

想着这样下去指不定哪天 Google 就把 Google photos 的免费 takeout 关了, 想想上面还是存了几千张照片的.

而且因为最近都一直用 immich 代替 google photos, 体验也挺不错的, 干脆把 Google photos 里的旧照片打包弄出来导入 immich, 完全告别 google photos.

于是看到了这个 issue https://github.com/immich-app/immich/discussions/1340 正是我想要的

想了解 immich 功能的可以看看项目地址 https://github.com/immich-app/immich 更新非常频繁而且时不时有 breaking change, 如果想稳定使用建议固定版本或者更新服务端前仔细阅读 changelog

首先肯定是去 takeout.google.com 把你的照片打包弄出来, 最好选择英语导出, 我只选了 Google photos 和 AlbumArchive, 格式 zip, 单个包直接选 50G, 反正系统很新不怕解不了压. 几个小时后就收到 Google 的邮件和下载链接了.

然后重要的地方是: 把下载好解压出来的 Takeout 文件夹复制到你 linux 文件系统里, 不要在挂载的 smb 共享里操作, 因为文件系统的区别(比如大小写敏感度的不同), issue 里提到的几个工具都有可能会报错!

接下来就是选择你希望的导入方式, 如果你想保留 google photos 里面的一个个的相册. 那么可以用https://github.com/MrYakobo/exif-wrapper这个脚本 它会在你的 Takeout/Google Photos 里生成 mattwilson1024/google-photos-exif 这个工具需要的文件夹 然后你就可以用 bash 脚本批量上传到你的 immich 服务器

#!/bin/bash

KEY="YOURKEYHERE"
SERVER="https://photos.yourdomain.com/api"
BASE_DIR="/foo/bar/Takeout/Google Photos/AlbumsProcessed"

# Change the working directory to BASE_DIR
cd "$BASE_DIR"

for album in "$BASE_DIR"/*; do
  if [ -d "$album" ]; then
    echo "Uploading album: $album"
    immich upload \
      -y \
      --key "$KEY" \
      --server "$SERVER" \
      -d "$album" -t 16 -al
  fi
done

我自己选择的是第二个工具, 因为我从来没有手动整理过 Google 相册, 所以我只需要简单的时间线和地理位置就行了, 而且相比第一个工具, 第二个工具能处理的文件扩展名更多 https://github.com/garzj/google-photos-migrate

我用的 ubuntu cloud 22.04 的虚拟机, 用别的发行版的可以自行改动一下 先安装 nodejs, 注意版本需要>=14, 我这里装 16 并安装 Yarn 包管理器

curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -

sudo apt install gnupg2
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn

然后下载 google-photos-migrate 并安装依赖

git clone https://github.com/garzj/google-photos-migrate.git
cd google-photos-migrate

yarn

此时要注意, 如果你 takeout 选择的语言不是英语, 需要修改 src/meta/find-meta-file.ts 加入你相册里面可以看到的对应语言的'edited'后缀, 不加的话这部分的图片会找不到对应的 meta file.

woExt = woExt.replace(/-(edited|bearbeitet|modifié)$/i, '');
修改完保存然后继续编译执行

yarn build

mkdir output error
yarn start '/path/to/takeout/Google Photos' './output' './error'

等待它执行完, 你需要上传的就是 output 文件夹里的内容, 重复的会放在 /output/duplicateXX 文件夹里, 可以自行决定删除哪个, 也可以直接上传, immich 会过滤掉 hash 一致的图片, 如果还有重复的估计就是缩略图, 自己看着删除或者写个比较大小的脚本删吧.

上传直接用 immich cli 就行了

yarn add immich

yarn immich upload --key YOUR_IMMICH_API_KEY --server http://docker.lan:3001 --recursive /home/bar/google-photos-migrate/output/

注意一下, immich 新加的 typesense 识图的时候会把 cpu 吃饱(我的 13400 直接 16 线程吃满 90%了...), 如果你上传的虚拟机不幸卡死, 可以等 immich 服务器那边 load 下去以后, 再上传一次, 它会自动对比云端和本地的文件, 不会上传重复的照片.

上传完就可以和 Google Photos 说再见了. 当然如果你用的 icloud 之类的,可以先把 icloud 的图片全部下载到手机, 然后用 immich 的 app 同步手机里的图片视频到 nas, 同步完以后再关闭 icloud 的图片同步功能, icloud 会直接腾出空间给你同步其它的内容, 照片会在暂存一个月后永久删除.