]> zdv2.bktei.com Git - BK-2020-03.git/commitdiff
feat(user/bk-copy-rand-music):Use effective size after downcoding
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Wed, 14 Jan 2026 15:58:02 +0000 (15:58 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Wed, 14 Jan 2026 15:58:02 +0000 (15:58 +0000)
user/bk-copy-rand-music

index f3ddb5576c6b58b144369f52c1db197606d399c1..c99fa389c169aee2178fcf5fc9014337bc6f3e80 100755 (executable)
@@ -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])
 #!/usr/bin/env bash
 # Desc: Copies random audio files
 # Usage: bk-copy-rand-music [dir SOURCE] [dir DEST] [int DURATION] ([int BYTES])
-# Version: 0.5.0
+# Version: 0.6.0
 # Depends: BK-2020-03: bkshuf v0.1.0
 
 declare -Ag appRollCall # Associative array for storing app status
 # Depends: BK-2020-03: bkshuf v0.1.0
 
 declare -Ag appRollCall # Associative array for storing app status
@@ -15,8 +15,8 @@ ext_ignore=".ots\$|.mid\$|.json\$|.gz\$|.jpg\$|.png\$|.asc\$|.pdf\$|.txt\$|.vtt\
 max_filename_length="255"; # max output filename length
 min_file_duration="30"; # minimum duration per music file
 max_file_duration="3600"; # maximum duration per music file
 max_filename_length="255"; # max output filename length
 min_file_duration="30"; # minimum duration per music file
 max_file_duration="3600"; # maximum duration per music file
-min_file_size="100000"; # minimum size per music file (bytes)
-max_file_size="100000000"; # maximum size per music file (bytes)
+min_file_size="100000"; # minimum size per input music file (bytes)
+max_file_size="100000000"; # maximum size per input music file (bytes)
 siz_dest="600000000"; # default destination size limit: 600 MB
 max_find_depth="10"; # max find depth
 limit_bitrate="320000"; # maximum bitrate (bps) for output audio files
 siz_dest="600000000"; # default destination size limit: 600 MB
 max_find_depth="10"; # max find depth
 limit_bitrate="320000"; # maximum bitrate (bps) for output audio files
@@ -301,6 +301,8 @@ get_media_length() {
        die "ERROR:Not a file:$file_in";
     fi;
     ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file_in";
        die "ERROR:Not a file:$file_in";
     fi;
     ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file_in";
+
+    #yell "DEBUG:Finished get_media_length(). $(declare -p file_in)";
 } # Get media container length in seconds via stdout
 checkInt() {
     # Desc: Checks if arg is integer
 } # Get media container length in seconds via stdout
 checkInt() {
     # Desc: Checks if arg is integer
@@ -386,7 +388,7 @@ get_media_bitrate() {
     fi;
     ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "$file_in";
 
     fi;
     ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "$file_in";
 
-    yell "DEBUG:Finished get_media_bitrate with $? on:$1";
+    #yell "DEBUG:Finished get_media_bitrate with $? on:$1";
 } # Get media container length in seconds via stdout
 transcode_copy() {
     # Desc: Transcode high bitrate file into smaller opus file
 } # Get media container length in seconds via stdout
 transcode_copy() {
     # Desc: Transcode high bitrate file into smaller opus file
@@ -497,8 +499,26 @@ main() {
 
         path_candfile="$line"; # path of candidate file
 
 
         path_candfile="$line"; # path of candidate file
 
-        ### Check size
-        siz_cand="$(du -Lb "$path_candfile" | awk '{ print $1 }')"; # size in bytes
+        ### Check duration
+       dur_cand="$(get_media_length "$path_candfile")"; # length in seconds (float)
+       dur_cand="${dur_cand%%.*}"; # convert float to int
+        if ! checkInt "$dur_cand"; then yell "STATUS:Not an int:dur_cand:${dur_cand}"; continue; fi; # reject
+        if [[ "$((dur + dur_cand))" -gt "$dur_dest" ]]; then yell "STATUS:Will put us over on time."; continue; fi; # reject
+       if [[ "$dur_cand" -lt "$min_file_duration" ]]; then yell "STATUS:Duration too short."; continue; fi; # reject
+        if [[ "$dur_cand" -gt "$max_file_duration" ]]; then yell "STATUS:Duration too long."; continue; fi; # reject
+
+        ### Check raw size
+        siz_cand_raw="$(du -Lb "$path_candfile" | awk '{ print $1 }')"; # size in bytes
+       if ! checkInt "$siz_cand_raw"; then continue; fi; # reject
+       if [[ "$siz_cand_raw" -lt "$min_file_size" ]]; then yell "STATUS:Input file too small."; continue; fi; # reject
+        if [[ "$siz_cand_raw" -gt "$max_file_size" ]]; then yell "STATUS:Input file too large."; continue; fi; # reject
+        ### Check effective size
+        bit_cand="$(get_media_bitrate "$path_candfile")"; # bitrate in bps
+        if ! checkInt "$bit_cand"; then die "FATAL:Unable to get bitrate of candidate file. $(declare -p bit_cand path_candfile)"; fi;
+        #### Take into account max output bitrate $limit_bitrate
+        if [[ "$bit_cand" -gt "$limit_bitrate" ]]; then bit_cand="$limit_bitrate"; fi; # assume output bitrate cap
+        siz_cand="$(( (bit_cand * dur_cand) / 8  ))"; # effective size in bytes
        if ! checkInt "$siz_cand"; then continue; fi; # reject
         if [[ "$((siz + siz_cand))" -gt "$siz_dest" ]]; then continue; fi; # reject
        if [[ "$siz_cand" -lt "$min_file_size" ]]; then continue; fi; # reject
        if ! checkInt "$siz_cand"; then continue; fi; # reject
         if [[ "$((siz + siz_cand))" -gt "$siz_dest" ]]; then continue; fi; # reject
        if [[ "$siz_cand" -lt "$min_file_size" ]]; then continue; fi; # reject
@@ -511,14 +531,6 @@ main() {
        file_format="$(get_audio_format "$path_candfile")";
        if ! checkIsInArray "$file_format" "${music_codecs[@]}"; then continue; fi; # reject
 
        file_format="$(get_audio_format "$path_candfile")";
        if ! checkIsInArray "$file_format" "${music_codecs[@]}"; then continue; fi; # reject
 
-        ### Check duration
-       dur_cand="$(get_media_length "$path_candfile")"; # length in seconds (float)
-       dur_cand="${dur_cand%%.*}"; # convert float to int
-        if ! checkInt "$dur_cand"; then continue; fi; # reject
-        if [[ "$((dur + dur_cand))" -gt "$dur_dest" ]]; then continue; fi; # reject
-       if [[ "$dur_cand" -lt "$min_file_duration" ]]; then continue; fi; # reject
-        if [[ "$dur_cand" -gt "$max_file_duration" ]]; then continue; fi; # reject
-
         ### Update stats digits widths
         #### duration
         dur_cand_wnow="$(printf "%s" "$dur_cand" | wc -m)"; # duration width count
         ### Update stats digits widths
         #### duration
         dur_cand_wnow="$(printf "%s" "$dur_cand" | wc -m)"; # duration width count