Skip to content

Instantly share code, notes, and snippets.

@padakuro
Created November 22, 2013 22:23
Show Gist options
  • Select an option

  • Save padakuro/7607888 to your computer and use it in GitHub Desktop.

Select an option

Save padakuro/7607888 to your computer and use it in GitHub Desktop.
python3: write ogg vorbis (vorbiscomment) cover art (METADATA_BLOCK_PICTURE)
from PIL import Image
cover = Image.open(self.get_art_path(track))
cover_mime = Image.MIME[cover.format]
cover_width = cover.size[0]
cover_height = cover.size[1]
cover_bitdepth = cover.bits
with open(self.get_art_path(track), 'rb') as image_file:
cover_data = image_file.read()
METADATA_BLOCK_PICTURE = struct.pack(
#https://github.com/imalone/oggflacblock/blob/master/oggflacblock.pl#L278
#(L[2]A[%d]LA[%d]L[5]a[%d])>
'>2L%dsL%ds5L%ds' % (len(cover_mime), 0, len(cover_data)),
3, # The picture type according to the ID3v2 APIC frame: = Cover (front)
len(cover_mime), # The length of the MIME type string in bytes.
cover_mime.encode(encoding='ascii'), # The MIME type string, in printable ASCII characters 0x20-0x7e.
0, # The length of the description string in bytes.
b'', # The description of the picture, in UTF-8.
cover_width, #The width of the picture in pixels.
cover_height, # The height of the picture in pixels.
cover_bitdepth, # The color depth of the picture in bits-per-pixel.
0, # For indexed-color pictures (e.g. GIF), the number of colors used, or 0 for non-indexed pictures.
len(cover_data), # The length of the picture data in bytes.
cover_data #The binary picture data.
)
tags.append('METADATA_BLOCK_PICTURE=%s' % base64.b64encode(METADATA_BLOCK_PICTURE).decode(encoding='ascii'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment