dotfiles/.local/bin/ogg-cover-art

90 lines
2.3 KiB
Plaintext
Raw Normal View History

2020-12-27 00:52:16 +01:00
# -*- mode: sh -*-
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p unixtools.xxd
usage(){
fmt <<EOF
DESCRIPTION
Adapted from ogg-cover-art by acabal: https://github.com/acabal/scripts/blob/master/ogg-cover-art
USAGE
ogg-cover-art [COVER-FILENAME] META-FILENAME
convert normal image into ogg supported metadata
EOF
exit
}
die(){ printf "Error: ${1}\n" 1>&2; exit 1; }
require(){ command -v $1 > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" -o "$1" = "-h" ]; then usage; fi fi
#End boilerplate
#require "opuscomment.pl" "Try: apt-get install opus-tools"
if [ $# -eq 0 ]; then
usage
fi
imagePath=""
if [ $# -eq 1 ]; then
imagePath="$1"
fi
if [ ! -f "${imagePath}" ]; then
die "Couldn't find cover image."
fi
imageMimeType=$(file -b --mime-type "${imagePath}")
if [ "${imageMimeType}" != "image/jpeg" -a "${imageMimeType}" != "image/png" ]; then
die "Cover image isn't a jpg or png image."
fi
#Insert cover image from file
#metadata_block_picture format
#See: https://xiph.org/flac/format.html#metadata_block_picture
imageWithHeader="$(mktemp -t "tmp.XXXXXXXXXX")"
description=""
#Reset cache file
echo -n "" > "${imageWithHeader}"
#Picture type <32>
printf "0: %.8x" 3 | xxd -r -g0 >> "${imageWithHeader}"
#Mime type length <32>
printf "0: %.8x" $(echo -n "${imageMimeType}" | wc -c) | xxd -r -g0 >> "${imageWithHeader}"
#Mime type (n * 8)
echo -n "${imageMimeType}" >> "${imageWithHeader}"
#Description length <32>
printf "0: %.8x" $(echo -n "${description}" | wc -c) | xxd -r -g0 >> "${imageWithHeader}"
#Description (n * 8)
echo -n "${description}" >> "${imageWithHeader}"
#Picture with <32>
printf "0: %.8x" 0 | xxd -r -g0 >> "${imageWithHeader}"
#Picture height <32>
printf "0: %.8x" 0 | xxd -r -g0 >> "${imageWithHeader}"
#Picture color depth <32>
printf "0: %.8x" 0 | xxd -r -g0 >> "${imageWithHeader}"
#Picture color count <32>
printf "0: %.8x" 0 | xxd -r -g0 >> "${imageWithHeader}"
#Image file size <32>
printf "0: %.8x" $(wc -c "${imagePath}" | cut --delimiter=' ' --fields=1) | xxd -r -g0 >> "${imageWithHeader}"
#Image file
cat "${imagePath}" >> "${imageWithHeader}"
echo "metadata_block_picture=$(base64 --wrap=0 < "${imageWithHeader}")"
#Delete temp files
rm "${imageWithHeader}"