Skip to content

Instantly share code, notes, and snippets.

@it9gamelog
Created June 26, 2020 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save it9gamelog/b13b19974060ea289347552a5826e750 to your computer and use it in GitHub Desktop.
Save it9gamelog/b13b19974060ea289347552a5826e750 to your computer and use it in GitHub Desktop.
Generating thumbnail periodically
#!/bin/bash
# 將orig/*生成128x128的縮圖放到thumbnail/*
# 每次跑只會搜索新的檔案
# thumbnail如已經存在較新的也會忽略,不會覆蓋
#
# orig和thumbnail兩個目錄要自己創建
cd "$(dirname "$(realpath "$0")")";
# 時間截: 每次跑完會記錄時間,下次只搜索比這個時間新的檔案
[ -f next.ts ] || touch -d 1980-01-01 next.ts
touch next.ts.new
gen() {
local FILENAME="$1"
# 來源檔案名
local ORIG="orig/$FILENAME"
# 目標檔案名
local DEST="thumbnail/$FILENAME"
# 如果目標已存在,而且目標是比較新的話,那就退出
[ -f "$DEST" -a "$DEST" -nt "$ORIG" ] && return 0
# 不然就resize到128x128 (保持比例)
convert -thumbnail 128x128 "$ORIG" "$DEST" || return 1
echo "$DEST generated"
}
export -f gen
# cd orig, 再搜索jpg或jpeg比next.ts新的檔案
# 然後調用 gen "檔案名"
# 完成後把時間截更新
(cd orig; find . \( -iname '*.jepg' -o -iname '*.jpg' \) -type f -newer ../next.ts) | \
xargs -L 1 -I {} bash -c 'gen "$@"' _ {} && \
mv next.ts.new next.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment