Skip to content

Commit

Permalink
feat: skip by pattern and use ffprobe for videos
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecavestro committed Jun 2, 2024
1 parent eebfbee commit 16f5668
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 18 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
105 changes: 87 additions & 18 deletions save.sh
Original file line number Diff line number Diff line change
@@ -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
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

0 comments on commit 16f5668

Please sign in to comment.