quodlibet / mutagen

Python module for handling audio metadata

Home Page:https://mutagen.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to add artwork to `.ogg` [vorbis] file

opened this issue · comments

import requests
from mutagen.oggvorbis import OggVorbis
from mutagen.id3 import APIC, PictureType

class AddMetaData:
    """
    Adds metadata to audio file
    :param file: file path
    :param art_url: album art url
    """
    def __init__(self, file, art_url):
        art_data = requests.get(art_url).content
        audio = OggVorbis(file)
        apic = APIC(
            encoding=3,
            mime="image/jpeg",
            type=PictureType.COVER_FRONT,
            desc="Cover",
            data=art_data
        )
        audio["metadata_block_picture"] = str(apic)
        audio.save()

file = "./music.ogg"
art_url = "https://music_art.com/art.jpg"

AddMetaData(file, art_url)

There are no errors but the artwork is not being added

First you should use a flac.Picture and not the APIC ID3 frame.

Then the data needs to be encoder as a base64 ASCII string. See this code for an example:

https://github.com/metabrainz/picard/blob/master/picard/formats/vorbis.py#L319

See also https://wiki.xiph.org/VorbisComment#METADATA_BLOCK_PICTURE

Modify my code. I am totally confused I have tried more than 10 ways and my brain got messed so pls..

I updated the code still it's not adding artwork. [doesn't print errors]

class AddMetaData:
    """
    Adds metadata to audio file
    :param file: file path
    :param art_url: album art url
    """
    def __init__(self, file, art_url):
        art_data = requests.get(art_url).content
        audio = OggVorbis(file)
        pic = Picture()
        pic.type = 3
        pic.mime = "image/jpeg"
        pic.data = art_data
        pic_data = pic.write()
        pic_data = base64.b64encode(pic_data).decode("ascii")
        audio["metadata_block_picture"] = [pic_data]
        audio.save(file)

Yep, your code is correct. That will ad the image. How do you determine that it did not work?

For a full working example this here adds the tag properly:

import base64
import requests
from mutagen.oggvorbis import OggVorbis
from mutagen.id3 import PictureType
from mutagen.flac import Picture


class AddMetaData:
    """
    Adds metadata to audio file
    :param file: file path
    :param art_url: album art url
    """

    def __init__(self, file, art_url):
        art_data = requests.get(art_url).content
        audio = OggVorbis(file)
        pic = Picture()
        pic.type = PictureType.COVER_FRONT
        pic.mime = "image/jpeg"
        pic.data = art_data
        pic_data = pic.write()
        pic_data = base64.b64encode(pic_data).decode("ascii")
        audio["metadata_block_picture"] = [pic_data]
        audio.save(file)


file = "./music.ogg"
art_url = "https://fanart.tv/media/overview/5.jpg"

AddMetaData(file, art_url)

Check the result with MP3Tag or Picard to check the tag is present.

Yep, your code is correct. That will ad the image. How do you determine that it did not work?

For a full working example this here adds the tag properly:

import base64
import requests
from mutagen.oggvorbis import OggVorbis
from mutagen.id3 import PictureType
from mutagen.flac import Picture


class AddMetaData:
    """
    Adds metadata to audio file
    :param file: file path
    :param art_url: album art url
    """

    def __init__(self, file, art_url):
        art_data = requests.get(art_url).content
        audio = OggVorbis(file)
        pic = Picture()
        pic.type = PictureType.COVER_FRONT
        pic.mime = "image/jpeg"
        pic.data = art_data
        pic_data = pic.write()
        pic_data = base64.b64encode(pic_data).decode("ascii")
        audio["metadata_block_picture"] = [pic_data]
        audio.save(file)


file = "./music.ogg"
art_url = "https://fanart.tv/media/overview/5.jpg"

AddMetaData(file, art_url)

Check the result with MP3Tag or Picard to check the tag is present.

Lemme change my player and see

PictureType.COVER_FRONT

yeah its working player issue. [resolved]

@phw Thank you your kind support. I have no more words to express my feeling towards you. You solved a problem due to which I was struggling from many days.

Glad I could help :)