4 # Desc: Gets audio format of file as string
5 # Usage: get_audio_format arg1
8 # Input: arg1: input file path
9 # Output: stdout (if valid audio format)
10 # exit code 0 if audio file; 1 otherwise
11 # Example: get_audio_format myvideo.mp4
12 # Note: Would return "opus" if full ffprobe report had 'Audio: opus, 48000 Hz, stereo, fltp'
13 # Note: Not tested with videos containing multiple video streams
14 # Ref/Attrib: [1] https://stackoverflow.com/questions/5618363/is-there-a-way-to-use-ffmpeg-to-determine-the-encoding-of-a-file-before-transcod
15 # [2] https://stackoverflow.com/questions/44123532/how-to-find-out-the-file-extension-for-extracting-audio-tracks-with-ffmpeg-and-p#comment88464070_50723126
16 local audio_format file_in
;
20 # Return error exit code if not audio file
21 ## Return error if ffprobe itself exited on error
22 if ! ffprobe
-v error
-select_streams a
-show_entries stream
=codec_name
-of default
=nokey
=1:noprint_wrappers
=1 "$file_in" 1>/dev
/null
2>&1; then
27 audio_format
="$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 "$file_in")"; # see [1]
29 ## Return error if audio format is incorrectly formatted (e.g. reject if contains spaces)
30 pattern
="^[[:alnum:]]+$"; # alphanumeric string with no spaces
31 if [[ $audio_format =~
$pattern ]]; then
40 if [[ $return_state = "true" ]]; then
45 } # Get audio format as stdout
47 # Author: Steven Baltakatei Sandoval