quodlibet / mutagen

Python module for handling audio metadata
https://mutagen.readthedocs.io
GNU General Public License v2.0
1.57k stars 158 forks source link

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

Closed ghost closed 1 year ago

ghost commented 1 year ago
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

phw commented 1 year ago

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

phw commented 1 year ago

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

ghost commented 1 year ago

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..

ghost commented 1 year ago

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)
phw commented 1 year ago

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.

ghost commented 1 year ago

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

ghost commented 1 year ago
PictureType.COVER_FRONT

yeah its working player issue. [resolved]

ghost commented 1 year ago

@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.

phw commented 1 year ago

Glad I could help :)