From 16f5668604e671f5d659deff79009012cfc36786 Mon Sep 17 00:00:00 2001 From: Davide Cavestro Date: Sun, 2 Jun 2024 09:26:47 +0200 Subject: [PATCH] feat: skip by pattern and use ffprobe for videos --- Dockerfile | 1 + save.sh | 105 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index f59ba43..76ce6a7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,7 @@ RUN apt-get update && apt-get install -y \ rsync \ locales \ tree \ + ffmpeg \ && rm -rf /var/lib/apt/lists/* # Set the locale diff --git a/save.sh b/save.sh index 8b034ab..50f43db 100755 --- a/save.sh +++ b/save.sh @@ -1,28 +1,97 @@ #!/bin/env bash -FILE=$1 -PARENT_DIR=$(dirname $FILE) +#pwd -pwd - -ls -lha $FILE +#ls -lha $FILE DEST_DIR='' : "${DEST_DIR:?DEST_DIR is not set}" [ -d "$DEST_DIR" ] || { echo "DEST_DIR does not exist: $DEST_DIR"; exit 2; } -echo "PARENT_DIR: $PARENT_DIR" -# set file time -exiftool "-DateTimeOriginal>FileModifyDate" $FILE +#IGNORE_REGEX="${IGNORE_REGEX:-(^(Screenshot_|VID-).*)|(.*(MV-PANO|COLLAGE|-ANIMATION|-EFFECTS)\..*)}" +IGNORE_REGEX="${IGNORE_REGEX:-.*(MV-PANO|COLLAGE|-ANIMATION|-EFFECTS)\..*)" + +function do_image () { + local FILE=$1 + local PARENT_DIR=$(dirname $FILE) + + echo "PARENT_DIR: $PARENT_DIR" + # set file time + exiftool "-DateTimeOriginal>FileModifyDate" $FILE + + # move file to proper subdir + exiftool -d "${PARENT_DIR}/%Y/%Y-%m" '-directory<${CreateDate}' '-filename<${filename}' $FILE + + # move to destination folder + cd $PARENT_DIR + rsync \ + -av \ + --remove-source-files \ + --include='20[0-9][0-9]/' --include='20[0-9][0-9]/20[0-9][0-9]-[0-1][0-9]/' \ + . $DEST_DIR + + rm -rf $PARENT_DIR +} + +function do_video () { + local FILE=$1 + local PARENT_DIR=$(dirname $FILE) + + echo "PARENT_DIR: $PARENT_DIR" + + #json=$(ffprobe -v quiet -print_format json -show_format $1) + #creation_time=$(echo $json | jq -r .format.tags.creation_time) + #location=$(echo $json | jq -r .format.tags.location) + + local creation_time=$(ffprobe -v quiet -print_format json -show_entries format_tags=creation_time "$FILE" | jq -r '.format.tags.creation_time') + + # If creation_time is not available, use file modification time + if [[ "$creation_time" == "null" || -z "$creation_time" ]]; then +# creation_time=$(stat -c %y "$file") + echo "$FILE has no creation_time" + else + # Extract year and month from the creation time + local year=$(date -d "$creation_time" +"%Y") + local month=$(date -d "$creation_time" +"%Y-%m") + + # Create target directory if it doesn't exist + local target_dir="${PARENT_DIR}/${year}/${year}-${month}" + mkdir -p "${target_dir}" + + # Move the file to the target directory + mv "$FILE" "${target_dir}/" + + echo "Moved $FILE to $target_dir/" + + # move to destination folder + cd $PARENT_DIR + rsync \ + -av \ + --remove-source-files \ + --include='20[0-9][0-9]/' --include='20[0-9][0-9]/20[0-9][0-9]-[0-1][0-9]/' \ + . $DEST_DIR -# move file to proper subdir -exiftool -d "${PARENT_DIR}/%Y/%Y-%m" '-directory<${CreateDate}' '-filename<${filename}' $FILE + fi + + rm -rf $PARENT_DIR +} -# move to destination folder -cd $PARENT_DIR -rsync \ - -av \ - --remove-source-files \ - --include='20[0-9][0-9]/' --include='20[0-9][0-9]/20[0-9][0-9]-[0-1][0-9]/' \ - . $DEST_DIR +# Check if the file matches the regex in IGNORE_REGEX +base=$(basename $1) +if [[ ! $base =~ $IGNORE_REGEX ]]; then + echo "Processing: $1" + mimetype=$(mimetype -b "$1") -rm -rf $PARENT_DIR \ No newline at end of file + case $mimetype in + image/*) + do_image "$1" + ;; + video/*) + do_video "$1" + ;; + *) + echo "$1 is neither an image nor a video" + ;; + esac +else + echo "Ignoring $1" +fi