X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/b8f798aa115328694754d1c343127440c270a83f..a9a36cc48e1b18d63c1b1f240c9347191ec8d67f:/unitproc/bktemp-get_audio_format?ds=sidebyside diff --git a/unitproc/bktemp-get_audio_format b/unitproc/bktemp-get_audio_format new file mode 100644 index 0000000..78d9cf6 --- /dev/null +++ b/unitproc/bktemp-get_audio_format @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +get_audio_format() { + # Desc: Gets audio format of file as string + # Usage: get_audio_format arg1 + # Depends: ffprobe + # Version: 0.0.1 + # Input: arg1: input file path + # Output: stdout (if valid audio format) + # exit code 0 if audio file; 1 otherwise + # Example: get_audio_format myvideo.mp4 + # Note: Would return "opus" if full ffprobe report had 'Audio: opus, 48000 Hz, stereo, fltp' + # Note: Not tested with videos containing multiple video streams + # Ref/Attrib: [1] https://stackoverflow.com/questions/5618363/is-there-a-way-to-use-ffmpeg-to-determine-the-encoding-of-a-file-before-transcod + # [2] https://stackoverflow.com/questions/44123532/how-to-find-out-the-file-extension-for-extracting-audio-tracks-with-ffmpeg-and-p#comment88464070_50723126 + local audio_format file_in; + local return_state; + file_in="$1"; + + # Return error exit code if not audio file + ## Return error if ffprobe itself exited on error + 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 + return_state="false"; + fi; + + # Get audio format + audio_format="$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 "$file_in")"; # see [1] + + ## Return error if audio format is incorrectly formatted (e.g. reject if contains spaces) + pattern="^[[:alnum:]]+$"; # alphanumeric string with no spaces + if [[ $audio_format =~ $pattern ]]; then + return_state="true"; + # Report audio format + echo "$audio_format"; + else + return_state="false"; + fi; + + # Report exit code + if [[ $return_state = "true" ]]; then + return 0; + else + return 1; + fi; +} # Get audio format as stdout + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+ \ No newline at end of file