Advertisement
spikeysnack

audiolength

Jun 13th, 2017
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.95 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # audiolength : duration of an audio file HH:MM:SS
  4. #
  5. # @Author  Chris Reid <spikeysnack@gmail.com>
  6. # @Date    13 June 2017
  7. # @License Free for non-commercial use. Nothing guaranteed
  8. # @requires  soxi awk
  9. #DEBUG=1
  10. DEBUG=    
  11.  
  12.  
  13. # output to stderr only if debug is true
  14. debug()
  15. {
  16.     [[ ${DEBUG} ]] && >&2 echo "${@}"
  17.     return 0 # so it is true
  18. }
  19.  
  20. SOXI=$(which soxi)
  21.  
  22. for A in "gawk" "nawk" "awk" "mawk"  
  23. do
  24.     A=$(which $A)
  25.     AWK=${AWK:-${A}} # find one
  26. done
  27.  
  28. [[ ! $SOXI ]]  && echo "CAN NOT FIND soxi.  Install or fix path."  && exit 1
  29. [[ ! $AWK  ]]  && echo "CAN NOT FIND any awk binaries."  && exit 1
  30.  
  31.  
  32. AUDEXTS="flac mp3 ac3 pcm mp2 mpa mpc ogg oga ape  m4a aac aiff wma wav wv"
  33. # these are partial matches but wrong
  34. PROBLEMEXT="m4 c"              # these are anomalies
  35. aext=("${AUDEXTS}")
  36. problemext=("${PROBLEMEXT}")
  37.  
  38.  
  39. # check good file return 0 for good
  40. goodfile()
  41. {
  42.     [[ $# -eq 0 ]] && >&2 echo "goodfile: no argument" && exit 1
  43.    
  44.     ext="${1##*.}"
  45.  
  46.     debug "GOODFILE: ext = ${ext}"
  47.  
  48.     BAD=1   # bash 1 is false
  49.     GOOD=0  # bash 0 is true
  50.    
  51.    
  52.     for P in ${problemext[@]}
  53.     do
  54.     # file is on problematic filename list
  55.         [[ "${P}" =~ "${ext}" ]] &&   debug "GOODFILE: PROBLEM FILE" && return ${BAD}
  56.     done  
  57.  
  58.  
  59.     for E in ${aext[@]}
  60.     do
  61.     # file is in good filename list
  62.         [[ "${E}" =~ "${ext}" ]] &&   debug "GOODFILE: GOOD" && return ${GOOD}
  63.     done  
  64.  
  65.     debug "NO MATCHES"
  66.  
  67.     return ${BAD}
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74. clean_files()
  75. {
  76.     FileList=( "$@" ) # make an array from the args
  77.  
  78.     debug "OLD FILE LIST:  ( ${FileList[@]} )"
  79.  
  80.     n=${#FileList[@]} # number of elements
  81.  
  82.     # oops no files
  83.     [ "${n}" -eq 0 ] && >&2 printf "no file(s) specified."   && exit 1
  84.  
  85.     #clean out non-files, files with no extension, non-audio files
  86.     for (( i=0; i<n; i++ ))
  87.     do
  88.     M="${FileList[$i]}"
  89.     ext="${M##*.}"
  90.    
  91.     # not a file name
  92.     [ ! -f "${M}" ] &&  unset FileList[$i]  && continue
  93.    
  94.     # case of file name  with no extension
  95.     test "${ext}" = "${M}" && unset FileList[$i]  && continue
  96.    
  97.     goodfile "${M}"
  98.  
  99.     let good=$?
  100.  
  101.  
  102.     if [[ $((good)) -eq 0 ]] ; then
  103.        
  104.         debug echo "GOOD IS TRUE"
  105.  
  106.     else
  107.             debug "GOOD IS FALSE"
  108.         unset FileList[$i]  
  109.         continue
  110.     fi
  111.    
  112.     done # for i
  113.  
  114.     debug "NEW FILE LIST:  ( ${FileList[@]} )"
  115.  
  116.     echo "${FileList[@]}"
  117. }
  118.  
  119.  
  120.  
  121.  
  122. duration()
  123. {
  124.     local DUR
  125.  
  126.     [[ $# -eq 0 ]] && >&2 echo "duration: no argument" && exit 1
  127.  
  128.     DUR=$(${SOXI}  -D "${1}" | $AWK '{SUM += $1} END { printf "%d:%d:%d\n",SUM/3600,SUM%3600/60,SUM%60}')
  129.  
  130.     echo "${DUR}"
  131. }
  132.  
  133.  
  134. main()
  135. {
  136.     # oops no files
  137.     [[ ${#@} -eq 0 ]] && >&2 printf "main: no file(s) specified."   && exit 1
  138.  
  139.     files=( "$(clean_files "${@}")" )
  140.  
  141.     [[ ! ${files} ]] && exit
  142.  
  143.     for F in "${files[@]}"
  144.     do
  145.        dur=$(duration "${F}")
  146.        
  147.        echo "${F}: ${dur}"
  148.     done
  149. }
  150.  
  151.  
  152.  
  153. main "${@}"
  154.  
  155. # END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement