Skip to content

Instantly share code, notes, and snippets.

@NaanProphet
Last active May 12, 2017 14:57
Show Gist options
  • Save NaanProphet/e4f4746af59c9f159222 to your computer and use it in GitHub Desktop.
Save NaanProphet/e4f4746af59c9f159222 to your computer and use it in GitHub Desktop.
Timecode Enricher
#!/bin/sh
### Timecode generation script for MP4 footage (AVCHD, XAVC-S, etc.)
### Uses ffmpeg for timecode creation; EditReady for container cleanup;
### exiftool for copying over original track dates; and SetFile for setting
### the File Creation Date for FCP X event
### Note: this script currently does not work for files with spaces
# quit on first error (almost always)
# special thanks to: http://stackoverflow.com/a/3474556
set -e
# set to 1 to debug
INTERACTIVE=0
function pause(){
read -p "$*"
}
function interactive(){
if [[ INTERACTIVE -eq 1 ]]; then
pause 'Press [Enter] key to continue...'
fi
}
prevent_idle="caffeinate -i"
if [ "$#" -ne 1 ]
then
echo "Usage: script.sh INPUT_FOLDER"
echo "Note: INPUT_FOLDER is non-recursive"
exit 1
fi
# trim forward slashes from folders
# special thanks to: http://stackoverflow.com/a/1848456
INPUT_FOLDER=${1%/}
# no forward slash
# TODO for some reason ~ breaks exiftool command
OUTPUT_FOLDER="/Users/Krish/Movies/EditReady"
NDF_SUFFIX=";00"
EDITREADY="/Applications/EditReady.app/Contents/MacOS/EditReady"
REWRAP_PRESET="/Applications/EditReady.app/Contents/Resources/Rewrap.erpreset"
for f in $INPUT_FOLDER/*
do
if [[ -d "$f" ]]; then
echo "$f is a directory, skipping"
continue
fi
# special thanks to: http://stackoverflow.com/a/965072
filename=$(basename "$f")
extension="${filename##*.}"
filename="${filename%.*}"
echo "Processing $filename..."
START_TIMEDATE=`exiftool -api largefilesupport=1 -T -TrackCreateDate "$f"`
START_TIME=`echo ${START_TIMEDATE} | tail -c9`
START_YEAR=`echo ${START_TIMEDATE} | head -c4`
START_MONTH=`echo "${START_TIMEDATE}" | head -c7 | tail -c2`
START_DAY=`echo "${START_TIMEDATE}" | head -c10 | tail -c2`
echo " Track Create Date is ${START_TIME}"
# ------------- Generate Timecode Track ---------------------
TCFILE="${1}/${filename}.tmp.${extension}"
echo " Generating timecode track $TCFILE..."
interactive
${prevent_idle} ffmpeg -vf "fieldorder=tff" -i "$f" -codec copy -timecode ${START_TIME}${NDF_SUFFIX} $TCFILE
# ------------- Transfer track creation metadata ---------------------
echo "Copying date tags from original to newly rewrapped file ${EDITREADY_OUTPUT}..."
interactive
${prevent_idle} exiftool -api largefilesupport=1 -overwrite_original -tagsFromFile "$f" -CreateDate -TrackCreateDate ${TCFILE}
# ------------- EditReady rewrap for proper container integrity. Also sets TrackModifyDate and ModifyDate based on earlier ExifTool changes for CreateDate and TrackCreateDate ---------------------
TCFILENAME=$(basename "${TCFILE}")
EDITREADY_OUTPUT="${OUTPUT_FOLDER}/${TCFILENAME}"
echo "Rewrapping $TCFILE with EditReady to clean up container..."
interactive
${prevent_idle} ${EDITREADY} -s ${TCFILE} -d ${OUTPUT_FOLDER} -p ${REWRAP_PRESET}
# ------------- Adjust file creation date for FCP X ---------------------
# special thanks to: http://apple.stackexchange.com/a/99599
echo "Setting Mac Creation Time (for proper FCP X event date) on ${EDITREADY_OUTPUT}..."
interactive
SetFile -d "${START_MONTH}/${START_DAY}/${START_YEAR} ${START_TIME}" ${EDITREADY_OUTPUT}
# ------------- Cleanup temp files ---------------------
# echo " Deleting temp file ${TCFILE}..."
# interactive
# rm ${TCFILE}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment