#! /bin/bash
# Last edited on 2021-04-24 01:17:06 by jstolfi

# Given an MP4 file name, writes to standard output its creation date and time.
# If it can't find it, prints "????-??-??"

vfile="$1"; shift

tfile=".$$"

ext="${vfile##*.}"
if [[ ( "/${ext}" != "/MP4" ) && ( "/${ext}" != "/mp4" ) && ( "/${ext}" != "/avi" ) ]]; then
  date="bad-ext"
else
  echo "????-??-??" > ${tfile} # Default
  ffmpeg -i ${vfile} -dump \
    |& egrep -i -e 'DateTime|creation_time' \
    | sed -e 's:^.*[:]  *::g' \
    | sort \
    >> ${tfile}
  date="`tail -n 1 ${tfile}`"
fi
printf "%s\n" "${date}"
rm -f ${tfile}