From 5bd7d93cb65b491b359260900ad6ca18be37f441 Mon Sep 17 00:00:00 2001 From: Marcel Schoengens Date: Tue, 2 May 2023 10:33:10 +0200 Subject: [PATCH] Add script that sorts media files into a target folder --- zsh/funcs/img2folder.sh | 194 ++++++++++++++++++++++++++++++++++++++++ zsh/funcs/imgfn2exif.sh | 4 +- 2 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 zsh/funcs/img2folder.sh diff --git a/zsh/funcs/img2folder.sh b/zsh/funcs/img2folder.sh new file mode 100644 index 0000000..f158d01 --- /dev/null +++ b/zsh/funcs/img2folder.sh @@ -0,0 +1,194 @@ +function img2folder () ( + #set -x + FLAG_DRYRUN=1 + FLAG_MOVE=0 + FLAG_SOURCE= + FLAG_TARGET= + + function _print_help() { + echo "Sort media files from a source dir and into a target dir" + echo "" + echo "Usage: img2folder -s -t " + echo " -s : source folder" + echo " -t : target folder" + echo " -d : no dry run" + echo " -m : move instead of copy" + } + + function _img_fn_2_exif () { + file=$1 + file_name=$(basename ${file}) + file_name_prefix=${file_name%.*} + + if [ $FLAG_DRYRUN -eq 1 ]; then + cmd=(echo) + else + cmd=() + fi + cmd+=(exiftool -overwrite_original -DateTimeOriginal=${file_name_prefix} ${file}) + echo ${cmd} + "${cmd[@]}" + } + + function _exif_to_isoformat () { + current_exif=$1 + date_only=${current_exif#*Original*: } + date_wo_colon=${date_only//:/} + date_wo_ws=${date_wo_colon// /T} + echo ${date_wo_ws} + } + + function _is_same_file () { + file1=$1 + file2=$2 + hash1=$(shasum $file1) + hash2=$(shasum $file2) + if [ "${hash1% *}" = "${hash2% *}" ]; then + echo 1 + else + echo 2 + fi + } + + function _gen_target_file_path () { + date=$1 + target_sort_dir=$2 + extension=$3 + suffix_c=$4 + datedirs=${date:0:4}"/"${date:4:2} + filename=${date/T/-} + suffix_f=$(printf "%04d" "$suffix_c") + target_file_path=$target_path"/"$datedirs"/"$filename"-"$suffix_f"."$extension + echo $target_file_path + } + + # process input flags + while true; do + case $1 in + -d) + FLAG_DRYRUN=0 + shift; + ;; + -h) + _print_help + exit 0 + ;; + -m) + FLAG_MOVE=1 + shift; + ;; + -s) + shift + FLAG_SOURCE=$1 + shift + ;; + -t) + shift + FLAG_TARGET=$1 + shift + ;; + *) + break + ;; + esac + done + + # make sure input is correct + if [ -z $FLAG_SOURCE ]; then + echo "You need to specify a source directory" + exit 1 + fi + if [ -z $FLAG_TARGET ]; then + echo "You need to specify a target directory" + exit 1 + fi + + source_path=$(readlink -f ${FLAG_SOURCE}) + target_path=$(readlink -f ${FLAG_TARGET}) + if [ ! -d ${target_path} ]; then + echo "Target directory ${target_path} does not exist" + exit 1 + fi + + #set -x + # main processing + IFS=$'\n' + for file in $(find ${source_path} -type f); do + file_basename=$(basename $file) + extension=${file_basename##*.} + current_exif=$(exiftool -m -DateTimeOriginal ${file}) + if [ -z ${current_exif} ]; then + _img_fn_2_exif ${file} + exit_value=$? + if [ $exit_value -ne 0 ]; then + echo "Could not add Exif information, exit value was $exit_value, skipping" + continue + else + current_exif=$(exiftool -m -DateTimeOriginal ${file}) + fi + fi + datetime=$(_exif_to_isoformat ${current_exif}) + + # create target file path and make sure we dont generate duplicates + target_exists=true + suffix_c=0 + while $target_exists; do + target_file_path=$(_gen_target_file_path ${datetime} ${target_path} ${extension} ${suffix_c}) + if [ -e $target_file_path ]; then + if [ $(_is_same_file ${file} ${target_file_path}) -eq 1 ]; then + echo "Duplicate found, skipping ${file}." + break + else + suffix_c=$((suffix_c+1)) + fi + else + target_exists=false + fi + done + + # create directory if needed + target_dir_path=$(dirname ${target_file_path}) + if [ ! -d ${target_dir_path} ]; then + if [ $FLAG_DRYRUN -eq 1 ]; then + cmd=(echo) + else + cmd=() + fi + cmd+=(mkdir -p ${target_dir_path}) + echo ${cmd} + "${cmd[@]}" + fi + + # copy or move file + if [ $FLAG_DRYRUN -eq 1 ]; then + cmd=(echo) + else + cmd=() + fi + if [ $FLAG_MOVE -eq 1 ]; then + cmd+=(mv) + else + cmd+=(cp) + fi + cmd+=($file $target_file_path) + echo ${cmd} + "${cmd[@]}" + + done + + # rm empty folders in source dir + if [ $FLAG_MOVE -eq 1 ]; then + for dir in $(find ${source_path} -type d); do + if [ $FLAG_DRYRUN -eq 1 ]; then + cmd=(echo) + else + cmd=() + fi + cmd+=(rmdir -p $dir) + echo ${cmd} + "${cmd[@]}" + done + fi + +) + diff --git a/zsh/funcs/imgfn2exif.sh b/zsh/funcs/imgfn2exif.sh index e991c41..cb2f597 100644 --- a/zsh/funcs/imgfn2exif.sh +++ b/zsh/funcs/imgfn2exif.sh @@ -12,12 +12,12 @@ function imgfn2exif () ( echo " : -d write exif information" echo " : -o overwrite existing exif information" } - + function _img_fn_2_exif () { file=$1 file_name=$(basename ${file}) file_name_prefix=${file_name%.*} - + if [ $FLAG_DRYRUN -eq 1 ]; then cmd=(echo) else