[encode all] lots more date logic, started a script to correct fixable discrepancies

This commit is contained in:
Luke Tidd 2023-11-18 11:57:45 -05:00
parent 6a7038b599
commit ef79128f12
Signed by: luke
GPG Key ID: 75D6600BEF4E8E8F

View File

@ -4,9 +4,24 @@
ulimit -n "$(ulimit -Hn)" ulimit -n "$(ulimit -Hn)"
. env_parallel.bash . env_parallel.bash
function ts() {
date '+%Y-%m-%d_%H:%M'
}
overwrite_all=true
# overwrite_all=false
debug_only=false
# debug_only=true
src_library="${HOME}/flac" src_library="${HOME}/flac"
output_dir="${HOME}/opus" output_dir="${HOME}/opus"
log="${HOME}/encode_log_$(date '+%Y-%m-%d_%H:%M')" logdir="${HOME}/encoding_logs"
[[ -d "$logdir" ]] || mkdir "$logdir"
start_ts="$(ts)"
log="${logdir}/${start_ts}_encoding_log"
fixes="${logdir}/${start_ts}_fixes.sh"
printf '#!/bin/bash\n# FIXES generated by library_works/encode_all\n\n' > "$fixes"
function encode() { function encode() {
file="$1" file="$1"
@ -14,11 +29,16 @@ function encode() {
# file is full original path + filename # file is full original path + filename
src_dir="$(dirname "$file")" src_dir="$(dirname "$file")"
file_name="$(basename "$file")" file_name="$(basename "$file")"
final_dir="$(sed "s_${HOME}/flac_${HOME}/opus_" <<< "$src_dir")" output_dir="$(sed "s_${HOME}/flac_${HOME}/opus_" <<< "$src_dir")"
final_opus="$(sed -e 's/\.flac$/.opus/' -e "s_${HOME}/flac_${HOME}/opus_" <<< "$file")" final_opus="$(sed \
if [[ -f "$final_opus" ]]; then -e 's/\.flac$/.opus/' \
-e "s_${HOME}/flac_${HOME}/opus_" <<< "$file")"
if [[ -f "$final_opus" && "$overwrite_all" == 'false' ]]; then
printf '%s exists, not reencoding\n' >> "$log"
return return
fi fi
unset tags unset tags
unset dest_dir unset dest_dir
@ -27,36 +47,103 @@ function encode() {
tag_type="$(cut -d= -f1 <<< "$line")" tag_type="$(cut -d= -f1 <<< "$line")"
tag_type="${tag_type^^}" tag_type="${tag_type^^}"
tag_val="$(cut -d= -f2- <<< "$line")" tag_val="$(cut -d= -f2- <<< "$line")"
# TODO: suggest fixes for lower case tags
tags["$tag_type"]="$tag_val" tags["$tag_type"]="$tag_val"
done < <(metaflac --list --block-type=VORBIS_COMMENT "$file" | grep 'comment\[' | cut -d: -f2- | sed 's/^ //') done < <(metaflac --list --block-type='VORBIS_COMMENT' "$file" | \
album="$(basename "$final_dir")" grep 'comment\[' | \
if [[ "$file_name" =~ ^D[0-9]+ ]]; then cut -d: -f2- | \
disk="$(cut -d\ -f1 <<< "$file_name")" sed 's/^ //')
tags[DISK]="${disk#D}" album="$(basename "$output_dir")"
album_dir_date="${album%% *}"
album_dir_date_valid='true'
if [[ ! "$album_dir_date" =~ ^[-0-9]+$ ]]; then
printf '%s invalid album date extracted from album directory name: %s\n' "$file" "$album_dir_date" >> "$log"
album_dir_date_valid='false'
fi fi
declare -a tag_ensure
tag_ensure=( TRACKNUMBER TITLE DATE ALBUM ARTIST ) # FLAC tags to read in order: DATE RELEASEDATE YEAR
for tag_type in "${tag_ensure[@]}"; do album_flac_date_valid='false'
if [[ -z "${tags[$tag_type]}" ]]; then for date_tag in DATE RELEASEDATE YEAR; do
tags[$tag_type]="missing $tag_type" album_flac_date="${tags[$date_tag]}"
if [[ "$album_flac_date" =~ ^[-0-9]+$ ]]; then
album_flac_date_valid='true'
break
fi fi
done done
[[ -d "$final_dir" ]] || mkdir -p "$final_dir" if [[ "$album_flac_date_valid" == 'false' ]]; then
if [[ -n "${tags[DISK]}" ]]; then [[ -n "$album_flac_date" ]] && printf '%s invalid album date extracted from flac data: %s\n' "$file" "$album_flac_date" >> "$log"
err="$(opusenc --quiet --discard-comments --discard-pictures \ [[ ! -n "$album_flac_date" ]] && printf '%s no album date extracted from flac data\n' "$file" >> "$log"
--title "${tags[TITLE]}" --artist "${tags[ARTIST]}" \ fi
--album "${tags[ALBUM]}" --tracknumber "${tags[TRACKNUMBER]}" \
--date "${tags[DATE]}" --comment "DISK=${tags[DISK]}" "$file" "$final_opus" 2>&1)" album_date='unknown'
if [[ "$album_dir_date_valid" && ! "$album_flac_date_valid" ]]; then
printf '%s invalid flac tag date\n' >> "$log"
album_date="$album_dir_date"
if [[ "$file" != *\'* ]]; then
printf "metaflac --set-tag=DATE='%s' '%s'" "$album_date" "$file" >> "$fixes"
elif [[ "$file" != *\"* && "$file" == *\'* ]]; then
printf 'metaflac --set-tag=DATE="%s" "%s"' "$album_date" "$file" >> "$fixes"
elif [[ "$file" == *\"* && "$file" == *\'* ]]; then
printf "# metaflac --set-tag=DATE='%s' '%s'" "$album_date" "$file" >> "$fixes"
printf '# cant quote\n' >> "$fixes"
fi
[[ -n "$album_flac_date" ]] && printf "# old tag: '%s'\n" "$album_flac_date" >> "$fixes"
[[ -z "$album_flac_date" ]] && printf "# no old tag\n" >> "$fixes"
elif [[ ! "$album_dir_date_valid" && "$album_flac_date_valid" ]]; then
printf '%s invalid album dir date\n' "$file" >> "$log"
album_date="$album_flac_date"
elif [[ "$album_dir_date_valid" && "$album_flac_date_valid" ]]; then
album_dir_date_length="$(wc -c <<< "$album_dir_date")"
album_flac_date_length="$(wc -c <<< "$album_flac_date")"
if [[ "$album_dir_date_length" -gt "$album_flac_date_length" ]]; then
printf '%s has a longer date set in album dir name\n' "$file" >> "$log"
album_date="$album_dir_date"
if [[ "$file" != *\'* ]]; then
printf "metaflac --set-tag=DATE='%s' '%s'" "$album_date" "$file" >> "$fixes"
elif [[ "$file" != *\"* && "$file" == *\'* ]]; then
printf 'metaflac --set-tag=DATE="%s" "%s"' "$album_date" "$file" >> "$fixes"
elif [[ "$file" == *\"* && "$file" == *\'* ]]; then
printf "# metaflac --set-tag=DATE='%s' '%s'" "$album_date" "$file" >> "$fixes"
printf '# cant quote\n' >> "$fixes"
fi
printf "# old tag: '%s'\n" "$album_flac_date" >> "$fixes"
else
printf '%s has a longer date set in flac tag\n' "$file" >> "$log"
album_date="$album_flac_date"
fi
fi
if [[ "$file_name" =~ ^D[0-9]+ ]]; then
disk="$(cut -d\ -f1 <<< "$file_name")"
tags['DISK']="${disk#D}"
fi
declare -a tag_ensure
tag_ensure=( 'TRACKNUMBER' 'TITLE' 'ALBUM' 'ARTIST' )
for tag_type in "${tag_ensure[@]}"; do
if [[ -z "${tags["$tag_type"]}" ]]; then
printf '%s is missing tag: %s\n' "$file" "$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' "${album_date}")
[[ -n "${tags[DISK]}" ]] && tag_flags+=('--comment' "DISK=${tags['DISK']}")
if [[ "$debug_only" == 'false' ]]; then
err="$(opusenc --quiet --discard-comments --discard-pictures "${tag_flags[@]}" "$file" "$final_opus" 2>&1)"
else else
err="$(opusenc --quiet --discard-comments --discard-pictures \ echo opusenc --quiet --discard-comments --discard-pictures "${tag_flags[@]}" "$file" "$final_opus"
--title "${tags[TITLE]}" --artist "${tags[ARTIST]}" \
--album "${tags[ALBUM]}" --tracknumber "${tags[TRACKNUMBER]}" \
--date "${tags[DATE]}" "$file" "$final_opus" 2>&1)"
fi fi
if [[ -n "$err" ]]; then if [[ -n "$err" ]]; then
printf '%s\n' "$err" >> "$log" printf '%s\n' "$err" >> "$log"
fi fi
} }
time find "$src_library" -type f -name '*.flac' -print0 | env_parallel -P 0 -0 --bar encode {} find "$src_library" -type f -name '*.flac' -print0 | env_parallel -P 0 -0 --bar encode {}