Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active May 17, 2020 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennanMKE/1c8e4e79ccd297514d2a5d8498520a8e to your computer and use it in GitHub Desktop.
Save brennanMKE/1c8e4e79ccd297514d2a5d8498520a8e to your computer and use it in GitHub Desktop.
Import and sort video files from SD Card

Importing and Sorting Video Files

A reliable and easy way to import lots of video files with a high capacity SD Card makes it a lot easier to manage lots of recordings. These 2 scripts will import and sort the files. The variables for the paths should be customized at the start of the scripts.

Then simply open your shell to the directory where these script are placed and run the combined commands below.

sh import.sh && sort.sh

Once these command have finished the video files will be organized by date of file creation to make it easier to later find a video you want to edit.

  • macOS (Catalina)
#!/bin/bash
SOURCE_DIR="/Volumes/SDCard/Videos"
DESINATION_DIR="/Volumes/Imports/Unsorted"
mkdir -p "${DESINATION_DIR}"
FILES=$(ls "${SOURCE_DIR}"/ | grep -E "(\.JPG|\.MOV|\.MPG|\.MP4)" | xargs basename)
for FILE in ${FILES}; do
SOURCE_FILE="${SOURCE_DIR}/${FILE}"
DESTINATION_FILE="${DESINATION_DIR}/${FILE}"
if [ -f "${SOURCE_FILE}" -a ! -f "${DESTINATION_FILE}" ]; then
echo "Copying ${FILE}"
# set -x
cp -p "${SOURCE_FILE}" "${DESTINATION_FILE}"
if [ -f "${DESTINATION_FILE}" ]; then
SOURCE_SHA=$(shasum -a 256 "${SOURCE_FILE}" | cut -d ' ' -f 1)
DESTINATION_SHA=$(shasum -a 256 "${DESTINATION_FILE}" | cut -d ' ' -f 1)
if [ "${SOURCE_SHA}" == "${DESTINATION_SHA}" ]; then
rm "${SOURCE_FILE}"
fi
fi
fi
done
#!/bin/bash
UNSORTED_DIR="/Volumes/Imports/Unsorted"
SORTED_DIR="/Volumes/Imports/Sorted"
file_datestamp()
{
FILE_PATH=$1
TIME_FORMAT="%Y-%m-%d"
stat -F -t ${TIME_FORMAT} ${FILE_PATH} | cut -d " " -f 6
}
OIFS="$IFS"
IFS=$'\n'
FILE_PATHS=$( find "${UNSORTED_DIR}" -type f -maxdepth 1 -print | xargs -0 echo )
for FILE_PATH in ${FILE_PATHS}; do
FILE_DATESTAMP=$(file_datestamp "${FILE_PATH}")
DESTINATION_DIR="${SORTED_DIR}/${FILE_DATESTAMP}"
echo "${FILE_PATH} / ${FILE_DATESTAMP}"
mkdir -p "${DESTINATION_DIR}"
mv "${FILE_PATH}" "${DESTINATION_DIR}"
done
IFS="$OIFS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment