Pastebin

Paste #15629: No description

< previous paste - next paste>

Pasted by Anonymous Coward

Download View as text

#!/bin/zsh
# Go through the photo collection and produce a mirror copy in $dest_folder
# containing lower resolution instance of each image file (using imagemagick).
# Produce hardlinks for each video file encountered in the source collection.
# Delete any files from the $dest_folder that is not present in the source collection.
# Only produce new instance files if the file does not exist in the $dest_folder with
# a newer timestamp.
umask 007
# enable extended globbing required for the folder matching we do here
setopt extendedglob
# Folder in which the low res copies will be created
dest_folder="../view_copy"
# Compression factor: must be between 0.99. 
# Higher means higher quality but bigger size
quality="60"
resolution="1920x1920"
private_folders="privat(e|)" 
photo_extensions="(jpg|JPG|jpeg|heic|HEIC|png|PNG)"
video_extensions="(mov|MOV|mp4|MP4|avi|AVI|mkv|MKV)"
# Renice the main process
renice -n 10 $$
# Create folder structure in destination that matches that of source.
# Create only folders matching the following:
#   Must be in a year folder (e.g. a folder matching 2???)
#   Must be inside an album (e.g. a folder inside a year folder starting with an isodate - matching 2*
#   Ignore folders named "private" or "privat".
# 
# Match all folders matching 2???/2* and all their subfolders, except if any of those folders are called 'private'
( for x in 2???/2*/(^(private|privat)/)#(/); do mkdir -p $dest_folder/$x; done )
# Create folder structure for "in" folders
( for x in 2???/in/(^(private|privat)/)#(/); do mkdir -p $dest_folder/$x; done )
# Delete files in destination that do not exist in the source
for x in $dest_folder/**/*(.); do
    orig=${x#$dest_folder/}
    # This check includes a test if a jpg file exist in the destination 
    # The original check was: [[ -e $orig ]] || delete_candidates+=($x)
    # We want to delete the existing jpeg file in the destination even if the original file in source folder is a .heic file
    [[ -e $orig  || -e $orig:r.jpg || -e $orig:r.JPG ]] || delete_candidates+=($x)
done
#(( $#delete_candidates > 0 )) && rmdir -p $delete_candidates
rm -fv $delete_candidates
# Delete folders in destination that do not exist in the source
for folder in $dest_folder/**/*(/); do
    orig=${folder#$dest_folder/}
    [[ -e $orig ]] || delete_candidates+=($folder)
done
# Delete the delete candidates unless the list is empty
(( $#delete_candidates > 0 )) && rmdir $delete_candidates
for year in 20??; do 
    time ( 
    echo $year STARTING
    # Generate low res copies of photos in YYYY/YY-MM-DD-album and YYYY/in/YY-MM-DD-album
    #for x in $year/2*/(^($~private_folders)/)#/*.($~photo_extensions)(N) $year/in/(^($~private_folders)/)#/*.($~photo_extensions)(N) ; do 
    for x in $year/(2*|in)/(^($~private_folders)/)#/*.($~photo_extensions)(N); do
        ## convert .heic images to .jpg as they do not work upstream in the gallery
        # Meaning of the ":" modifiers:
        #   :l     lower case
        #   :e     extension
        #   :r     filename without extension (root of file name)
        # Check if $dest_folder/$x older than  $x
        ext=$x:e
        [[ $ext:l = heic ]] && ext=jpg; 
        # Only process images that has been updated in the source folder or does not exist in the dest folder
        if [[ ! -e "$dest_folder/$x:r.$ext" ]] || [[ "$x" -nt "$dest_folder/$x:r.$ext" ]]; then
            echo Updating $x
            convert -quiet -quality $quality -resize $resolution "$x" "$dest_folder/$x:r.$ext"
            #echo -n .
        else
            echo Skipping $x
            #echo -n _
        fi
    done
    # Create hardlinks to videos in YYYY/YY-MM-DD-album and YYYY/in/YY-MM-DD-album
    for x in $year/2*/(^($~private_folders)/)#/*.($~video_extensions)(N) $year/in/(^($~private_folders)/)#/*.($~video_extensions)(N) ; do 
        # if $dest_folder/$x then create a hardlink to it
        if [[ ! -e "$dest_folder/$x" ]]  ; then
            ln "$x" "$dest_folder/$x"
            #echo -n .
        else
            #echo "Skipping $x"
            #echo -n _
        fi
    done
    echo
    echo $year DONE
    ) &
    #exit
done
echo "Waiting for processes to complete"
wait
echo "DONE"

New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.