Skip to content

Instantly share code, notes, and snippets.

@erikson1970
Last active March 5, 2018 04:06
Show Gist options
  • Save erikson1970/434ac923f96d0499fe38ae9703739d5c to your computer and use it in GitHub Desktop.
Save erikson1970/434ac923f96d0499fe38ae9703739d5c to your computer and use it in GitHub Desktop.
#!/bin/bash
function show_help {
echo "Usage: $0 [-g SSSS [ActionVerb=FOO, GO required to start processing] ] [-v verbose] [-h help] [-a NN [maxAgeMonths=6]] [-b noBackup = False][-c NN maxCRF=30 ] [-e SSS EXT=mov;mpg ] [-d SSSSS backup staging dir [default=based on EXT] ] [-p purge old files [False] ] [-s NN max number of Dim scaling events [5] ]" >&2
exit 1
}
####################################
#reset date values of file after clearing exif tags:
# touch /tmp/foo.txt;find . -type f -iname "*.mov" -print0 | while IFS= read -r -d '' file;do touch -r $file /tmp/foo.txt;exiftool -xmp:credit= "$file";touch -r /tmp/foo.txt "$file";done
####################################
###################################
# Copy files from backup back to original folder
# basedir=~/Movies/20180304204328_SHRINKY; find . -type f -iname "*.mov" -print0 | while IFS= read -r -d '' file;do basen=`basename ${file}`;cp $basedir/$basen $file;done
##################################
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
output_dest=XXXXX
verbose=0
EXT="mov;mpg"
maxAgeMonths=16
maxCRF=30
noBackup=0
noPurge=1
maxDimenScalings=5
#require a "GO" to start processing for safety
ActionVerb=FOO
while getopts "g:h?va:bc:d:e:s:" opt; do
case "$opt" in
a) maxAgeMonths=$OPTARG
;;
c) maxCRF=$OPTARG
;;
b) noBackup=1
;;
d) output_dest=$OPTARG
;;
e) EXT=$OPTARG
;;
g) ActionVerb=$OPTARG
;;
h|\?)
show_help
exit 0
;;
p) noPurge=0
;;
s) maxDimenScalings=$OPTARG
;;
v) verbose=1
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
sessionID=`date +%Y%m%d%H%M%S`
tmpfile=/tmp/${RANDOM}_placeholder.txt
while IFS=';' read -ra ADDR; do
for THISEXT in "${ADDR[@]}"; do
# process "$i"
printf "|==================================================|\n Processing Extension:%s\n|==================================================|\n " $THISEXT
# echo Processing Extension $THISEXT
dest=/tmp/${RANDOM}_output.${THISEXT}
if [ $output_dest == 'XXXXX' ]; then
case "$THISEXT" in
m[op][gv4] )
backupStagingDir=~/Movies/${sessionID}_SHRINKY
;;
[jp][pn]g )
backupStagingDir=~/Pictures/${sessionID}_SHRINKY
;;
*)
backupStagingDir=/tmp/${sessionID}_SHRINKY
;;
esac
else
backupStagingDir=$output_dest
fi
if [ ! -d $backupStagingDir ]; then
echo destination directory for originals $backupStagingDir not found
echo creating $backupStagingDir
mkdir $backupStagingDir
if [ ! -d $backupStagingDir ]; then
echo creating dest for originals dir failed!!!
exit 1
fi
fi
logfile=$backupStagingDir/ZZZZ_AgingHistory.log
touch $logfile
recoveryfile=$backupStagingDir/ZZZZ_${sessionID}_RecoveryFile.log
cnt=0
echo EXECUTING: maxCRF ${maxCRF} maxAgeMonths ${maxAgeMonths} ActionVerb ${ActionVerb}
echo EXECUTING: find . -type f -iname \"*.${THISEXT}\" -print0
find . -type f -iname "*.${THISEXT}" -print0 | while IFS= read -r -d '' file
do
#print0eserve the access times of the file in a temp file
rm $tmpfile
touch -r "$file" $tmpfile
filebasename=`basename $file`
#calculate file age in months
age=$((($(date +%s) - $(stat -t %s -f %m -- "$file")) / 86400 / 30))
fileDTG=$(stat -f "%Sc" -t "%Y%m%d%H%M" "$file")
ager=$(stat -f "%Sm" -t "%Y%m%d%H%M" "$file")
printf "Oo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oO\n FileName:%s\n Age in Months:%2d Creation Date:%s Last Access:%s" "$file" $age $fileDTG $ager
if [ "$age" -gt "$maxAgeMonths" ]; then
printf " OLD FILE... %s %s " $age $maxAgeMonths
if [ $ActionVerb == 'GO' ]; then
if [ $noPurge == 0 ]; then
rm "$file"
printf " Purging...file deleted!\n"
else
printf " NO-ACTION: Purge of old files not enabled \n"
fi
else
printf " NO-ACTION: rm %s\n" $file
fi
else
printf " Aging File.. \n"
mycrf=$(dc -e "[$(($age + 23))]sM ${maxCRF}d $(($age + 23))<Mp")
#checking the exif tags to see if we've handled this file before
CHECKIN=$(exiftool -xmp:credit "$file")
arrCHECKIN=(${CHECKIN//:/ })
if [[ ${arrCHECKIN[1]} == "XXXXAGED_FILE" ]]; then
printf ' Previously Aged File! Prior CRF is:%s Last time aged:%s \n' ${arrCHECKIN[3]} ${arrCHECKIN[4]}
printf ' Number of times aged:%s Dimension Scaler:%s \n' ${arrCHECKIN[5]} ${arrCHECKIN[2]}
if [ "${arrCHECKIN[4]}" -ge "$age" ]; then
#TODO skip or process file
printf " File age (%s) is same or less than age at last script run (%s). Skipping this file.\n" ${arrCHECKIN[4]} $age
continue
fi
else
printf " First Time handling this file..."
printf " Tagging File..."
#Tag Format: XXXXAGED_FILE:
# ... [dimension scaller-float 0-1]:
# ... [CRF at last resize- byte 1-31]:
# ... [age @ last time shrunk - integer]
# ... [number of times aged - integer]
arrCHECKIN=(Credit XXXXAGED_FILE 0.75 $mycrf $age 0)
if [ $ActionVerb == 'GO' ]; then
exiftool -xmp:credit=XXXXAGED_FILE:0.75:$mycrf:$age:0 "$file"
#transcribe the access times back to the original file
touch -r $tmpfile "$file"
else
exiftoolCMD="exiftool -xmp:credit=XXXXAGED_FILE:0.75:$mycrf:$age:0 '$file'"
echo NO ACTION! skipped.. $exiftoolCMD
printf " NO-ACTION: touch -r %s %s\n" $tmpfile "$file"
fi
if [ "$noBackup" -gt "0" ]; then
printf " Skipping backup...\n"
else
printf " Creating a backup of the file in the Staging Area (for offload to Google Drive or Dropbox)..."
if [ $ActionVerb == 'GO' ]; then
cp "$file" "$backupStagingDir/."
touch -r "$file" "$backupStagingDir/$basename"
printf "done!\n"
else
printf "NO ACTION! skipped..\n"
fi
fi
fi
echo arrCHECKIN[5] = ${arrCHECKIN[5]} maxDimenScalings = "$maxDimenScalings"
if [ "${arrCHECKIN[5]}" -lt "$maxDimenScalings" ]; then
#stop scaling dimension down
dimScalerTerm=""
printf "No Dimension Scaling - exceeded max number. "
else
#continue scaling down dimension
dimScalerTerm="-vf scale=iw*${arrCHECKIN[3]}:ih*${arrCHECKIN[3]}"
printf "Dimension Scaler Term: %s " $dimScalerTerm
fi
if [ $ActionVerb == 'GO' ]; then
echo " AGING the file. CRF: ${mycrf}"
echo EXECUTING: ffmpeg -y -i "$file" -nostdin -vcodec libx264 -profile:v main -level 3.1 -preset medium -crf $mycrf $dimScalerTerm -x264-params ref=4 -acodec libmp3lame -q:a 9 -movflags +faststart "$dest"
ffmpeg -y -i "$file" -nostdin -vcodec libx264 -profile:v main -level 3.1 -preset medium -crf $mycrf $dimScalerTerm -x264-params ref=4 -acodec libmp3lame -q:a 9 -movflags +faststart "$dest"
#transcribe original file creation date onto new file
touch -t $ager "$dest"
#transcribe file's exif tags and mark the file as having been aged
exiftool -tagsFromFile "$file" "$dest"
exiftool -xmp:credit=XXXXAGED_FILE:$mycrf:$age:$( ${arrCHECKIN[5]} + 1 ) "$dest"
echo " SOURCE FILE:" `ls -larth "${file}"` " REDUCED FILE:" `ls -larth "${dest}"`
mv "${dest}" "${file}"
else
printf " NO-ACTION: ffmpeg -y -i \"%s\" -vcodec libx264 -profile:v main -level 3.1 -preset medium -crf %s %s -x264-params ref=4 -acodec libmp3lame -q:a 9 -movflags +faststart %s\n" "$file" $mycrf $dimScalerTerm $dest
printf " NO-ACTION: touch -r %s %s\n" $ager $dest
fi
fi
# if [[ $cnt > 6 ]]; then
# echo "EXITING..."
# exit 1
# fi
cnt=$(($cnt+1))
done
printf "\nDoing Cleanup for Extension:%s\n|==================================================|\n " $THISEXT
if [ -f ${dest} ] ; then
rm ${dest}
fi
if [ -f ${tmpfile} ] ; then
rm $tmpfile
fi
done
done <<< "$EXT"
echo "EXITING...."
@erikson1970
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment