#!/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.1
# Depends: BK-2020-03: bkshuf v0.1.0
declare -Ag appRollCall # Associative array for storing app status
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
+limit_bitrate="256000"; # maximum bitrate (bps) for output audio files (256000 for opus)
# Load env vars (bkshuf defaults for typical music albums)
if [[ ! -v BKSHUF_PARAM_LINEC ]]; then export BKSHUF_PARAM_LINEC=1000000; fi;
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
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
# var limit_bitrate int max output transcode bitrate (bps)
# Output: file
- ffmpeg -nostdin -i "$1" -c:a libopus -b:a "${limit_bitrate}" "${2}.opus";
+ ffmpeg -nostdin -i "$1" -c:a libopus -b:a "${limit_bitrate}" "${2}.opus";
}; # transcode to lower bitrate audio file
main() {
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
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