2 # Desc: Converts file readable by ffmpeg to FLAC audio format
3 # Usage: convert_file_to_flac.sh [path]
5 # Ref/Attrib: [1] Convert audio file to FLAC with ffmpeg? https://superuser.com/a/802126
7 declare -Ag appRollCall
# Associative array for storing app status
8 declare -Ag fileRollCall
# Associative array for storing file status
9 declare -Ag dirRollCall
# Associative array for storing dir status
11 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
12 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
13 try
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
15 # Desc: If arg is a command, save result in assoc array 'appRollCall'
16 # Usage: checkapp arg1 arg2 arg3 ...
18 # Input: global assoc. array 'appRollCall'
19 # Output: adds/updates key(value) to global assoc array 'appRollCall'
25 if command -v "$arg" 1>/dev
/null
2>&1; then # Check if arg is a valid command
26 appRollCall
[$arg]="true";
27 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
29 appRollCall
[$arg]="false"; returnState
="false";
33 #===Determine function return code===
34 if [ "$returnState" = "true" ]; then
39 } # Check that app exists
41 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
42 # Usage: checkfile arg1 arg2 arg3 ...
44 # Input: global assoc. array 'fileRollCall'
45 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
46 # Output: returns 0 if app found, 1 otherwise
52 if [ -f "$arg" ]; then
53 fileRollCall
["$arg"]="true";
54 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
56 fileRollCall
["$arg"]="false"; returnState
="false";
60 #===Determine function return code===
61 if [ "$returnState" = "true" ]; then
66 } # Check that file exists
68 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
69 # Usage: checkdir arg1 arg2 arg3 ...
71 # Input: global assoc. array 'dirRollCall'
72 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
73 # Output: returns 0 if all args are dirs; 1 otherwise
79 if [ -z "$arg" ]; then
80 dirRollCall
["(Unspecified Dirname(s))"]="false"; returnState
="false";
81 elif [ -d "$arg" ]; then
82 dirRollCall
["$arg"]="true";
83 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
85 dirRollCall
["$arg"]="false"; returnState
="false";
89 #===Determine function return code===
90 if [ "$returnState" = "true" ]; then
95 } # Check that dir exists
97 # Desc: Displays missing apps, files, and dirs
98 # Usage: displayMissing
100 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
101 # Output: stderr: messages indicating missing apps, file, or dirs
102 # Output: returns exit code 0 if nothing missing; 1 otherwise
103 # Depends: bash 5, checkAppFileDir()
104 local missingApps value appMissing missingFiles fileMissing
105 local missingDirs dirMissing
107 #==BEGIN Display errors==
108 #===BEGIN Display Missing Apps===
109 missingApps
="Missing apps :";
110 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
111 for key
in "${!appRollCall[@]}"; do
112 value
="${appRollCall[$key]}";
113 if [ "$value" = "false" ]; then
114 #echo "DEBUG:Missing apps: $key => $value";
115 missingApps
="$missingApps""$key ";
119 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
120 echo "$missingApps" 1>&2;
123 #===END Display Missing Apps===
125 #===BEGIN Display Missing Files===
126 missingFiles
="Missing files:";
127 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
128 for key
in "${!fileRollCall[@]}"; do
129 value
="${fileRollCall[$key]}";
130 if [ "$value" = "false" ]; then
131 #echo "DEBUG:Missing files: $key => $value";
132 missingFiles
="$missingFiles""$key ";
136 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
137 echo "$missingFiles" 1>&2;
140 #===END Display Missing Files===
142 #===BEGIN Display Missing Directories===
143 missingDirs
="Missing dirs:";
144 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
145 for key
in "${!dirRollCall[@]}"; do
146 value
="${dirRollCall[$key]}";
147 if [ "$value" = "false" ]; then
148 #echo "DEBUG:Missing dirs: $key => $value";
149 missingDirs
="$missingDirs""$key ";
153 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
154 echo "$missingDirs" 1>&2;
157 #===END Display Missing Directories===
159 #==END Display errors==
160 #==BEGIN Determine function return code===
161 if [ "$appMissing" == "true" ] ||
[ "$fileMissing" == "true" ] ||
[ "$dirMissing" == "true" ]; then
166 #==END Determine function return code===
167 } # Display missing apps, files, dirs
168 check_parsable_audio_ffprobe
() {
169 # Desc: Checks if ffprobe returns valid audio codec name for file
170 # Usage: check_parsable_audio_ffprobe [path FILE]
172 # Input: arg1: file path
173 # Output: exit code 0 if returns valid codec name; 1 otherwise
174 # Depends: ffprobe, die()
175 local file_in ffprobe_out
177 if [[ $# -ne 1 ]]; then die
"ERROR:Invalid number of args:$#"; fi;
181 # Check if ffprobe detects an audio stream
182 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
185 return_state
="false";
188 # Fail if ffprobe returns no result
189 ffprobe_out
="$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 "$file_in")";
190 if [[ -z $ffprobe_out ]]; then
191 return_state
="false";
195 if [[ $return_state = "true" ]]; then
200 } # Checks if file has valid codec name using ffprobe
203 if [[ ! -f $1 ]]; then die
"FATAL:Not a file:$1"; fi;
204 if [[ $# -ne 1 ]]; then die
"FATAL:Arguments not equal to 1:$#"; fi;
207 if ! checkapp ffmpeg ffprobe
; then
209 die
"FATAL:Missing apps"; fi;
212 # Check file is parsable by ffprobe
213 if ! check_parsable_audio_ffprobe
"$1"; then
214 die
"FATAL:Not an audio file:$1"; fi;
216 # Convert file to FLAC
217 try ffmpeg
-i "$1" -f "$1".flac
222 # Author: Steven Baltakatei Sandoval