#!/bin/zsh OUTPUT_FILE=${argv[1]} IMAGE_PATH="./cover.jpg" IMAGE_MIME_TYPE="image/jpeg" # Export existing comments to file. local COMMENTS_PATH="$(command mktemp -t "tmp.XXXXXXXXXX")" command vorbiscomment --list --raw "${OUTPUT_FILE}" > "${COMMENTS_PATH}" # Remove existing images. command sed -i -e '/^metadata_block_picture/d' "${COMMENTS_PATH}" # Insert cover image from file. # metadata_block_picture format. # See: https://xiph.org/flac/format.html#metadata_block_picture local IMAGE_WITH_HEADER="$(command mktemp -t "tmp.XXXXXXXXXX")" local DESCRIPTION="Front Cover - Low Resolution" # Reset cache file. echo -n "" > "${IMAGE_WITH_HEADER}" # Picture type <32>. command printf "0: %.8x" 3 | command xxd -r -g0 \ >> "${IMAGE_WITH_HEADER}" # Mime type length <32>. command printf "0: %.8x" $(echo -n "${IMAGE_MIME_TYPE}" | command wc -c) \ | command xxd -r -g0 \ >> "${IMAGE_WITH_HEADER}" # Mime type (n * 8) echo -n "${IMAGE_MIME_TYPE}" >> "${IMAGE_WITH_HEADER}" # Description length <32>. command printf "0: %.8x" $(echo -n "${DESCRIPTION}" | command wc -c) \ | command xxd -r -g0 \ >> "${IMAGE_WITH_HEADER}" # Description (n * 8) echo -n "${DESCRIPTION}" >> "${IMAGE_WITH_HEADER}" # Picture with <32>. command printf "0: %.8x" 0 | command xxd -r -g0 \ >> "${IMAGE_WITH_HEADER}" # Picture height <32>. command printf "0: %.8x" 0 | command xxd -r -g0 \ >> "${IMAGE_WITH_HEADER}" # Picture color depth <32>. command printf "0: %.8x" 0 | command xxd -r -g0 \ >> "${IMAGE_WITH_HEADER}" # Picture color count <32>. command printf "0: %.8x" 0 | command xxd -r -g0 \ >> "${IMAGE_WITH_HEADER}" # Image file size <32>. command printf "0: %.8x" $(command wc -c "${IMAGE_PATH}" \ | command cut --delimiter=' ' --fields=1) \ | command xxd -r -g0 \ >> "${IMAGE_WITH_HEADER}" # Image file. command cat "${IMAGE_PATH}" >> "${IMAGE_WITH_HEADER}" echo "metadata_block_picture=$(command base64 --wrap=0 < "${IMAGE_WITH_HEADER}")" >> "${COMMENTS_PATH}" # Update vorbis file comments. command vorbiscomment --write --raw --commentfile "${COMMENTS_PATH}" "${OUTPUT_FILE}" # Delete cache file. command rm "${IMAGE_WITH_HEADER}" # Delete comments file. command rm "${COMMENTS_PATH}"