Load and save tags to a local trackļƒ

In this example, you will:
  • Load a local track

  • Modify the tags of a local track and save them to the file

Set up loggingļƒ

Set up logging to ensure you can see all info reported by the later operations. Libraries log info about loaded objects to the custom STAT level.

import logging
from musify.log import STAT

logging.basicConfig(format="%(message)s", level=STAT)

Load a trackļƒ

You can load a track as follows:

from musify.libraries.local.track import FLAC, MP3, M4A, WMA

track = FLAC("<PATH TO A FLAC TRACK>")
track = MP3("<PATH TO AN MP3 TRACK>")
track = M4A("<PATH TO AN M4A TRACK>")
track = WMA("<PATH TO A WMA TRACK>")

# pretty print information about this track
print(track)

You can also just have Musify automatically determine the track type to load based on the fileā€™s extension:

from musify.libraries.local.track import load_track

track = load_track("<PATH TO AN MP3 TRACK>")

If you want to be able to assign a URI to your track, youā€™ll need to provide a RemoteDataWrangler to the track object for the relevant music streaming source.

The following is an example for doing this with Spotify as the data source:

from musify.libraries.remote.spotify.processors import SpotifyDataWrangler

track = MP3("<PATH TO AN MP3 TRACK>", remote_wrangler=SpotifyDataWrangler())

Modify the trackā€™s tagsļƒ

Note

To be able to modify a trackā€™s images, you will need to have installed the images optional dependencies. See Installation for more details.

  1. Change some tags:

    from datetime import date
    
    track.title = "new title"
    track.artist = "new artist"
    track.album = "new album"
    track.track_number = 200
    track.genres = ["super cool genre", "awesome genre"]
    track.key = "C#"
    track.bpm = 120.5
    track.date = date(year=2024, month=1, day=1)
    track.compilation = True
    track.image_links.update({
        "cover front": "https://i.scdn.co/image/ab67616d0000b2737f0918f1560fc4b40b967dd4",
        "cover back": "<PATH TO AN IMAGE ON YOUR LOCAL DRIVE>"
    })
    
    # see the updated information
    print(track)
    
  2. Save the tags to the file:

    # save all the tags like so...
    results = track.save(replace=True, dry_run=False)
    
    # ...or select which tags you wish to save like so
    from musify.libraries.local.track.field import LocalTrackField
    
    tags = [
        LocalTrackField.TITLE,
        LocalTrackField.GENRES,
        LocalTrackField.KEY,
        LocalTrackField.BPM,
        LocalTrackField.DATE,
        LocalTrackField.COMPILATION,
        LocalTrackField.IMAGES
    ]
    
    results = track.save(tags=tags, replace=True, dry_run=False)
    
    # print a list of the tags that were saved
    print([tag.name for tag in results.updated])