custom-scripts/ingest-mac.sh

382 lines
12 KiB
Bash
Raw Normal View History

2024-06-10 01:01:08 +00:00
#!/bin/bash
######################################################################################
2024-06-23 17:50:40 +00:00
# Name: Photo/Video Ingest Script for Camera and Drone
2024-06-10 01:01:08 +00:00
# Author: voidf1sh (Skylar Grant)
2024-06-23 17:50:40 +00:00
# Environment: M1 MacBook Pro, Sony a5100, DJI Mini 2
2024-06-10 01:01:08 +00:00
#
2024-06-23 17:50:40 +00:00
# Mounts: (Device Mount -- Volume Mount)
# Camera SD: /dev/diskXsY (varies) -- /Volumes/NO NAME
2024-06-23 17:50:40 +00:00
# Drone SD: /dev/diskXsY (varies) -- /Volumes/Untitled
# NAS: //GUEST:@192.168.0.2/media -- /Volumes/media or /Volumes/media-1 (varies)
2024-06-10 01:01:08 +00:00
#
2024-06-23 17:50:40 +00:00
# Ingest Source Paths:
# Camera SD: /DCIM/100MSDCF
# Drone SD: /DCIM/100MEDIA
#
2024-06-10 01:01:08 +00:00
# Git: https://git.vfsh.dev/voidf1sh/custom-scripts
2024-06-23 17:50:40 +00:00
# File: ingest-mac.sh
# Version: 0.1.1
######################################################################################
2024-06-10 01:01:08 +00:00
# Some initial variables to work with
# Set the date for folder creation
CURRENT_DATE=$(date +%F)
2024-06-23 17:50:40 +00:00
# Paths
# IMPORTANT: Paths should NEVER have trailing slashes, and ALWAYS have leading slashes
2024-06-23 16:52:26 +00:00
2024-06-23 17:50:40 +00:00
# Moving mount points to indices, 0=Camera Mode 1=Drone Mode
INGEST_MODE=9 # Default to an invalid mode.
declare -a MODES
MODES[0]="Camera"
MODES[1]="Drone"
2024-06-23 16:52:26 +00:00
# SD card mount point
2024-06-23 17:50:40 +00:00
declare -a SD_MOUNT_POINT
SD_MOUNT_POINT[0]="/Volumes/NO NAME" # Camera
SD_MOUNT_POINT[1]="Volumes/Untitled" # Drone
# Camera's path to images
2024-06-23 17:50:40 +00:00
declare -a SD_SRC_PATH
SD_SRC_PATH[0]="/DCIM/100MSDCF" # Camera
SD_SRC_PATH[1]="/DCIM/100MEDIA" # Drone
# NAS Network Address
NAS_ADDRESS="//GUEST:@192.168.0.2/media"
2024-06-23 16:52:26 +00:00
# Folder where ingests are stored on the Mac
2024-06-23 17:50:40 +00:00
declare -a MAC_INGEST_FOLDER
MAC_INGEST_FOLDER[0]="$HOME/Pictures/Camera Ingest" # Camera
MAC_INGEST_FOLDER[1]="$HOME/Pictures/Drone Ingest" # Drone
2024-06-23 16:52:26 +00:00
# Folder where ingests are stored on the NAS
2024-06-23 17:50:40 +00:00
declare -a NAS_INGEST_FOLDER
NAS_INGEST_FOLDER[0]="/Camera Ingest"
NAS_INGEST_FOLDER[1]="/Drone Ingest"
2024-06-23 16:52:26 +00:00
######################################################################################
# Functions
######################################################################################
2024-06-23 17:50:40 +00:00
# Set mode configuration
set_mode() {
INGEST_MODE=$1
# Where to ingest to on the Mac
MAC_INGEST_PATH="${MAC_INGEST_FOLDER[$INGEST_MODE]}/$CURRENT_DATE"
# Where to ingest to on the NAS
NAS_INGEST_PATH="${NAS_INGEST_FOLDER[$INGEST_MODE]}/$CURRENT_DATE"
echo "Ingest mode set to ${MODES[$INGEST_MODE]} [$INGEST_MODE]"
}
# Mode Prompt
mode_prompt() {
# Print the selection prompt
echo "[ Photo/Video Ingest Script ]"
echo ""
echo "Please select an ingest mode:"
echo ""
echo "[1] Camera"
echo "[2] Drone"
echo ""
echo "[0] Exit"
# Wait for input
read -p "Mode: " mode
# Execute the correct commands based on input.
case "$mode" in
1)
# Camera Mode
clear
set_mode 0
;;
2)
# Drone Mode
clear
set_mode 1
;;
0)
# Exit the script
echo "!! Quitting !!"
sleep 0.8s
clear
exit
;;
*)
clear
echo "!! Invalid Option !!"
exit 1
;;
esac
}
# Create and open Mac folders.
create_folders_mac() {
# Check if the parent ingest directory doesn't exist
if [[ ! -d "$MAC_INGEST_PATH" ]]; then
echo "Creating ingest directories at $MAC_INGEST_PATH/{ARW,JPG,MP4}..."
# Since the parent doesn't exist, we create it and all children at once
mkdir -p "$MAC_INGEST_PATH"/{ARW,JPG,MP4}
# $? is the exit status of the last commmand, with 0 being success
if [[ $? -eq 0 ]]; then
echo "Success!"
else
echo "Failed to create directories at $MAC_INGEST_PATH"
exit 1
fi
else
# The parent directory exists, but do the children?
echo "Parent ingest directory already exists. Checking for subdirectories..."
# Iterate over each folder name
for folder in ARW JPG MP4; do
# If the subdir doesn't exist
if [[ ! -d "$MAC_INGEST_PATH/$folder" ]]; then
echo "Subdirectory $folder is missing, creating..."
# Create the missing subdir
mkdir -p "$MAC_INGEST_PATH/$folder"
# $? is the exit status of the last commmand, with 0 being success
if [[ $? -eq 0 ]]; then
echo "Successfully created $MAC_INGEST_PATH/$folder"
else
echo "Failed to create $MAC_INGEST_PATH/$folder"
exit 1
fi
else
echo "$MAC_INGEST_PATH/$folder already exists."
fi
done
fi
}
# Create and open NAS folders.
create_folders_nas() {
# Check if the parent ingest directory doesn't exist
if [[ ! -d "$NAS_MOUNT_POINT/$NAS_INGEST_PATH" ]]; then
echo "Creating ingest directories at $NAS_INGEST_PATH/{ARW,JPG,MP4}..."
# Since the parent doesn't exist, we create it and all children at once
mkdir -p "$NAS_MOUNT_POINT/$NAS_INGEST_PATH"/{ARW,JPG,MP4}
# $? is the exit status of the last commmand, with 0 being success
if [[ $? -eq 0 ]]; then
echo "Success!"
else
echo "Failed to create directories at $NAS_MOUNT_POINT/$NAS_INGEST_PATH"
exit 1
fi
else
# The parent directory exists, but do the children?
echo "Parent ingest directory already exists. Checking for subdirectories..."
# Iterate over each folder name
for folder in ARW JPG MP4; do
# If the subdir doesn't exist
if [[ ! -d "$NAS_MOUNT_POINT/$NAS_INGEST_PATH/$folder" ]]; then
echo "Subdirectory $folder is missing, creating..."
# Create the missing subdir
mkdir -p "$NAS_MOUNT_POINT/$NAS_INGEST_PATH/$folder"
# $? is the exit status of the last commmand, with 0 being success
if [[ $? -eq 0 ]]; then
echo "Successfully created $NAS_MOUNT_POINT/$NAS_INGEST_PATH/$folder"
else
echo "Failed to create $NAS_MOUNT_POINT/$NAS_INGEST_PATH/$folder"
exit 1
fi
fi
done
fi
}
2024-06-22 21:18:23 +00:00
# Cleanup images on camera SD.
clean_sd() {
2024-06-22 21:18:23 +00:00
# Check if the SD card is mounted
if mount | grep -q "/Volumes/NO NAME"; then
echo "SD Card Mounted at $SD_MOUNT_POINT."
else
echo "ERR: SD Card Not mounted!"
exit 1
fi
echo "Traversing to Camera..."
2024-06-22 21:18:23 +00:00
cd "$SD_MOUNT_POINT$SD_SRC_PATH" || { echo "Failed to navigate to SD card path."; return 1; }
echo "Current Directory: $(pwd)"
echo "Are you sure you want to delete all JPG, ARW, and MP4 files in $SD_MOUNT_POINT$SD_SRC_PATH? (y/N)"
read -r confirmation
if [[ "$confirmation" =~ ^[Yy]$ ]]; then
echo "Removing JPG files..."
rm *.JPG
echo "Removing ARW files..."
rm *.ARW
echo "Removing MP4 files..."
rm *.MP4
echo "Cleanup completed!"
else
echo "Cleanup aborted."
fi
cd - || { echo "Failed to return to the previous directory."; return 1; }
}
# Camera->Mac Ingest
ingest_mac() {
create_folders_mac
echo "Beginning copy..."
# Copy JPG files
jpg_files=("$SD_MOUNT_POINT/$SD_SRC_PATH/"*.JPG)
if [[ -f "${jpg_files[0]}" ]]; then
echo "Copying JPGs..."
rsync -avzc --progress "$SD_MOUNT_POINT/$SD_SRC_PATH/"*.JPG "$MAC_INGEST_PATH/JPG/"
else
echo "No JPGs, skipping."
fi
# Copy ARW files
arw_files=("$SD_MOUNT_POINT/$SD_SRC_PATH/"*.ARW)
if [[ -f "${arw_files[0]}" ]]; then
echo "Copying ARWs..."
rsync -avzc --progress "$SD_MOUNT_POINT/$SD_SRC_PATH/"*.ARW "$MAC_INGEST_PATH/ARW/"
else
echo "No ARWs, skipping."
fi
# Copy MP4 files
mp4_files=("$SD_MOUNT_POINT/$SD_SRC_PATH/"*.MP4)
if [[ -f "${mp4_files[0]}" ]]; then
echo "Copying MP4s..."
rsync -avzc --progress "$SD_MOUNT_POINT/$SD_SRC_PATH/"*.MP4 "$MAC_INGEST_PATH/MP4/"
else
echo "No MP4s, skipping."
fi
echo "Ingest complete!"
open "$MAC_INGEST_PATH"
}
# Mac->NAS Ingest
ingest_nas() {
create_folders_nas
echo "Beginning copy..."
2024-06-23 16:52:26 +00:00
echo "Syncing ingested images with NAS..."
# Sync /Volumes/voidf1sh/Pictures/Camera Ingest/ with NAS/media/Camera Ingest/
rsync -azv --progress "$MAC_INGEST_FOLDER/"
# Copy JPG files
2024-06-23 16:52:26 +00:00
# jpg_files=("$MAC_INGEST_PATH/JPG/"*.JPG)
# if [[ -f "${jpg_files[0]}" ]]; then
# echo "Copying JPGs..."
# rsync -avzc --progress "$MAC_INGEST_PATH/JPG/"*.JPG "$NAS_MOUNT_POINT/$NAS_INGEST_PATH/JPG/"
# else
# echo "No JPGs, skipping."
# fi
2024-06-23 16:52:26 +00:00
# # Copy ARW files
# arw_files=("$MAC_INGEST_PATH/ARW/"*.ARW)
# if [[ -f "${arw_files[0]}" ]]; then
# echo "Copying ARWs..."
# rsync -avzc --progress "$MAC_INGEST_PATH/ARW/"*.ARW "$NAS_MOUNT_POINT/$NAS_INGEST_PATH/ARW/"
# else
# echo "No ARWs, skipping."
# fi
2024-06-23 16:52:26 +00:00
# # Copy MP4 files
# mp4_files=("$MAC_INGEST_PATH/MP4/"*.MP4)
# if [[ -f "${mp4_files[0]}" ]]; then
# echo "Copying MP4s..."
# rsync -avzc --progress "$MAC_INGEST_PATH/MP4/"*.MP4 "$NAS_MOUNT_POINT/$NAS_INGEST_PATH/MP4/"
# else
# echo "No MP4s, skipping."
# fi
echo "Ingest complete!"
open "$NAS_MOUNT_POINT/$NAS_INGEST_PATH"
}
2024-06-23 17:50:40 +00:00
######################################################################################
# Check for Connections
######################################################################################
# Check if the SD card is mounted
if mount | grep -q "${SD_MOUNT_POINT[$INGEST_MODE]}"; then
echo "SD Card Mounted at $SD_MOUNT_POINT."
else
echo "ERR: SD Card Not mounted!"
exit 1
fi
# Check if the NAS is mounted
if mount | grep -q "$NAS_ADDRESS"; then
# Get the NAS mount point since it can vary
NAS_MOUNT_POINT=$(mount | grep "$NAS_ADDRESS" | awk '{print $3}' | grep -E '^/Volumes/media(-1)?$')
echo "NAS Mounted at $NAS_MOUNT_POINT."
else
echo "ERR: NAS Not mounted!"
exit 1
fi
######################################################################################
# Mode Prompt
######################################################################################
# Empty the screen
clear
mode_prompt
######################################################################################
# Prompt Loop
######################################################################################
# Bash allows for linebreaks in string literals and will
# break lines accordingly in the shell
while true; do
2024-06-23 17:50:40 +00:00
# Empty the screen
clear
# Print the prompt
echo "[ Photo/Video Ingest Script ]"
echo "Mode: ${MODES[$INGEST_MODE]}"
echo "Ingest Source Path: ${SD_MOUNT_POINT[$INGEST_MODE]}${SD_SRC_PATH[$INGEST_MODE]}"
echo "Mac Ingest Path: $MAC_INGEST_PATH"
echo "NAS Ingest Path: $NAS_INGEST_PATH"
echo ""
echo "Please enter an option from below:"
echo ""
2024-06-23 17:50:40 +00:00
echo "[1] SD->Mac Ingest"
echo "[2] Mac->NAS Ingest"
2024-06-23 17:50:40 +00:00
echo "[3] !! Delete ALL images on SD card!!"
echo "[4] Change ingest mode"
echo ""
echo "[0] Exit"
# Wait for input
read -p "Option: " opt
# Execute the correct commands based on input.
case "$opt" in
1)
2024-06-23 17:50:40 +00:00
# SD->Mac Ingest
clear
ingest_mac
;;
2)
# Mac->NAS Ingest
clear
ingest_nas
;;
3)
2024-06-23 17:50:40 +00:00
# !! Delete ALL images on SD card!!
clear
2024-06-22 21:18:23 +00:00
clean_sd
;;
2024-06-23 17:50:40 +00:00
4)
# Change ingest mode
clear
mode_prompt;;
0)
# Exit the script
echo "!! Quitting !!"
sleep 0.8s
clear
exit
;;
*)
clear
echo "!! Invalid Option !!"
;;
esac
done