Created
December 1, 2019 00:13
-
-
Save dustinrouillard/06699cf2a3cef2478eaca1cc18a7a22a to your computer and use it in GitHub Desktop.
Watcher for footage from our network drive - Made for the @NotifyTeam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
INGEST_DIRECTORY="/mnt/shares/footage/ingest" | |
FOOTAGE_DIRECTORY="/mnt/shares/footage" | |
inotifywait -m $INGEST_DIRECTORY -e create -e moved_to | while read path action file; do | |
echo "DEBUG : Detected new file in $path ($file)" | |
# Use media info to pull the metadata of the media | |
FILE_INFO=$(mediainfo ${path}${file} --Output=JSON) | |
FILE_DATE=$(echo ${FILE_INFO} | jq .media.track[0].Encoded_Date | cut -d" " -f2) | |
USED_TODAY="false" | |
echo "DEBUG : $file has an encoded date of $FILE_DATE" | |
# Catch if the date is null | |
if [ $FILE_DATE == "null" ] | |
then | |
echo "DEBUG : Date for $file is null, giving it todays date" | |
USED_TODAY="true" | |
FILE_DATE=$(date +%Y-%m-%d) | |
fi | |
# Create the folder if it doesn't exist | |
if [ ! -d ${FOOTAGE_DIRECTORY}/${FILE_DATE} ] | |
then | |
echo "DEBUG : Folder doesn't exist for the date, creating" | |
mkdir ${FOOTAGE_DIRECTORY}/${FILE_DATE} | |
chmod 0777 ${FOOTAGE_DIRECTORY}/${FILE_DATE} | |
echo "DEBUG : Created folder ${FOOTAGE_DIRECTORY}/${FILE_DATE}" | |
fi | |
FILE_NAME=$(echo $file | cut -d"." -f1) | |
FILE_EXTENSION=$(echo $file | cut -d"." -f2) | |
# Check if the name matches any other file in the target directory | |
if [ -f ${FOOTAGE_DIRECTORY}/${FILE_DATE}/${file} ] | |
then | |
echo "DEBUG : $file already exists in ${FOOTAGE_DIRECTORY}/${FILE_DATE}, checking metadata for a match" | |
NEW_FILE_INFO=$(mediainfo ${path}${file} --Output=JSON | jq .media.track[0].Encoded_Date) | |
EXISTING_FILE_INFO=$(mediainfo ${FOOTAGE_DIRECTORY}/${FILE_DATE}/${file} --Output=JSON | jq .media.track[0].Encoded_Date) | |
if [ "$NEW_FILE_INFO" = "$EXISTING_FILE_INFO" ] && [ $USED_TODAY = "false" ] | |
then | |
echo "DEBUG : File date matches exactly, ignoring file" | |
continue | |
else | |
echo "DEBUG : File name matches, but the date does not match, appending current time stamp" | |
FILE_NAME=${FILE_NAME}-$(date +%s%N | cut -b1-13) | |
fi | |
fi | |
# Move the media to the footage folder for the specfied date | |
if [ ! -d ${FOOTAGE_DIRECTORY}/${FILE_DATE}/${FILE_NAME}.${FILE_EXTENSION} ] | |
then | |
echo "DEBUG : Moving ${FILE_NAME}.${FILE_EXTENSION} to ${FOOTAGE_DIRECTORY}/${FILE_DATE}" | |
mv ${path}${file} ${FOOTAGE_DIRECTORY}/${FILE_DATE}/${FILE_NAME}.${FILE_EXTENSION} | |
chmod 0777 ${FOOTAGE_DIRECTORY}/${FILE_DATE}/${FILE_NAME}.${FILE_EXTENSION} | |
echo "DEBUG : Successfully moved, new location ${FOOTAGE_DIRECTORY}/${FILE_DATE}/${FILE_NAME}.${FILE_EXTENSION}" | |
fi | |
echo "DEBUG : Done processing ${path}${file}, ready for more" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment