2 # Desc: Copies random audio files
3 # Usage: bk-copy-rand-music.sh [dir SOURCE] [dir DEST] [int DURATION]
6 declare -Ag appRollCall
# Associative array for storing app status
7 declare -Ag fileRollCall
# Associative array for storing file status
8 declare -Ag dirRollCall
# Associative array for storing dir status
9 declare -a music_codecs
# Array for storing valid codec names (e.g. "aac" "mp3")
11 # Adjustable parameters
12 music_codecs
=("vorbis" "aac" "mp3" "flac" "opus"); # whitelist of valid codec_names ffprobe might return
13 max_loops
="1000000"; # max number of files to test whether are audio or not
14 max_filename_length
="255"; # max output filename length
15 min_file_duration
="10"; # minimum duration per music file
17 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
18 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
19 try
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
21 # Desc: If arg is a command, save result in assoc array 'appRollCall'
22 # Usage: checkapp arg1 arg2 arg3 ...
24 # Input: global assoc. array 'appRollCall'
25 # Output: adds/updates key(value) to global assoc array 'appRollCall'
31 if command -v "$arg" 1>/dev
/null
2>&1; then # Check if arg is a valid command
32 appRollCall
[$arg]="true";
33 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
35 appRollCall
[$arg]="false"; returnState
="false";
39 #===Determine function return code===
40 if [ "$returnState" = "true" ]; then
45 } # Check that app exists
47 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
48 # Usage: checkfile arg1 arg2 arg3 ...
50 # Input: global assoc. array 'fileRollCall'
51 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
52 # Output: returns 0 if app found, 1 otherwise
58 if [ -f "$arg" ]; then
59 fileRollCall
["$arg"]="true";
60 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
62 fileRollCall
["$arg"]="false"; returnState
="false";
66 #===Determine function return code===
67 if [ "$returnState" = "true" ]; then
72 } # Check that file exists
74 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
75 # Usage: checkdir arg1 arg2 arg3 ...
77 # Input: global assoc. array 'dirRollCall'
78 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
79 # Output: returns 0 if all args are dirs; 1 otherwise
85 if [ -z "$arg" ]; then
86 dirRollCall
["(Unspecified Dirname(s))"]="false"; returnState
="false";
87 elif [ -d "$arg" ]; then
88 dirRollCall
["$arg"]="true";
89 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
91 dirRollCall
["$arg"]="false"; returnState
="false";
95 #===Determine function return code===
96 if [ "$returnState" = "true" ]; then
101 } # Check that dir exists
103 # Desc: Displays missing apps, files, and dirs
104 # Usage: displayMissing
106 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
107 # Output: stderr: messages indicating missing apps, file, or dirs
108 # Output: returns exit code 0 if nothing missing; 1 otherwise
109 # Depends: bash 5, checkAppFileDir()
110 local missingApps value appMissing missingFiles fileMissing
111 local missingDirs dirMissing
113 #==BEGIN Display errors==
114 #===BEGIN Display Missing Apps===
115 missingApps
="Missing apps :";
116 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
117 for key
in "${!appRollCall[@]}"; do
118 value
="${appRollCall[$key]}";
119 if [ "$value" = "false" ]; then
120 #echo "DEBUG:Missing apps: $key => $value";
121 missingApps
="$missingApps""$key ";
125 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
126 echo "$missingApps" 1>&2;
129 #===END Display Missing Apps===
131 #===BEGIN Display Missing Files===
132 missingFiles
="Missing files:";
133 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
134 for key
in "${!fileRollCall[@]}"; do
135 value
="${fileRollCall[$key]}";
136 if [ "$value" = "false" ]; then
137 #echo "DEBUG:Missing files: $key => $value";
138 missingFiles
="$missingFiles""$key ";
142 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
143 echo "$missingFiles" 1>&2;
146 #===END Display Missing Files===
148 #===BEGIN Display Missing Directories===
149 missingDirs
="Missing dirs:";
150 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
151 for key
in "${!dirRollCall[@]}"; do
152 value
="${dirRollCall[$key]}";
153 if [ "$value" = "false" ]; then
154 #echo "DEBUG:Missing dirs: $key => $value";
155 missingDirs
="$missingDirs""$key ";
159 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
160 echo "$missingDirs" 1>&2;
163 #===END Display Missing Directories===
165 #==END Display errors==
166 #==BEGIN Determine function return code===
167 if [ "$appMissing" == "true" ] ||
[ "$fileMissing" == "true" ] ||
[ "$dirMissing" == "true" ]; then
172 #==END Determine function return code===
173 } # Display missing apps, files, dirs
175 # Desc: Display script usage information
180 # Depends: GNU-coreutils 8.30 (cat)
184 This script may be used to copy a random selection of files containing
185 audio tracks from SOURCE to DEST.
188 bk-copy-rand-music [dir SOURCE] [dir DEST] [int DURATION]
191 bk-copy-rand-music ~/Music /tmp/music-sample 3600
197 } # Display information on how to use this script.
198 check_parsable_audio_ffprobe
() {
199 # Desc: Checks if ffprobe returns valid audio codec name for file
200 # Usage: check_parsable_audio_ffprobe [path FILE]
202 # Input: arg1: file path
203 # Output: exit code 0 if returns valid codec name; 1 otherwise
204 # Depends: ffprobe, die()
205 local file_in ffprobe_out
207 if [[ $# -ne 1 ]]; then die
"ERROR:Invalid number of args:$#"; fi;
211 # Check if ffprobe detects an audio stream
212 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
215 return_state
="false";
218 # Fail if ffprobe returns no result
219 ffprobe_out
="$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 "$file_in")";
220 if [[ -z $ffprobe_out ]]; then
221 return_state
="false";
225 if [[ $return_state = "true" ]]; then
230 } # Checks if file has valid codec name using ffprobe
232 # Desc: Gets audio format of file as string
233 # Usage: get_audio_format arg1
236 # Input: arg1: input file path
237 # Output: stdout (if valid audio format)
238 # exit code 0 if audio file; 1 otherwise
239 # Example: get_audio_format myvideo.mp4
240 # Note: Would return "opus" if full ffprobe report had 'Audio: opus, 48000 Hz, stereo, fltp'
241 # Note: Not tested with videos containing multiple video streams
242 # Ref/Attrib: [1] https://stackoverflow.com/questions/5618363/is-there-a-way-to-use-ffmpeg-to-determine-the-encoding-of-a-file-before-transcod
243 # [2] https://stackoverflow.com/questions/44123532/how-to-find-out-the-file-extension-for-extracting-audio-tracks-with-ffmpeg-and-p#comment88464070_50723126
244 local audio_format file_in
;
248 # Return error exit code if not audio file
249 ## Return error if ffprobe itself exited on error
250 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
251 return_state
="false";
255 audio_format
="$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 "$file_in")"; # see [1]
257 ## Return error if audio format is incorrectly formatted (e.g. reject if contains spaces)
258 pattern
="^[[:alnum:]]+$"; # alphanumeric string with no spaces
259 if [[ $audio_format =~
$pattern ]]; then
261 # Report audio format
262 echo "$audio_format";
264 return_state
="false";
268 if [[ $return_state = "true" ]]; then
273 } # Get audio format as stdout
275 # Use ffprobe to get media container length in seconds (float)
276 # Usage: get_media_length arg1
277 # Input: arg1: path to file
278 # Output: stdout: seconds (float)
279 # Depends: ffprobe 4.1.8
280 # Ref/Attrib: [1] How to get video duration in seconds? https://superuser.com/a/945604
283 if [[ ! -f $file_in ]]; then
284 die
"ERROR:Not a file:$file_in";
286 ffprobe
-v error
-show_entries format
=duration
-of default
=noprint_wrappers
=1:nokey
=1 "$file_in";
287 } # Get media container length in seconds via stdout
289 # Desc: Checks if arg is integer
290 # Usage: checkInt arg
291 # Input: arg: integer
292 # Output: - return code 0 (if arg is integer)
293 # - return code 1 (if arg is not integer)
294 # Example: if ! checkInt $arg; then echo "not int"; fi;
299 if [[ $# -ne 1 ]]; then
300 die
"ERROR:Invalid number of arguments:$#";
303 RETEST1
='^[0-9]+$'; # Regular Expression to test
304 if [[ ! $1 =~
$RETEST1 ]] ; then
310 #===Determine function return code===
311 if [ "$returnState" = "true" ]; then
316 } # Checks if arg is integer
318 # Desc: Checks if input arg is element in array
319 # Usage: checkIsInArray arg1 arg2
321 # Input: arg1: test string
323 # Output: exit code 0 if test string is in array; 1 otherwise
324 # Example: checkIsInArray "foo" "${myArray[@]}"
325 # Ref/Attrib: [1] How do I check if variable is an array? https://stackoverflow.com/a/27254437
326 # [2] How to pass an array as function argument? https://askubuntu.com/a/674347
327 local return_state input arg1 string_test
328 declare -a arg2 array_test
329 input
=("$@") # See [2]
331 arg2
=("${input[@]:1}");
332 #yell "DEBUG:input:${input[@]}";
333 #yell "DEBUG:arg1:${arg1[@]}";
334 #yell "DEBUG:arg2:${arg2[@]}";
337 array_test
=("${arg2[@]}");
339 #yell "DEBUG:string_test:$string_test";
340 #yell "DEBUG:$(declare -p array_test)";
341 for element
in "${array_test[@]}"; do
342 #yell "DEBUG:element:$element";
343 if [[ "$element" =~ ^
"$string_test" ]]; then
350 if [[ $return_state == "true" ]]; then
355 } # Check if string is element in array
358 # Input: arg1: path to source tree
359 # arg2: path to destination tree
360 # arg3: cumulative duration (seconds) of audio files in destination tree
361 # assoc arrays: appRollCall, fileRollCall, dirRollCall
363 # Depends: yell(), checkdir() 0.1.2, displayMissing() 1.0.0, GNU Coreutils 8.30 (shuf)
364 local arg1 arg2 arg3 dur_dest dir_source dir_dest list_all
365 declare -a list_files
# array for files to be considered
366 declare -A list_copy
# assoc array for files to be copied (key=path; value=duration)
372 if [[ $# -ne 3 ]]; then showUsage
; die
"ERROR:Invalid number of args."; fi;
375 if checkInt
"$arg3"; then
378 yell
"ERROR:Duration (seconds) not an int:$arg3"
382 if checkdir
"$arg1" "$arg2"; then
386 yell
"ERROR:Directory error";
392 if ! displayMissing
; then
394 die
"ERROR:Check missing resources.";
397 yell
"STATUS:Working...";
399 # Generate file path list
400 list_all
="$(find -L "$dir_source")";
401 #yell "DEBUG:list_files_rel:$list_files_rel";
403 # Prune list_all of non-files and save as array list_files
404 while read -r line
; do
405 #yell "DEBUG:line:$line";
406 if ! [[ -f $line ]]; then
407 #yell "DEBUG:Not a file:$line";
411 list_files
+=("$line");
412 done < <(echo "$list_all");
414 # Randomly test and add elements of list_files array to list_copy
415 dur
=0; # Initialize duration
416 n
=0; # Initialize loop counter
417 ## Get element count of list_files array
418 list_files_count
="${#list_files[@]}";
419 while [[ $dur -le $dur_dest ]]; do
420 #yell "DEBUG:list_copy building loop:$n";
421 ### Select random element of list_files array
422 list_files_index
="$(shuf -i 1-"$list_files_count" -n1)";
423 list_files_index
="$((list_files_index - 1))"; # bash arrays are zero-indexed
424 path_candfile
="${list_files[$list_files_index]}"; # path of candidate file
426 ### Check if has valid codec
427 if ! check_parsable_audio_ffprobe
"$path_candfile"; then continue; fi; # reject
429 ### Check if desired codec
430 file_format
="$(get_audio_format "$path_candfile")";
431 if ! checkIsInArray
"$file_format" "${music_codecs[@]}"; then continue; fi; # reject
433 ### Check and save duration
434 dur_cand
="$(get_media_length "$path_candfile")";
435 dur_cand
="${dur_cand%%.*}"; # convert float to int
436 if ! checkInt
"$dur_cand"; then continue; fi; # reject
437 if [[ "$dur_cand" -lt "$min_file_duration" ]]; then continue; fi; # reject
439 ### Add/update candfile to list_copy assoc. array (key=path; value=duration)
440 #yell "DEBUG:Adding $path_candfile";
441 list_copy
["$path_candfile"]="$dur_cand";
443 ### Update total duration $dur by summing all list_copy assoc. array values
445 for value
in "${list_copy[@]}"; do
446 dur
="$((dur + value))";
448 #yell "DEBUG:dur:$dur";
452 if [[ $n -gt $max_loops ]]; then die
"ERROR:Too many loops:$n"; fi;
455 n
=0; # Initialize loop counter
456 # Copy files in list_copy to dir_dest;
457 for key
in "${!list_copy[@]}"; do
458 value
="${list_copy[$key]}";
459 ## Get basename of path
460 file_basename
="$(basename "$key")";
462 ## Get 16-character b2sum fingerprint (for different files that share basename)
463 fingerprint
="$(b2sum -l64 "$key" | cut -d' ' -f1)";
465 ## Form output filename
466 file_name
="$fingerprint"..
"$file_basename";
467 file_name
="${file_name:0:$max_filename_length}"; # Limit filename length (e.g. Windows has max of 255 characters)
470 path_output
="$dir_dest"/"$file_name";
473 try
cp "$key" "$path_output" && yell
"NOTICE:Copied ($value seconds): $key ";
474 #yell "DEBUG:Copied $file_basename to $dur_dest.";
477 path_log_output
="$dir_dest"/COPY.log
;
478 if [[ $n -le 0 ]]; then
479 echo "fingerprint","duration","original_path" >> "$path_log_output";
481 echo "$fingerprint","$value","$key" >> "$path_log_output";
485 unset file_basename path_output
488 # Report total duration
489 yell
"NOTICE:Total duration (seconds):$dur";
495 # Author: Steven Baltakatei Sandoval