Skip to content

Instantly share code, notes, and snippets.

@brandonmartinez
Created November 19, 2023 19:38
Show Gist options
  • Save brandonmartinez/af0ce5304e8e608040d2a58fd8a80f17 to your computer and use it in GitHub Desktop.
Save brandonmartinez/af0ce5304e8e608040d2a58fd8a80f17 to your computer and use it in GitHub Desktop.
A script that will take videos named in the YYYY-MM-DD-HH-mm-ss* format and use that to update the file created date time and EXIF created date times. Meant to run on macOS (due to `touch` and `stat` differences from linux).
#!/bin/zsh
DEBUG=${DEBUG:-false}
fullpath=$1
log_header() {
RED=$(tput setaf 1)
WHITE=$(tput setaf 7)
RESET=$(tput sgr0)
WRAPPED_TEXT=$(echo "$1" | fmt -w 100)
echo ""
echo -e "${RED}"$(printf '%0.s*' {1..100})"${RESET}"
echo -e "${WHITE}${WRAPPED_TEXT}${RESET}"
echo -e "${RED}"$(printf '%0.s*' {1..100})"${RESET}"
echo ""
}
log_message() {
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
RESET=$(tput sgr0)
WRAPPED_TEXT=$(echo "$1" | fmt -w 100)
echo ""
echo -e "${YELLOW}"$(printf '%0.s*' {1..100})"${RESET}"
echo -e "${BLUE}${WRAPPED_TEXT}${RESET}"
echo -e "${YELLOW}"$(printf '%0.s*' {1..100})"${RESET}"
echo ""
}
# Set initial values and log the date that
# will be written
##################################################
filename=$(basename $fullpath)
fileext="${filename##*.}"
filename="${filename%.*}"
dirname=$(dirname -- "$fullpath")
archive_dir="$dirname/_archive"
date=$(echo $filename | awk -F- '{print $1 $2 $3$4$5"."$6}')
date=$(echo $date | xargs)
formated_date=$(date -j -f "%Y%m%d%H%M.%S" "$date" "+%Y-%m-%d %H:%M:%S")
log_header "Starting to process $fullpath"
echo "The new date which will be written based on the filename is: $formated_date (sourced from $date)"
# Log the initial values in the file
##################################################
log_message "Initial values for $fullpath"
echo "The revised file created date is:"
birthtime=$(stat -f%B $fullpath)
echo $(date -r $birthtime)
echo "The current video record dates are:"
exiftool -FileModifyDate -FileCreateDate -QuickTime:CreateDate $fullpath
# Change the dates using touch and exiftool
##################################################
log_message "Revising date to $date for $fullpath"
if [ "$DEBUG" = true ]; then
if [ "$fileext" = "mpg" ]
then
echo "Command that would be executed:\nffmpeg -i $fullpath ${fullpath%.*}.mp4 -y"
fi
echo "Command that would be executed:\ntouch -mt $date $fullpath"
echo ""
echo "Command that would be executed:\nexiftool -FileModifyDate=\"$date\" -FileCreateDate=\"$date\" -QuickTime:CreateDate=\"$date\" $fullpath -overwrite_original"
echo ""
else
if [ "$fileext" = "mpg" ]
then
echo "Converting $fullpath to mp4"
ffmpeg -i $fullpath ${fullpath%.*}.mp4 -y
echo "Moving $fullpath to $archive_dir"
mv "$fullpath" "$archive_dir/$filename.$fileext"
fullpath=${fullpath%.*}.mp4
fi
touch -mt $date "$fullpath"
exiftool -FileModifyDate="$formated_date" -FileCreateDate="$formated_date" -QuickTime:CreateDate="$formated_date" "$fullpath" -overwrite_original
fi
# Log the revised values in the file
##################################################
log_message "Revised values for $fullpath"
echo "The revised file created date is:"
birthtime=$(stat -f%B $fullpath)
echo $(date -r $birthtime)
echo "The revised video record dates are:"
exiftool -FileModifyDate -FileCreateDate -QuickTime:CreateDate $fullpath
# Exit
##################################################
echo "Done processing $fullpath"
#!/bin/zsh
setopt +o nomatch
dir=$1
DEBUG=${DEBUG:-false}
archive_dir="$dir/_archive"
imported_dir="$dir/_imported"
echo "Creating archive ($archive_dir) and imported ($imported_dir) directories"
mkdir -p "$archive_dir"
mkdir -p "$imported_dir"
for file in $dir/*.{mp4,m4v,mpg,mov}
do
if [ -f "$file" ]; then
echo "Processing $file"
./CreatedAndRecordDateCleanup.sh $file $DEBUG
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment