simplify photo-move function
This commit is contained in:
parent
7a50ca69a3
commit
849177f02d
1 changed files with 32 additions and 59 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
{
|
{
|
||||||
home.packages = with pkgs; [
|
home.packages = with pkgs; [
|
||||||
rsync
|
|
||||||
pdftk
|
pdftk
|
||||||
ffmpeg
|
ffmpeg
|
||||||
shntool
|
shntool
|
||||||
|
|
@ -109,7 +108,7 @@
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
function camera-copy() {
|
function photo-move() {
|
||||||
local delete_source=0
|
local delete_source=0
|
||||||
if [[ "$1" == "-d" || "$1" == "--delete" ]]; then
|
if [[ "$1" == "-d" || "$1" == "--delete" ]]; then
|
||||||
delete_source=1
|
delete_source=1
|
||||||
|
|
@ -117,81 +116,55 @@
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $# -ne 2 ]]; then
|
if [[ $# -ne 2 ]]; then
|
||||||
echo "Usage: camera-copy [-d|--delete] <source_dir> <destination>"
|
echo "Usage: photo-move [-d|--delete] <source_dir> <destination>"
|
||||||
echo ""
|
echo " -d, --delete Move files instead of copying"
|
||||||
echo "Copy photo and video files organized by date (YYYY-MM-DD/filename)"
|
echo " photo-move /Volumes/CAMERA/DCIM ~/DCIM"
|
||||||
echo ""
|
|
||||||
echo "Options:"
|
|
||||||
echo " -d, --delete Delete source files after successful copy"
|
|
||||||
echo ""
|
|
||||||
echo "Examples:"
|
|
||||||
echo " camera-copy /media/sdcard/DCIM ~/Videos/imports"
|
|
||||||
echo " camera-copy -d /Volumes/CAMERA/DCIM user@nas:/backup/camera"
|
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local SOURCE="$1"
|
local src="$1" dest="$2"
|
||||||
local DEST="$2"
|
|
||||||
|
|
||||||
if [[ ! -d "$SOURCE" ]]; then
|
if [[ ! -d "$src" ]]; then
|
||||||
echo "Error: Source directory does not exist: $SOURCE"
|
echo "Source not found: $src" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_get_media_date() {
|
local count=0 skipped=0
|
||||||
local file="$1"
|
|
||||||
|
while IFS= read -r -d "" file; do
|
||||||
|
local name=$(basename "$file")
|
||||||
|
[[ "$name" == .* ]] && continue
|
||||||
|
|
||||||
local raw_date
|
local raw_date
|
||||||
|
|
||||||
raw_date=$(${pkgs.exiftool}/bin/exiftool -s3 -d '%Y-%m-%d' \
|
raw_date=$(${pkgs.exiftool}/bin/exiftool -s3 -d '%Y-%m-%d' \
|
||||||
-DateTimeOriginal -CreateDate -MediaCreateDate "$file" 2>/dev/null | head -1)
|
-DateTimeOriginal -CreateDate -MediaCreateDate "$file" 2>/dev/null | head -1)
|
||||||
|
|
||||||
if [[ -n "$raw_date" && "$raw_date" != "0000-00-00" ]]; then
|
if [[ ! "$raw_date" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ || "$raw_date" == "0000-00-00" ]]; then
|
||||||
echo "$raw_date"
|
raw_date=$(${pkgs.coreutils}/bin/date -d "@$(${pkgs.coreutils}/bin/stat -c '%Y' "$file")" +%Y-%m-%d)
|
||||||
|
fi
|
||||||
|
|
||||||
|
local target="$dest/''${raw_date:0:4}/$raw_date"
|
||||||
|
mkdir -p "$target"
|
||||||
|
|
||||||
|
if [[ -e "$target/$name" ]]; then
|
||||||
|
((skipped++)) || true
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "[$raw_date] $name"
|
||||||
|
if (( delete_source )); then
|
||||||
|
mv "$file" "$target/$name"
|
||||||
else
|
else
|
||||||
local mtime
|
cp -a "$file" "$target/$name"
|
||||||
mtime=$(${pkgs.coreutils}/bin/stat -c '%Y' "$file" 2>/dev/null)
|
|
||||||
${pkgs.coreutils}/bin/date -d "@$mtime" +%Y-%m-%d
|
|
||||||
fi
|
fi
|
||||||
}
|
((count++)) || true
|
||||||
|
done < <(find "$src" -type f \( \
|
||||||
local copied=0
|
|
||||||
local failed=0
|
|
||||||
|
|
||||||
while IFS= read -r -d "" file; do
|
|
||||||
local filename=$(basename "$file")
|
|
||||||
|
|
||||||
[[ "$filename" == .* ]] && continue
|
|
||||||
|
|
||||||
local date_dir=$(_get_media_date "$file")
|
|
||||||
local year=''${date_dir:0:4}
|
|
||||||
|
|
||||||
echo "[$date_dir] $filename"
|
|
||||||
|
|
||||||
local target_dir="$DEST/$year/$date_dir"
|
|
||||||
if [[ "$DEST" != *:* ]]; then
|
|
||||||
mkdir -p "$target_dir"
|
|
||||||
fi
|
|
||||||
|
|
||||||
local rsync_opts=(-a --mkpath --progress --partial --ignore-existing)
|
|
||||||
[[ $delete_source -eq 1 ]] && rsync_opts+=(--remove-source-files)
|
|
||||||
|
|
||||||
if ${pkgs.rsync}/bin/rsync "''${rsync_opts[@]}" "$file" "$target_dir/$filename"; then
|
|
||||||
((copied++)) || true
|
|
||||||
else
|
|
||||||
echo " Failed to copy: $file"
|
|
||||||
((failed++)) || true
|
|
||||||
fi
|
|
||||||
done < <(find "$SOURCE" -type f \( \
|
|
||||||
-iname "*.mp4" -o -iname "*.mov" -o -iname "*.mts" -o -iname "*.m2ts" -o -iname "*.avi" \
|
-iname "*.mp4" -o -iname "*.mov" -o -iname "*.mts" -o -iname "*.m2ts" -o -iname "*.avi" \
|
||||||
-o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.heic" -o -iname "*.heif" \
|
-o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.heic" -o -iname "*.heif" \
|
||||||
-o -iname "*.cr2" -o -iname "*.cr3" -o -iname "*.nef" -o -iname "*.arw" -o -iname "*.dng" -o -iname "*.raf" -o -iname "*.orf" -o -iname "*.rw2" \
|
-o -iname "*.cr2" -o -iname "*.cr3" -o -iname "*.nef" -o -iname "*.arw" -o -iname "*.dng" -o -iname "*.raf" -o -iname "*.orf" -o -iname "*.rw2" \
|
||||||
\) -print0)
|
\) -print0)
|
||||||
|
|
||||||
echo ""
|
echo "Done: $count files, $skipped skipped"
|
||||||
echo "=== Summary ==="
|
|
||||||
echo "Copied: $copied"
|
|
||||||
echo "Failed: $failed"
|
|
||||||
[[ $delete_source -eq 1 ]] && echo "(source files removed)"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function extract() {
|
function extract() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue