From: Steven Baltakatei Sandoval Date: Mon, 10 Jun 2024 20:37:05 +0000 (+0000) Subject: feat(user/bkmpv2):Filter audio files by file extension X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/e41f7d0594245a0c00cb82f44cee251e11300314?ds=sidebyside;hp=75baf70102a9c94e694c9ac39deae404176b815b feat(user/bkmpv2):Filter audio files by file extension --- diff --git a/user/bkmpv2 b/user/bkmpv2 index d44fff7..e4128f0 100755 --- a/user/bkmpv2 +++ b/user/bkmpv2 @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Desc: Wrapper for mpv that accepts directory or file paths via posargs or stdin lines # Usage: bkmpv2 [DIR] -# Version: 0.1.0 +# Version: 0.2.0 # Depends: GNU Parallel, GNU Bash v5.1.16, mpv v0.34.1, bc v1.07.1 # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854 # Example: find $HOME/Music -type d | bkmpv2 @@ -10,7 +10,8 @@ # Note: Does not follow symlinks # Find settings -firegex=".+\(aac\|aif\|aiff\|flac\|m4a\|mp3\|mp4\|ogg\|opus\|wav\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u` +firegex=".+\(aac\|aif\|aiff\|flac\|m4a\|mp3\|mp4\|ogg\|opus\|wav\)$"; # POSIX regex for find. Update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u` +file_regex=".+(aac|aif|aiff|flac|m4a|mp3|mp4|ogg|opus|wav)$"; # extended regex for Bash. fsize="10k"; # default: minimum "10k" fdepth_posarg="10"; # find depth for positional arguments fdepth_stdin="1"; # find depth for stdin @@ -262,6 +263,21 @@ find_flist() { if [[ ! -d "$1" ]]; then return 1; fi; must find "$1" -maxdepth "$fdepth" -type f -iregex "$firegex" -size +"$fsize"; }; # print file list to stdout from dir with script parameters +check_files() { + # Desc: Applies $file_regex to files specified by path + # Input: var: file_regex + # array: files_stdin + # Output: array: files_stdin + local file; + declare -a filtered_files_stdin; + + for file in "${files_stdin[@]}"; do + if [[ "$file" =~ $file_regex ]]; then + filtered_files_stdin+=("$file"); + fi; + done; + files_stdin=("${filtered_files_stdin[@]}"); +}; # apply $firegex to files_stdin array main() { # Input: var: firegex find iregex file name pattern # var: fsize find minimum file siz @@ -299,6 +315,9 @@ main() { done < <( read_stdin; read_psarg "$@"; ); yell "STATUS:$SECONDS:Read stdin and psargs."; + # Apply the $file_regex to $files_stdin array + check_files; + # Catch all arrays empty if [[ "${#dirs_stdin[@]}" -le 0 ]] && \ [[ "${#dirs_psarg[@]}" -le 0 ]] && \