Last active
January 9, 2022 17:25
-
-
Save miry/9b73f1211a3b642c69f45603791cd68d to your computer and use it in GitHub Desktop.
Organize photos in Synology
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
#!/bin/bash | |
set -e | |
echo "Started: $(date)" | |
export MAGICK_TMPDIR=~/tmp | |
mkdir -p $MAGICK_TMPDIR | |
orgdir="/var/services/homes/miry/work/cloned_photos" | |
photodir="/volume1/photo" | |
# Default synology old version of imagemagick | |
# identify=identify | |
# Required Imagemagick(https://synocommunity.com/package/imagemagick) package installed | |
identify="/usr/local/bin/identify" | |
# identify="/var/packages/imagemagick/target/bin/identify" | |
shopt -s globstar | |
for file in $orgdir/**/* ; do | |
if [ -f "$file" ]; then | |
ext="${file##*.}" | |
if [ ${ext} == "aae" ] ; then | |
# ignore those files | |
continue | |
fi | |
filename=$(basename "${file}") | |
filedate=$(date -u -r "${file}" '+%Y-%m-%d') | |
year=${filedate:0:4} | |
month=${filedate:5:2} | |
if [ "${ext}" == "avi" ] || [ "${ext}" == "mp4" ] || [ "${ext}" == "mov" ] || [ "${ext}" == "3gp" ] || [ "${ext}" == "m4v" ]; then | |
video_creation_time=$(ffmpeg -i "${file}" -dump 2>&1 | grep creation_time | head -1) | |
video_creation_time=${video_creation_time#*: } | |
video_creation_date=${video_creation_time:0:10} | |
if [ "${video_creation_time}" != "" ] && [ "${video_creation_date}" != "${filedate}" ]; then | |
# echo "video_creation_date: ${video_creation_date} vs ${filedate} for ${file}" | |
if [[ "${video_creation_date}" < "${filedate}" ]] || [ $year == "1970" ] ; then | |
filedate="${video_creation_date}" | |
year=${filedate:0:4} | |
month=${filedate:5:2} | |
else | |
echo "video_creation_date[$file]: ${video_creation_date} vs ${filedate} : skip" | |
continue | |
fi | |
fi | |
elif [ "${ext}" == "heic" ] || [ ${ext} == "jpg" ] || [ ${ext} == "jpeg" ] || [ ${ext} == "png" ]; then | |
image_creation_time=$( $identify -format '%[EXIF:DateTimeOriginal]' "${file}" ) | |
image_creation_date=${image_creation_time:0:10} | |
image_creation_date=${image_creation_date//[:]/-} | |
if [ "${image_creation_date}" != "" ] && [ "${image_creation_date}" != "${filedate}" ]; then | |
if [[ "${image_creation_date}" < "${filedate}" ]] || [ $year == "1970" ]; then | |
filedate=$image_creation_date | |
year=${filedate:0:4} | |
month=${filedate:5:2} | |
else | |
echo "image_creation_date[$file]: ${image_creation_date} vs ${filedate} : skip" | |
continue | |
fi | |
fi | |
fi | |
if [ $year == "1970" ]; then | |
echo "[$file]: ${year} == 1970 / ${filedate} : skip" | |
continue | |
fi | |
same="false" | |
if [ "${ext}" == "heic" ] || [ "${ext}" == "jpg" ] || [ ${ext} == "jpeg" ] || [ ${ext} == "png" ] ; then | |
mkdir -p "$photodir/${year}/${month}" | |
dst="${photodir}/${year}/${month}/${filedate}-${filename}" | |
if [ -f "$dst" ]; then | |
signature=$($identify -format '%#' "${file}") | |
fi | |
i=0 | |
same="false" | |
while [ -f "$dst" ]; do | |
echo "$dst is already exists" | |
dest_signature=$($identify -format '%#' "${dst}") | |
if [ "${signature}" != "${dest_signature}" ]; then | |
((i++)) | |
echo "signature are different: ${signature} != ${dest_signature}" | |
dst="${photodir}/${year}/${month}/${filedate}-${filename}-${i}" | |
else | |
same="true" | |
break | |
fi | |
done | |
elif [ "${ext}" == "m4v" ] || [ ${ext} == "avi" ] || [ ${ext} == "mp4" ] || [ ${ext} == "mov" ] || [ ${ext} == "3gp" ] || [ ${ext} == "bmp" ] || [ ${ext} == "gif" ] ; then | |
mkdir -p "$photodir/${year}/${month}" | |
dst="${photodir}/${year}/${month}/${filedate}-${filename}" | |
if [ -f "$dst" ]; then | |
signature=$(md5sum "${file}" | cut -b -32) | |
fi | |
i=0 | |
same="false" | |
while [ -f "$dst" ]; do | |
echo "$dst is already exists" | |
dest_signature=$(md5sum "${dst}" | cut -b -32) | |
if [ "${signature}" != "${dest_signature}" ]; then | |
((i++)) | |
echo "signature are different: ${signature} != ${dest_signature}" | |
dst="${photodir}/${year}/${month}/${filedate}-${filename}-${i}" | |
else | |
same="true" | |
break | |
fi | |
done | |
else | |
echo "[$file] is unknown" | |
fi | |
if [ "$same" == "true" ]; then | |
rm "${file}" | |
continue | |
fi | |
mv -a "${file}" "${dst}" | |
fi | |
done | |
echo "Completed! $(date)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment