Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #
- # audiolength : duration of an audio file HH:MM:SS
- #
- # @Author Chris Reid <spikeysnack@gmail.com>
- # @Date 13 June 2017
- # @License Free for non-commercial use. Nothing guaranteed
- # @requires soxi awk
- #DEBUG=1
- DEBUG=
- # output to stderr only if debug is true
- debug()
- {
- [[ ${DEBUG} ]] && >&2 echo "${@}"
- return 0 # so it is true
- }
- SOXI=$(which soxi)
- for A in "gawk" "nawk" "awk" "mawk"
- do
- A=$(which $A)
- AWK=${AWK:-${A}} # find one
- done
- [[ ! $SOXI ]] && echo "CAN NOT FIND soxi. Install or fix path." && exit 1
- [[ ! $AWK ]] && echo "CAN NOT FIND any awk binaries." && exit 1
- AUDEXTS="flac mp3 ac3 pcm mp2 mpa mpc ogg oga ape m4a aac aiff wma wav wv"
- # these are partial matches but wrong
- PROBLEMEXT="m4 c" # these are anomalies
- aext=("${AUDEXTS}")
- problemext=("${PROBLEMEXT}")
- # check good file return 0 for good
- goodfile()
- {
- [[ $# -eq 0 ]] && >&2 echo "goodfile: no argument" && exit 1
- ext="${1##*.}"
- debug "GOODFILE: ext = ${ext}"
- BAD=1 # bash 1 is false
- GOOD=0 # bash 0 is true
- for P in ${problemext[@]}
- do
- # file is on problematic filename list
- [[ "${P}" =~ "${ext}" ]] && debug "GOODFILE: PROBLEM FILE" && return ${BAD}
- done
- for E in ${aext[@]}
- do
- # file is in good filename list
- [[ "${E}" =~ "${ext}" ]] && debug "GOODFILE: GOOD" && return ${GOOD}
- done
- debug "NO MATCHES"
- return ${BAD}
- }
- clean_files()
- {
- FileList=( "$@" ) # make an array from the args
- debug "OLD FILE LIST: ( ${FileList[@]} )"
- n=${#FileList[@]} # number of elements
- # oops no files
- [ "${n}" -eq 0 ] && >&2 printf "no file(s) specified." && exit 1
- #clean out non-files, files with no extension, non-audio files
- for (( i=0; i<n; i++ ))
- do
- M="${FileList[$i]}"
- ext="${M##*.}"
- # not a file name
- [ ! -f "${M}" ] && unset FileList[$i] && continue
- # case of file name with no extension
- test "${ext}" = "${M}" && unset FileList[$i] && continue
- goodfile "${M}"
- let good=$?
- if [[ $((good)) -eq 0 ]] ; then
- debug echo "GOOD IS TRUE"
- else
- debug "GOOD IS FALSE"
- unset FileList[$i]
- continue
- fi
- done # for i
- debug "NEW FILE LIST: ( ${FileList[@]} )"
- echo "${FileList[@]}"
- }
- duration()
- {
- local DUR
- [[ $# -eq 0 ]] && >&2 echo "duration: no argument" && exit 1
- DUR=$(${SOXI} -D "${1}" | $AWK '{SUM += $1} END { printf "%d:%d:%d\n",SUM/3600,SUM%3600/60,SUM%60}')
- echo "${DUR}"
- }
- main()
- {
- # oops no files
- [[ ${#@} -eq 0 ]] && >&2 printf "main: no file(s) specified." && exit 1
- files=( "$(clean_files "${@}")" )
- [[ ! ${files} ]] && exit
- for F in "${files[@]}"
- do
- dur=$(duration "${F}")
- echo "${F}: ${dur}"
- done
- }
- main "${@}"
- # END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement