Skip to content

Instantly share code, notes, and snippets.

@lemon24
Last active September 28, 2024 00:44
Show Gist options
  • Save lemon24/ebd0b8fa9b223be1948cddc279ea7970 to your computer and use it in GitHub Desktop.
Save lemon24/ebd0b8fa9b223be1948cddc279ea7970 to your computer and use it in GitHub Desktop.
using mutagen to update MP4 tags
import shutil
import mutagen
shutil.copy('original.mp4', 'new.mp4')
# mutagen.File knows how to open any file (works with both MP4 and MP4):
#
# https://mutagen.readthedocs.io/en/latest/user/gettingstarted.html
# https://mutagen.readthedocs.io/en/latest/api/base.html#mutagen.File
with open('new.mp4', 'r+b') as file:
media_file = mutagen.File(file, easy=True)
print('before:', media_file.pprint(), end='\n\n')
media_file['title'] = 'my title'
media_file['album'] = 'my album'
media_file['artist'] = 'my artist'
media_file.save(file)
print('after:', media_file.pprint(), end='\n\n')
print(type(media_file), type(media_file.tags), end='\n\n')
# Alternatively, you can use the explicit file type classes, e.g.
#
# with open('new.mp4', 'r+b') as file:
# media_file = mutagen.easymp4.EasyMP4(file)
#
# https://mutagen.readthedocs.io/en/latest/api/mp4.html#mutagen.easymp4.EasyMP4
# https://mutagen.readthedocs.io/en/latest/api/mp3.html#mutagen.mp3.EasyMP3
# To see the tags you can set, you need to look at their corresponding tags:
#
# https://mutagen.readthedocs.io/en/latest/api/id3.html#mutagen.easyid3.EasyID3
# https://mutagen.readthedocs.io/en/latest/api/mp4.html#mutagen.easymp4.EasyMP4Tags
from mutagen.easymp4 import EasyMP4Tags
from mutagen.easyid3 import EasyID3
print('mp3 tags:', *sorted(EasyID3.Set.keys()), end='\n\n')
print('mp4 tags:', *sorted(EasyMP4Tags.Set.keys()), end='\n\n')
before: MPEG-4 audio (AAC LC), 4.16 seconds, 96000 bps (audio/mp4)
after: MPEG-4 audio (AAC LC), 4.16 seconds, 96000 bps (audio/mp4)
album=my album
artist=my artist
title=my title
<class 'mutagen.easymp4.EasyMP4'> <class 'mutagen.easymp4.EasyMP4Tags'>
mp3 tags: acoustid_fingerprint acoustid_id album albumartist albumartistsort albumsort arranger artist artistsort asin author barcode bpm catalognumber compilation composer composersort conductor copyright date discnumber discsubtitle encodedby genre isrc language length lyricist media mood musicbrainz_albumartistid musicbrainz_albumid musicbrainz_albumstatus musicbrainz_albumtype musicbrainz_artistid musicbrainz_discid musicbrainz_releasegroupid musicbrainz_releasetrackid musicbrainz_trackid musicbrainz_trmid musicbrainz_workid musicip_fingerprint musicip_puid organization originaldate performer performer:* releasecountry replaygain_*_gain replaygain_*_peak title titlesort tracknumber version website
mp4 tags: album albumartist albumartistsort albumsort artist artistsort bpm comment composersort copyright date description discnumber genre grouping musicbrainz_albumartistid musicbrainz_albumid musicbrainz_albumstatus musicbrainz_albumtype musicbrainz_artistid musicbrainz_trackid musicip_puid releasecountry title titlesort tracknumber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment