#!/usr/bin/env bash
# Copyright Ken Fallon - Released into the public domain. http://creativecommons.org/publicdomain/ 
#============================================================

search_dir="${HOME}/processing/"

if [ -d "${1}" ]
then
  search_dir="${1}"
fi

find ${search_dir} -type f | grep -vP '/sponsor-anhonesthost.com-hpr15.flac|/outro.flac|/intro.flac|/sponsor-archive.org.flac' | while read mediafile
do 
  duration=$( mediainfo --full --Output=XML "${mediafile}" | xmlstarlet sel -T -t -m "_:MediaInfo/_:media/_:track[@type='Audio']/_:Duration[1]" -v "." -n - | awk -F '.' '{print $1}' )
  if [ "${duration}" != "" ]
  then
    echo "${mediafile}: ${duration}"
    continue
  fi
  duration=$( /bin/date -ud "1970-01-01 $( ffprobe -i "${mediafile}" 2>&1| awk -F ': |, ' '/Duration:/ { print $2 }' )" +%s )
  if [ "${duration}" != 0 ]
  then
    echo "${mediafile}: ${duration}"
    continue
  fi
done
