Skip to content

Instantly share code, notes, and snippets.

@elundmark
Created September 9, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elundmark/ec8ef054f697cf0b2be6bcfe2dc4af3d to your computer and use it in GitHub Desktop.
Save elundmark/ec8ef054f697cf0b2be6bcfe2dc4af3d to your computer and use it in GitHub Desktop.
Script to embed images in audio files using tageditor cli and chafa!
#!/usr/bin/env bash
declare tageditor=/home/user/bin/tageditor-latest-x86_64.AppImage
if ! command -v chafa &>/dev/null || [[ ! -x "$tageditor" ]] ; then
echo "Missing programs"
exit 1
fi
declare -a args=() error_files=()
declare -i arglen=0 i=0
declare cover_img
declare valid_ext='.\.(mp3|MP3|m4a|M4A|m4b|M4B|aac|AAC|flac|FLAC|ogg|OGG|webm|WEBM|opus|OPUS)$'
while getopts ":f:" options ; do
case "$options" in
f) cover_img="$OPTARG";;
*) exit 1;;
esac
done
args+=( "${@:OPTIND}" )
arglen=${#args[@]}
if [[ -z "$cover_img" ]]; then
for try_cover in cover.jpg Folder.jpg album.jpg; do
cover_img="$(dirname "${args[0]}")/${try_cover}"
[[ -f "$cover_img" ]] && break
done
fi
if [[ ! -f "$cover_img" ]] ; then
echo "No cover found or specified - press any key to exit"
while read -n 1 -r; do break; done
exit 33
fi
chafa "$cover_img" || exit 99
if ! confirm -c -q "Embed this image in (${arglen}) selected tracks?"; then
exit 1
fi
for (( i = 0; i < arglen; i++ )) ; do
if [[ ${args[i]} =~ $valid_ext ]] ; then
# remove existing imag first
if "$tageditor" set -n cover= --files "${args[i]}"; then
# add new cover image
"$tageditor" set -n cover="$cover_img" --files "${args[i]}"
fi
if [[ $? -ne 0 ]] ; then
error_files+=("${args[i]}")
else
if [[ -f "${args[i]}".bak ]] ; then
rm -v "${args[i]}".bak
fi
if [[ -f "${args[i]}".1.bak ]] ; then
rm -v "${args[i]}".1.bak
fi
fi
fi
done
if [[ "${#error_files[@]}" -gt 0 ]] ; then
echo "$0: There were errors: ${error_files[*]}" 1>&2
fi
exit "${#error_files[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment