-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: skip by pattern and use ffprobe for videos
- Loading branch information
1 parent
eebfbee
commit 16f5668
Showing
2 changed files
with
88 additions
and
18 deletions.
There are no files selected for viewing
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
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
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 |