在 linux 中通过命令行在 OGG 中嵌入专辑封面

dmi*_*ova 18 linux ogg album-art

我想将我的音乐从 flac 转换为 ogg,目前 oggenc 除了专辑封面之外完美地做到了这一点。Metaflac 可以输出专辑封面,但是似乎没有命令行工具可以将专辑封面嵌入 ogg。MP3Tag和的EasyTag是能够做到这一点,有它的规范在这里它要求为Base64编码格式的图像。但是到目前为止,我未能成功获取图像文件,将其转换为 base64 并将其嵌入到 ogg 文件中。

如果我从已经嵌入图像的 ogg 文件中获取 base64 编码图像,我可以使用 vorbiscomment 轻松将其嵌入到另一个图像中:

vorbiscomment -l withimage.ogg > textfile
vorbiscomment -c textfile noimage.ogg
Run Code Online (Sandbox Code Playgroud)

我的问题是将 jpeg 之类的内容转换为 base64。目前我有:

base64 --wrap=0 ./image.jpg
Run Code Online (Sandbox Code Playgroud)

这给了我转换为 base64 的图像文件,使用 vorbiscomment 并遵循标记规则,我可以将其嵌入到 ogg 文件中,如下所示:

echo "METADATA_BLOCK_PICTURE=$(base64 --wrap=0 ./image.jpg)" > ./folder.txt
vorbiscomment -c textfile noimage.ogg
Run Code Online (Sandbox Code Playgroud)

但是,这给了我一个图像无法正常工作的 ogg。我注意到在比较 base64 字符串时,所有正确嵌入的图片都有一个标题行,但我生成的所有 base64 字符串都缺少这个标题。进一步分析header:

od -c header.txt
0000000  \0  \0  \0 003  \0  \0  \0  \n   i   m   a   g   e   /   j   p
0000020   e   g  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000040  \0  \0  \0  \0  \0  \0  \0  \0 035 332
0000052
Run Code Online (Sandbox Code Playgroud)

遵循上面给出的规范。注意003对应封面,image/jpeg是mime类型。

所以最后,我的问题是,我怎样才能对文件进行 base64 编码并生成这个头文件以嵌入到 ogg 文件中?

小智 9

这是我对 /usr/bin/vorbiscomment: Argument list too long 问题的解决方案。我创建了一个脚本并将其命名为 oggart。只需像这样从命令行运行它:

oggart /path/to/music_file.ogg /path/to/image_file
Run Code Online (Sandbox Code Playgroud)

这会使用 METADATA_BLOCK_PICTURE 字段标记您的 ogg 文件。Easytag 使用 COVERART 字段而不是 METADATA_BLOCK_PICTURE 的旧方法来执行此操作。如果您想要 Easytag 兼容性,那么您可以像这样运行脚本:

oggart /path/to/music_file.ogg /path/to/image_file -e
Run Code Online (Sandbox Code Playgroud)

这是脚本:

#!/bin/sh

FILE1="`basename \"$1\"`"
EXT1=${FILE1##*.}
EXTTYPE1=`echo $EXT1 | tr '[:upper:]' '[:lower:]'`

FILE2="`basename \"$2\"`"
EXT2=${FILE2##*.}
EXTTYPE2=`echo $EXT2 | tr '[:upper:]' '[:lower:]'`

OGG=""
if [ "$EXTTYPE1" = ogg ]; then
OGG="$1"
elif [ "$EXTTYPE2" = ogg ]; then
OGG="$2"
fi
if [ "$OGG" = "" ]; then
echo no ogg file selected
exit 0
fi

PIC=""
array=(jpeg jpg png)
for item in ${array[*]}
do
if [ "$item" = "$EXTTYPE1" ]; then
PIC="$1"
elif [ "$item" = "$EXTTYPE2" ]; then
PIC="$2"
fi
done
if [ "$PIC" = "" ]; then
echo no jpg or png file selected
exit 0
fi

if [ "$3" = -e ]; then
EASYTAG=Y
else
EASYTAG=N
fi

DESC=`basename "$PIC"`
APIC=`base64 --wrap=0 "$PIC"`
if [ "`which exiv2`" != "" ]; then
MIME=`exiv2 "$PIC" | grep 'MIME type ' | sed 's/: /|/' | cut -f 2 -d '|' | tail -n 1`
fi
if [ "$MIME" = "" ]; then
MIME="image/jpeg"
fi

vorbiscomment -l "$OGG" | grep -v '^COVERART=' | grep -v '^COVERARTDESCRIPTION=' | grep -v '^COVERARTMIME=' | grep -v 'METADATA_BLOCK_PICTURE=' > "$OGG".tags

if [ "$EASYTAG" = N ]; then
echo METADATA_BLOCK_PICTURE="$APIC" > "$OGG".tags2
else
echo COVERART="$APIC" > "$OGG".tags2
fi
vorbiscomment -w -R -c "$OGG".tags2 "$OGG"
vorbiscomment -a -R -t COVERARTDESCRIPTION="$DESC" "$OGG"
vorbiscomment -a -R -t COVERARTMIME="$MIME" "$OGG"
vorbiscomment -a -R -c "$OGG".tags "$OGG"

rm -f "$OGG".tags
rm -f "$OGG".tags2
Run Code Online (Sandbox Code Playgroud)


Bia*_*apy 8

我刚刚编写了一个脚本,使用 vorbiscomment 从 OGG/Vorbis 文件导出/导入图像。它是音乐库转换工具的一部分。

相关脚本位于此工具的“mussync-tools-transfert_images”函数中:

https://github.com/biapy/howto.biapy.com/blob/master/various/mussync-tools

基本上,我已经为 metadata_block_picture 格式编写了一个 reader 和 writer。

代码相当复杂:

      OUTPUT_FILE="/path/to/my-ogg-file.ogg"
      IMAGE_PATH="/path/to/my-cover-art.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=""

      # 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}"
Run Code Online (Sandbox Code Playgroud)