Saving tags with mutagen

284 views
Skip to first unread message

Bernd Wechner

unread,
Jun 14, 2017, 8:02:25 PM6/14/17
to Quod Libet Development
I can't seem to find a good example anywhere in the docs, or on-line alas for this simple proforma:

import mutagen

AlbumArtist = "Somebody"   # An Album artist to write to the appropriate tag
 
mfile = mutagen.File(path)
filetype = type(mfile).__name__
                       
if filetype == "ASF":
# Save the Album Artist here ("WM/AlbumArtist")
# ... do something, what?
mfile.save()          
elif filetype == "MP3":
# Save the Album Artist here ("TPE2")
# ... do something, what?
mfile.save()          
elif filetype == "MP4":
# Save the Album Artist here ("aART")
# ... do something, what?
mfile.save()

I've been experimenting primarily with the TPE2 tage so far but have tried so many things and failed, it's not funny. So I'me wondering what the missing line of code  (or three missing lines of code) aught to look like?

I'd be greatly appreciative of any tips here, and don't mind adding what I learn to the (already kick-ass excellent) docs at readthedocs ;-).

Regards,

Bernd.

Christoph Reiter

unread,
Jun 17, 2017, 7:06:04 AM6/17/17
to Quod Libet Development
Every format/interface is different. Here are some programs which
handle multiple formats using mutagen:

https://github.com/quodlibet/quodlibet/tree/master/quodlibet/quodlibet/formats
https://github.com/metabrainz/picard/tree/master/picard/formats
https://github.com/beetbox/beets/blob/master/beets/mediafile.py

The interface you are probably looking for does not exist, see
https://github.com/quodlibet/mutagen/issues/186

Bernd Wechner

unread,
Jun 29, 2017, 11:59:22 PM6/29/17
to Quod Libet Development
OK, This works (only MP3 of the three formats I'm using seems to have a different call to set the tag). I wonder why?
import mutagen
path = "Some file or other"    # A file to set set the album artist
AlbumArtist = "Somebody"   # An Album artist to write to the appropriate tag
mfile = mutagen.File(path)
filetype = type(mfile).__name__
                       
if filetype == "ASF": tag = "WM/AlbumArtist"         mfile.tags[tag] = AlbumArtist         mfile.save()          
elif filetype == "MP3":
        tag = "TPE2"
        mfile.tags.setall(tag, [mutagen.id3.TPE2(3, [AlbumArtist])])
        mfile.save()          
elif filetype == "MP4":
        tag = "aART"         mfile.tags[tag] = AlbumArtist mfile.save()

Which brings me to another question:

How to find and remove duplicate tags.

It seems in MP3 files especially I'm seeing duplicate tags in some files. Here's what I see (and am not seeing clear documentation for):
import mutagen
          path = "Some file or other"    # A file to set set the album artist
          tag = "Some tag or other"      # For example "TPE1"

          mfile = mutagen.File(path)
filetype = type(mfile).__name__

if filetype == "MP3" and tag in mfile.tags:
    mtag = mfile.tags[tag]
    has_dupes = hasattr(mtag, "text") and isinstance(mtag.text, list) and len(mtag.text) > 1
    if has_dupes:
        print("Tag values:")
        for val in tags[tag].text:
            print(val)

The question is how to remove the duplicates thus found and moreover can duplicates be found in ASF and MP4 files too?  hasattr(mtag, "text") is false for those with filetype of ASF or MP4.

I'm not finding clear documentation of how duplicate tags are represented in each file type if at all.

Regards,

Bernd.
--

---
You received this message because you are subscribed to the Google Groups "Quod Libet Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quod-libet-develo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages