2 # Desc: Wrapper for mpv that accepts directory or file paths via posargs or stdin lines
5 # Depends: GNU Parallel, GNU Bash v5.1.16, mpv v0.34.1, bc v1.07.1
6 # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854
7 # Example: find $HOME/Music -type d | bkmpv2
8 # Example: bkmpv2 $HOME/Music/
9 # Example: find $HOME -type f -name "*.mp3" | bkmpv2
10 # Note: Does not follow symlinks
13 firegex
=".+\(aac\|aif\|aiff\|flac\|m4a\|mp3\|mp4\|ogg\|opus\|wav\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u`
14 fsize
="10k"; # default: minimum "10k"
15 fdepth_posarg
="10"; # find depth for positional arguments
16 fdepth_stdin
="1"; # find depth for stdin
17 fc_falloff
="1000"; # characteristic file count falloff in the output
18 fc_redbase
="10"; # logarithm base to reduce output file count
20 export firegex fsize
; # export for parallel
22 #===Declare local functions===
23 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
24 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
25 must
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
27 # Desc: If arg is a command, save result in assoc array 'appRollCall'
28 # Usage: checkapp arg1 arg2 arg3 ...
30 # Input: global assoc. array 'appRollCall'
31 # Output: adds/updates key(value) to global assoc array 'appRollCall'
37 if command -v "$arg" 1>/dev
/null
2>&1; then # Check if arg is a valid command
38 appRollCall
[$arg]="true";
39 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
41 appRollCall
[$arg]="false"; returnState
="false";
45 #===Determine function return code===
46 if [ "$returnState" = "true" ]; then
51 } # Check that app exists
53 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
54 # Usage: checkfile arg1 arg2 arg3 ...
56 # Input: global assoc. array 'fileRollCall'
57 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
58 # Output: returns 0 if app found, 1 otherwise
64 if [ -f "$arg" ]; then
65 fileRollCall
["$arg"]="true";
66 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
68 fileRollCall
["$arg"]="false"; returnState
="false";
72 #===Determine function return code===
73 if [ "$returnState" = "true" ]; then
78 } # Check that file exists
80 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
81 # Usage: checkdir arg1 arg2 arg3 ...
83 # Input: global assoc. array 'dirRollCall'
84 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
85 # Output: returns 0 if app found, 1 otherwise
91 if [ -d "$arg" ]; then
92 dirRollCall
["$arg"]="true";
93 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
95 dirRollCall
["$arg"]="false"; returnState
="false";
99 #===Determine function return code===
100 if [ "$returnState" = "true" ]; then
105 } # Check that dir exists
107 # Desc: Displays missing apps, files, and dirs
108 # Usage: displayMissing
110 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
111 # Output: stderr: messages indicating missing apps, file, or dirs
112 # Depends: bash 5, checkAppFileDir()
113 local missingApps value appMissing missingFiles fileMissing
114 local missingDirs dirMissing
116 #==BEGIN Display errors==
117 #===BEGIN Display Missing Apps===
118 missingApps
="Missing apps :";
119 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
120 for key
in "${!appRollCall[@]}"; do
121 value
="${appRollCall[$key]}";
122 if [ "$value" = "false" ]; then
123 #echo "DEBUG:Missing apps: $key => $value";
124 missingApps
="$missingApps""$key ";
128 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
129 echo "$missingApps" 1>&2;
132 #===END Display Missing Apps===
134 #===BEGIN Display Missing Files===
135 missingFiles
="Missing files:";
136 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
137 for key
in "${!fileRollCall[@]}"; do
138 value
="${fileRollCall[$key]}";
139 if [ "$value" = "false" ]; then
140 #echo "DEBUG:Missing files: $key => $value";
141 missingFiles
="$missingFiles""$key ";
145 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
146 echo "$missingFiles" 1>&2;
149 #===END Display Missing Files===
151 #===BEGIN Display Missing Directories===
152 missingDirs
="Missing dirs:";
153 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
154 for key
in "${!dirRollCall[@]}"; do
155 value
="${dirRollCall[$key]}";
156 if [ "$value" = "false" ]; then
157 #echo "DEBUG:Missing dirs: $key => $value";
158 missingDirs
="$missingDirs""$key ";
162 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
163 echo "$missingDirs" 1>&2;
166 #===END Display Missing Directories===
168 #==END Display errors==
169 } # Display missing apps, files, dirs
171 if ! checkapp mpv parallel bkshuf
bc b2sum
; then
173 die
"FATAL:Missing apps.";
176 }; # check dependencies
178 # Desc: Checks if arg is integer
179 # Usage: checkInt arg
180 # Input: arg: integer
181 # Output: - return code 0 (if arg is integer)
182 # - return code 1 (if arg is not integer)
183 # Example: if ! checkInt $arg; then echo "not int"; fi;
188 if [[ $# -ne 1 ]]; then
189 die
"ERROR:Invalid number of arguments:$#";
192 RETEST1
='^[0-9]+$'; # Regular Expression to test
193 if [[ ! $1 =~
$RETEST1 ]] ; then
199 #===Determine function return code===
200 if [ "$returnState" = "true" ]; then
205 } # Checks if arg is integer
207 # Desc: Consumes stdin; outputs as stdout lines
208 # Input: stdin (consumes)
209 # Output: stdout (newline delimited)
210 # Example: printf "foo\nbar\n" | read_stdin
211 # Depends: GNU bash (version 5.1.16)
213 local input_stdin output
;
216 if [[ -p /dev
/stdin
]]; then
217 input_stdin
="$(cat -)";
220 # Store as output array elements
222 if [[ -n $input_stdin ]]; then
223 while read -r line
; do
225 done < <(printf "%s\n" "$input_stdin");
229 printf "%s\n" "${output[@]}";
230 }; # read stdin to stdout lines
232 # Desc: Reads arguments; outputs as stdout lines
234 # Output: stdout (newline delimited)
235 # Example: read_psarg "$@"
236 # Depends: GNU bash (version 5.1.16)
238 local input_psarg output
;
241 if [[ $# -gt 0 ]]; then
245 # Store as output array elements
246 ## Read in positional arguments
247 if [[ -n $input_psarg ]]; then
254 printf "%s\n" "${output[@]}";
255 }; # read positional argument to stdout lines
257 # Desc: print file list to stdout via `find` using script parameters
258 # Input: arg1: path to dir
260 # var: pattern_find_iregex
262 if [[ ! -d "$1" ]]; then return 1; fi;
263 must
find "$1" -maxdepth "$fdepth" -type f
-iregex "$firegex" -size +"$fsize";
264 }; # print file list to stdout from dir with script parameters
266 # Input: var: firegex find iregex file name pattern
267 # var: fsize find minimum file siz
268 # var: fc_falloff characteristic file count falloff in the output
269 # var: fc_redbase logarithm base to reduce output file count
272 declare -a files_stdin dirs_stdin dirs_psarg
;
273 declare -a paths_files
;
274 declare list_paths_files
;
278 yell
"STATUS:$SECONDS:Started.";
279 #Populate dirs_stdin and dirs_psarg arrays
280 ## Read stdin as lines
281 re_dotfile
="^\."; # first char is a dot
282 while read -r line
; do
283 line
="$(readlink -e "$line")";
284 line_bn
="$(basename "$line")";
285 # Check if dir and not dotfile
286 if [[ -d "$line" ]] && [[ ! "$line_bn" =~
$re_dotfile ]]; then
287 dirs_stdin
+=("$line");
292 if [[ -f "$line" ]]; then
293 files_stdin
+=("$line");
298 yell
"WARNING:Not a valid dir or file:$line";
299 done < <( read_stdin
; read_psarg
"$@"; );
300 yell
"STATUS:$SECONDS:Read stdin and psargs.";
302 # Catch all arrays empty
303 if [[ "${#dirs_stdin[@]}" -le 0 ]] && \
304 [[ "${#dirs_psarg[@]}" -le 0 ]] && \
305 [[ "${#files_stdin[@]}" -le 0 ]]; then
306 die
"FATAL:No valid directories or files provided.";
310 ## Add stdin argument input
311 if [[ "${#files_stdin[@]}" -gt 0 ]]; then
312 paths_files
+=("${files_stdin[@]}");
315 ## Call find_filelist() in parallel for positional argument input
316 if [[ "${#dirs_psarg[@]}" -gt 0 ]]; then
317 fdepth
="$fdepth_posarg"; export fdepth
; # for dirs from positional arguments
318 paths_files
+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_psarg[@]}" )"); # See [1]
320 ## Call find_filelist() in parallel for stdin input
321 if [[ "${#dirs_stdin[@]}" -gt 0 ]]; then
322 fdepth
="$fdepth_stdin"; export fdepth
; # 1 for dirs from stdin
323 paths_files
+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_stdin[@]}" )"); # See [1]
326 # Convert paths_files array into file list
327 for i
in "${!paths_files[@]}"; do
328 list_paths_files
="$(printf "%s
\n%s
" "${paths_files[$i]}" "$list_paths_files")";
331 fc="$(wc -l < <(echo -n "$list_paths_files"))";
332 echo "STATUS:$SECONDS:file count:fc:$fc"
333 yell
"STATUS:$SECONDS:Generated file list.";
335 # Sort, remove duplicate paths
336 list_paths_files
="$(echo "$list_paths_files" | sort -u | tr -s '\n')";
337 yell
"STATUS:$SECONDS:Sorted and deduplicated list.";
339 # Remove paths with dotfiles
340 list_paths_files
="$(echo "$list_paths_files" | grep -viE "/\.
" )";
341 yell
"STATUS:$SECONDS:Removed dotfiles.";
344 list_paths_files_tmp
="/dev/shm/$(date +%Y%m%dT%H%M%S.%N%z)"..mpv_paths.txt
;
345 ## Reduce output file count if greater than falloff
346 if [[ $fc -gt $fc_falloff ]]; then
347 bc_exp
="$fc_falloff * (1 + l( $fc / $fc_falloff )/l($fc_redbase))";
348 fc_out
="$( echo "$bc_exp" | bc -l )";
352 ## Reduce output file count by fixed fraction (bkshuf optimization)
353 fc_out
="$(echo "$fc_out * 0.75" | bc -l)";
354 ## Select subset via bkshuf
355 ### Specify bkshuf-specific environment variables
356 export BKSHUF_PARAM_LINC
=1000000; # for these numbers of lines of input..
357 export BKSHUF_PARAM_GSIZE
=25; # target this group size
358 ### Round file count down
359 fc_out
="$(printf "%.0f
" "$fc_out")"; # round float down to int
360 ### Get neighbor-preserving shuffled subset (size $fc_out)
361 yell
"STATUS:$SECONDS:Selecting $fc_out of $fc files via bkshuf...";
363 echo -n "$list_paths_files" | \
364 bkshuf
"$fc_out" > "$list_paths_files_tmp";
365 yell
"STATUS:$SECONDS:Wrote playlist.";
368 yell
"STATUS:$SECONDS:Built file list in $SECONDS seconds.";
370 # Run mpv with filelist
373 cmd_args
+=("--audio-display=no"); # disable video for audio files
374 cmd_args
+=("--vid=no"); # donʼt show video track
375 cmd_args
+=("--image-display-duration=0"); # don't show album art
376 cmd_args
+=("--af=scaletempo=stride=15:overlap=1:search=15"); # improve scrubbing
377 cmd_args
+=("--playlist=$list_paths_files_tmp"); # read playlist
378 declare -p cmd_args
; # debug
380 must
"${cmd_args[@]}";
381 must
rm "$list_paths_files_tmp";
383 export -f yell die must read_stdin read_psarg find_flist
;
387 # Author: Steven Baltakatei Sandoval