Skip to content

Library, sync and metadata

Music Assistant shows one library, built from the libraries of all music providers a user has added. This page helps you understand how that library is built and kept up to date, and how its metadata is filled in.

The library lives in library.db and is owned by the music controller (mass.music). Each media type has its own sub-controller, such as mass.music.tracks or mass.music.albums.

A library item is one row in the database, with provider mappings: one ProviderMapping per provider item it stands for. The same song on a local disk and on two streaming services is one library track with three mappings.

Two things are easy to mix up:

  • item.provider == "library" only says the item has a row in the database. The album and artists of a library track get a row too.
  • Whether an item is in the user’s library is the in_library flag on its provider mappings. A liked item also counts as in the library.

To find the library item of a provider item, use get_library_item_by_prov_id on the controller of its media type. A mapping.item_id is the provider’s id, never a library id.

flowchart TD
  SCHED["Scheduled task per provider<br/>and media type"] --> RUN["Sync handler<br/>one provider sync at a time"]
  CMD["music/sync command"] --> RUN
  RUN --> SL["provider.sync_library"]
  SL --> GEN["get_library_tracks<br/>and the other media types"]
  GEN --> ADD["add_item_to_library<br/>match, then merge or insert"]
  ADD --> DB["library.db"]
  SL --> DEL["Deletions<br/>compared with the previous run"]
  DEL --> DB
  RUN --> DONE["MUSIC_SYNC_COMPLETED"]
  OPEN["Item opened or played"] --> META["schedule_update_metadata"]
  SCAN["Daily metadata scans"] --> META
  META --> DB

The top part is the sync, the bottom part is metadata. They run apart: a sync does not fill in metadata.

  • Schedule. When a music provider loads, the music controller registers a scheduled task with mass.tasks for each media type the user chose to sync (library_sync_tracks, library_sync_albums and so on). The provider sets the default schedule in get_default_library_sync_schedule. The music/sync command runs the same tasks by hand.
  • One at a time. A global lock lets one provider sync run at a time. During a sync, per-item events are suppressed; clients refresh on MUSIC_SYNC_COMPLETED and follow progress through task events.
  • Walk the provider library. sync_library iterates the provider’s library generators, such as get_library_tracks, and adds or updates each item.
  • Match and merge. add_item_to_library first looks for an existing library item: by library id, then by provider mapping, then by external ids such as ISRC or MusicBrainz ids, then by normalized name. A candidate found by external id or name is confirmed with compare_media_item (helpers/compare.py). With a match, the item is merged into the existing row: its mappings, external ids and metadata. Without one, a new row is inserted.
  • Removed items. The ids of the previous run are kept in the cache. An item that is gone from the provider is removed from the library when no other provider holds it and the provider is not a streaming service (local files, a media server). Otherwise its mapping is kept with in_library set to false, so its metadata stays. A run that could not sync every item skips deletions, and the user can turn deletions off in the advanced sync settings of a provider (library_sync_deletions).
  • Metadata. The metadata controller fills in images, descriptions and other metadata from music and metadata providers. It refreshes an item when it is opened or played and its metadata is older than the refresh interval, and daily scheduled scans pick up items that miss metadata. After a sync, MUSIC_SYNC_COMPLETED queues the task that links new items to MusicBrainz.

NOTE

Syncing walks the user’s own library through the provider’s throttler, which is fine. Walking a service’s catalogue for one user action is not: see the usage policy rules in the server’s AGENTS.md.

Music Assistant is a project of the Open Home Foundation.