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
import sys
from musify.logger import STAT
logging.basicConfig(format="%(message)s", level=STAT, stream=sys.stdout)
Load a track
Load a track as follows:
import asyncio
from musify.libraries.local.track import FLAC, MP3, M4A, WMA
track = FLAC("<PATH TO A FLAC TRACK>")
asyncio.run(track.load())
track = MP3("<PATH TO AN MP3 TRACK>")
asyncio.run(track.load())
track = M4A("<PATH TO AN M4A TRACK>")
asyncio.run(track.load())
track = WMA("<PATH TO A WMA TRACK>")
asyncio.run(track.load())
# 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 = asyncio.run(load_track("<PATH TO A 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.wrangle import SpotifyDataWrangler
track = asyncio.run(load_track("<PATH TO A TRACK>", remote_wrangler=SpotifyDataWrangler()))
Modify the track’s tags and save them
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)
Save all the modified tags to the file:
results = asyncio.run(track.save(replace=True, dry_run=False)) # print a list of the tags that were saved print([tag.name for tag in results.updated])
… or select exactly which modified tags you wish to save:
# ...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 = asyncio.run(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])