Create, load, and update a local playlist

In this example, you will:
  • Create a local playlist from scratch

  • Load a local playlist

  • Modify the tracks in a playlist and save the changes 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)

Create a playlist

You can create a playlist as follows:

from musify.libraries.local.playlist import M3U, XAutoPF
from musify.libraries.local.track import load_track

tracks = [
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
]

playlist = M3U("<PATH TO AN M3U PLAYLIST>", tracks=tracks)

Load a playlist

You can load a playlist as follows:

Note

To be able to use the XAutoPF playlist type, you will need to have installed the musicbee optional dependencies. See Installation for more details.

playlist = M3U("<PATH TO AN M3U PLAYLIST>")
playlist = XAutoPF("<PATH TO AN XAUTOPF PLAYLIST>")

# pretty print information about this playlist
print(playlist)

You can also just have Musify automatically determine the playlist type to load based on the file’s extension:

from musify.libraries.local.playlist import load_playlist

playlist = load_playlist("<PATH TO A PLAYLIST>")

If you already have some tracks loaded, and you want the playlist to only use those tracks instead of loading the tracks itself, you can pass these preloaded tracks to the playlist too.

from musify.libraries.local.track import load_track

tracks = [
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
    load_track("<PATH TO A TRACK>"),
]

playlist = M3U("<PATH TO AN M3U PLAYLIST>", tracks=tracks)

There may also be cases where the files in the file need mapping to be loaded e.g. if the paths contained in the playlist file are relative paths. You may give the playlist object a PathMapper or PathStemMapper to handle this.

from musify.file.path_mapper import PathMapper

playlist = M3U("<PATH TO AN M3U PLAYLIST>", path_mapper=PathMapper())

If you want to be able to read/update URIs on the loaded tracks, you’ll need to provide a RemoteDataWrangler to the playlist 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

playlist = M3U("<PATH TO AN M3U PLAYLIST>", remote_wrangler=SpotifyDataWrangler())

Modify the playlist

  1. Add some tracks to the playlist:

    # add a track to the playlist
    playlist.append(load_track("<PATH TO A TRACK>"))
    
    # add album's and artist's tracks to the playlist using either of the following
    playlist.extend(tracks)
    playlist += tracks
    
  2. Save the playlist:

    result = playlist.save(dry_run=False)
    print(result)