From 48dab430af0eb368f87d6e26eb7a7afa8b728a60 Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Sat, 18 Feb 2023 17:59:52 +0000 Subject: [PATCH 1/1] feat(user/bk-copy-rand-music): Use env vars for bkshuf params - fix(bk-copy-rand-music):Fix upper size and duration cutoffs - feat(unitproc/bkshuf):Detect env vars --- unitproc/bkshuf | 9 +++++-- user/bk-copy-rand-music | 56 ++++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/unitproc/bkshuf b/unitproc/bkshuf index 8aaac9d..e41d725 100644 --- a/unitproc/bkshuf +++ b/unitproc/bkshuf @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Desc: Mixes input lines while also preserving some neighbors # Usage: cat file | bkshuf arg1 -# Version 0.1.0 +# Version 0.1.1 # Depends: bc 1.07.1, GNU Coreutils 8.32 (shuf) # Input: var: arg1 initial lines to output @@ -124,7 +124,12 @@ main() { else lc_out_max="$1"; # output line count fi; - + + # Check env vars + if ! checkInt "$BKSHUF_PARAM_LINEC"; then + die "FATAL:Not an int:BKSHUF_PARAM_LINEC:$BKSHUF_PARAM_LINEC"; fi; + if ! checkInt "$BKSHUF_PARAM_GSIZE"; then + die "FATAL:Not an int:BKSHUF_PARAM_LINEC:$BKSHUF_PARAM_GSIZE"; fi; # store input lines from stdin (like `shuf`) while read -r line; do diff --git a/user/bk-copy-rand-music b/user/bk-copy-rand-music index 18f95f3..bc21e5b 100644 --- a/user/bk-copy-rand-music +++ b/user/bk-copy-rand-music @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Desc: Copies random audio files # Usage: bk-copy-rand-music [dir SOURCE] [dir DEST] [int DURATION] ([int BYTES]) -# Version: 0.1.0 +# Version: 0.1.1 # Depends: BK-2020-03: bkshuf v0.1.0 declare -Ag appRollCall # Associative array for storing app status @@ -19,6 +19,9 @@ max_file_size="100000000"; # maximum size per music file (bytes) siz_dest="600000000"; # default destination size limit: 600 MB max_find_depth="10"; # max find depth +# Load env vars (bkshuf defaults for typical music albums) +if [[ ! -v BKSHUF_PARAM_LINEC ]]; then export BKSHUF_PARAM_LINEC=1000000; fi; +if [[ ! -v BKSHUF_PARAM_GSIZE ]]; then export BKSHUF_PARAM_GSIZE=10; fi; yell() { echo "$0: $*" >&2; } # print script path and all args to stderr die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status @@ -200,6 +203,10 @@ showUsage() { DEPENDENCIES: ffprobe GNU Coreutils 8.30 + + ENVIRONMENT VARIABLES + BKSHUF_PARAM_LINEC (see `bkshuf` in BK-2020-03) + BKSHUF_PARAM_GSIZE (see `bkshuf` in BK-2020-03) EOF } # Display information on how to use this script. check_parsable_audio_ffprobe() { @@ -367,8 +374,8 @@ main() { # arg3: cumulative duration (seconds) of audio files in destination tree # arg4: cumulative size (bytes) of audio files in destination tree (optional) # assoc arrays: appRollCall, fileRollCall, dirRollCall - # env.var: BKSHUF_PARAM_LINEC - # BKSHUF_PARAM_GSIZE + # env.var: BKSHUF_PARAM_LINEC (bkshuf) + # BKSHUF_PARAM_GSIZE (bkshuf) # arrays: music_codecs # vars: max_filename_length, min_file_duration, max_file_duration, # min_file_size, max_file_size, siz_dest, max_find_depth @@ -377,20 +384,28 @@ main() { # BK-2020-03: bkshuf v0.1.0 local arg1 arg2 arg3 dur_dest dir_source dir_dest declare -a list_files # array for files to be considered - declare -a list_copy_sa # simple array for files to be copied (string: "$dur,$path") + declare -a list_copy # array for files to be copied (string: "$dur,$fsize,$path") # Parse args arg1="$1"; arg2="$2"; arg3="$3"; arg4="$4"; - if ! ([[ $# -eq 3 ]] || [[ $# -eq 4 ]]); then showUsage; die "ERROR:Invalid number of args:$#"; fi; - + if ! { [[ $# -eq 3 ]] || [[ $# -eq 4 ]]; }; then + showUsage; + die "ERROR:Invalid number of args:$#"; fi; + + # Check env vars + if ! checkInt "$BKSHUF_PARAM_LINEC"; then + die "FATAL:Not an int:BKSHUF_PARAM_LINEC:$BKSHUF_PARAM_LINEC"; fi; + if ! checkInt "$BKSHUF_PARAM_GSIZE"; then + die "FATAL:Not an int:BKSHUF_PARAM_LINEC:$BKSHUF_PARAM_GSIZE"; fi; + ## Check duration if checkInt "$arg3"; then dur_dest="$arg3"; else - yell "ERROR:Duration (seconds) not an int:$arg3" + die "FATAL:Duration (seconds) not an int:$arg3" fi; ## Check size @@ -398,7 +413,7 @@ main() { if checkInt "$arg4"; then siz_dest="$arg4"; else - yell "ERROR:Size (bytes) not an int:$arg4"; + die "FATAL:Size (bytes) not an int:$arg4"; fi; fi; @@ -450,7 +465,7 @@ main() { ### Check and save duration dur_cand="$(get_media_length "$path_candfile")"; dur_cand="${dur_cand%%.*}"; # convert float to int - if [[ "$((dur + dur_cand))" -gt "$dur_dest" ]]; then continue; fi; # reject + if [[ "$((dur + dur_cand))" -gt "$dur_dest" ]]; then break; fi; # no more dur_cand_wnow="$(printf "%s" "$dur_cand" | wc -m)"; # duration width count if [[ $dur_cand_wnow -gt $dur_cand_w ]]; then dur_cand_w="$dur_cand_wnow"; fi; @@ -460,6 +475,7 @@ main() { ### Check and save size siz_cand="$(du -b "$path_candfile" | awk '{ print $1 }')"; # size in bytes + if [[ "$((siz + siz_cand))" -gt "$siz_dest" ]]; then break; fi; # no more siz_cand_wnow="$(printf "%s" "$siz_cand" | wc -m)"; # size width count if [[ $siz_cand_wnow -gt $siz_cand_w ]]; then siz_cand_w="$siz_cand_wnow"; fi; @@ -468,9 +484,12 @@ main() { if [[ "$siz_cand" -gt "$max_file_size" ]]; then continue; fi; # reject ### Add/update candfile to array: - ### list_copy_sa (simple array with only paths) + ### list_copy (array with "duration, size, path") #yell "DEBUG:Adding $path_candfile"; - list_copy_sa+=("$dur_cand,$siz_cand,$path_candfile"); # for copying with order + #printf "DEBUG:%8d,%8d,%s\n" "$dur_cand" "$siz_cand" "$path_candfile" 1>&2; + #printf "DEBUG:dur:%s\n" "$dur" 1>&2; + #printf "DEBUG:siz:%s\n" "$siz" 1>&2; + list_copy+=("$dur_cand,$siz_cand,$path_candfile"); # for copying with order ### Update total duration $dur and total size $siz dur="$((dur + dur_cand))"; @@ -481,14 +500,17 @@ main() { ((n++)); done < <(printf "%s\n" "${list_files[@]}" | bkshuf); + #yell "DEBUG:BKSHUF_PARAM_LINEC:$BKSHUF_PARAM_LINEC"; + #yell "DEBUG:BKSHUF_PARAM_GSIZE:$BKSHUF_PARAM_GSIZE"; + n=0; # Initialize loop counter - num_w="$(printf "%s" "${#list_copy_sa[@]}" | wc -m)"; # init file number format + num_w="$(printf "%s" "${#list_copy[@]}" | wc -m)"; # init file number format num_fmt="%0""$num_w""d"; path_log_output="$dir_dest"/COPY.log; printf "num,fingerprint,duration,size,original_path\n" >> "$path_log_output"; # Copy files in list_copy to dir_dest; while read -r line; do - yell "DEBUG:line:$line"; # debug + #yell "DEBUG:line:$line"; # debug fdur="$(printf "%s" "$line" | cut -d',' -f1)"; fsize="$(printf "%s" "$line" | cut -d',' -f2)"; fpath="$(printf "%s" "$line" | cut -d',' -f3-)"; @@ -513,12 +535,11 @@ main() { ## Append log fpath_can="$(readlink -f "$fpath")"; # resolve symlinks to canonical path log_fmt="%s,%s,%""$dur_cand_w""d,%""$siz_cand_w""d,%s\n"; # e.g. "%s,%3d,%5d,%s" if dur_cand_w=3 and siz_cand_w=5 - #yell "DEBUG:log_fmt:$log_fmt"; sleep 10; # debug printf "$log_fmt" "$num" "$fingerprint" "$fdur" "$fsize" "$fpath_can" >> "$path_log_output"; ((n++)); unset file_basename path_output - done < <(printf "%s\n" "${list_copy_sa[@]}"); + done < <(printf "%s\n" "${list_copy[@]}"); # Report total duration and size yell "NOTICE:Total duration (seconds):$dur"; @@ -530,3 +551,8 @@ main "$@"; # Author: Steven Baltakatei Sandoval # License: GPLv3+ + +# bkshuf v0.1.0 +# Author: Steven Baltakatei Sandoval +# License: GPLv3+ +# URL: https://gitlab.com/baltakatei/baltakatei-exdev/-/blob/b9e8b771e985fcdf26ba8b9ccb8e31b62da757d3/unitproc/bkshuf -- 2.30.2