#!/bin/bash # encode_all encodes the whole library into specified format # ulimit -n "$(ulimit -Hn)" . env_parallel.bash function ts() { date '+%Y-%m-%d_%H:%M' } debug_only=false # debug_only=true src_dir="$1" export output_dir="$2" if [[ -z "$src_dir" || -z "$output_dir" ]]; then printf 'Usage:\nencode_dir \n' >&2 exit 1 fi logdir="${HOME}/encoding_logs" [[ -d "$logdir" ]] || mkdir "$logdir" start_ts="$(ts)" log="${logdir}/${start_ts}_encoding_log" fixes="${logdir}/${start_ts}_fixes.sh" temp_dir="$(mktemp -d)" printf '#!/bin/bash\n# FIXES generated by library_works/encode_all\n\n' > "$fixes" function encode() { src_file_and_path="$1" src_file="$(basename "$in_file_path")" # file is full original path + filename src_dir="$(dirname "$src_file_and_path")" file_name="$(basename "$src_file_and_path")" final_opus="${output_dir}/$(sed -e 's/\.flac$/.opus/' <<< "$file_name")" unset tags unset dest_dir declare -A tags while read -r line; do tag_type="$(cut -d= -f1 <<< "$line")" tag_type="${tag_type^^}" tag_val="$(cut -d= -f2- <<< "$line")" # TODO: suggest fixes for lower case tags tags["$tag_type"]="$tag_val" done < <(metaflac --list --block-type='VORBIS_COMMENT' "$src_file_and_path" | \ grep 'comment\[' | \ cut -d: -f2- | \ sed 's/^ //') tag_ensure=( 'TRACKNUMBER' 'TITLE' 'ALBUM' 'ARTIST' 'YEAR') for tag_type in "${tag_ensure[@]}"; do if [[ -z "${tags["$tag_type"]}" ]]; then printf '%s is missing tag: %s\n' "$src_file_and_path" "$tag_type" >> "$log" tags["$tag_type"]="missing $tag_type" fi done [[ -d "$output_dir" ]] || mkdir -p "$output_dir" tag_flags=( '--title' "${tags['TITLE']}" '--artist' "${tags['ARTIST']}" '--album' "${tags['ALBUM']}" '--tracknumber' "${tags['TRACKNUMBER']}" '--date' "${tags['DATE']}") [[ -n "${tags[DISK]}" ]] && tag_flags+=('--comment' "DISK=${tags['DISK']}") tags='' for tag in "${tag_flags[@]}"; do tags+="$(printf '"%s" ' "$tag")" done cmd=$(printf 'opusenc --downmix-mono --bitrate 48 --speech --quiet --discard-comments --discard-pictures %s "%s" "%s"\n' "$tags" "$src_file_and_path" "$final_opus") printf '%s\n' "$cmd" eval $cmd if [[ -n "$err" ]]; then printf '%s\n' "$err" >> "$log" fi } echo starting processing # find "$src_dir" -type f -name '*.flac' | while read line; do encode "${line}"; done # exit 1 # find "$src_dir" -type f -name '*.flac' | while read line; do encode "$line"; done # wait find "$src_dir" -type f -name '*.flac' -print0 | env_parallel -P 0 -0 --bar encode {} echo done processing echo deleting any empty directories # find "$output_dir" -empty -type d -delete