Kodi Music Scanning and Scraping Process (v18)
4,850 Total views across this article’s versions; includes the historical baseline where available.
Kodi is a popular open-source multimedia player that runs on a variety of operating systems including Windows, macOS, Android, iOS, and Linux (including Linux installations on Raspberry Pi hardware).
This article begins a series examining the Kodi multi-media player's handling of music files. Specifically, its process flow logic. There's plenty of information elsewhere describing how to use Kodi to manage your music library and serve content, such as Kodi's Wiki. What is missing elsewhere is a deep-dive into the process flow of Kodi's music library management system. Do you need or want to know the order in which Kodi makes various decisions about music files? If yes, then you may find this series to be helpful.
This article series uses diagrams of the relevant of Kodi's software architecture to explain Kodi's music process flow logic. The articles pertain to the Linux version of Kodi exclusively. During the research phase of writing these articles, I reverse-engineered the relevant C++ implementation files and their included headers. At various points, the code's behavior is explained in great detail and at other points the processes are described at a higher level. I have tried to keep the technical details light to focus on the process flow and order-of-operations. The focus is Kodi's behavior while scanning and scraping music files (tracks/songs).
Article Contents
This article introduces the article series. A high-level process overview diagram is presented at the bottom of this article. It contains the following sections:
- Kodi's Music Management is Confusing
- Scanning vs. Scraping
- MusicBrainz
- Why?
- The 10,000 Foot View (diagram)
- The 5,000 Foot View (diagram)
Kodi's Music Management Is Confusing
One of the most challenging learning curves with Kodi is understanding how it sorts and categorizes music files. From a user-experience viewpoint, what seems logical to one person may not be to another. This is underscored by the myriad of music genres, file types, and metadata fields. Any way that you look at it, the subject of categorizing music is much more complicated than any other digital media type. Therefore, the complexity and steep learning curve is not a design issue with Kodi; rather, it is the nature of the beast, so to speak. The alternative would be a draconian implementation forcing music content into predetermined silos. If you want that, you're probably not interested in Kodi to begin with (or open source solutions for that matter).
Over time, the Kodi project has evolved to become more flexible in organizing the media it manages as it has struggled to meet the demands and needs of its users. Unfortunately, in spite of these efforts, its processing of music files such as cataloging and sorting remains an enigma to most users. Kodi's music organization algorithms are significantly more complex compared to its handling of videos, photos, and other media types.
Scanning vs. Scraping
For the music library, scanning reads music-file tags and builds or updates database records. DoScan() checks folders and their stored hashes; changed folders, new folders, or an explicit rescan lead to ScanTags() and album construction. Cue-sheet handling also exists. This is separate from looking up additional album and artist information through a scraper.
In the checked 18.5 path, a successful scan of a source is followed by RetrieveLocalArt() for albums added by that scan. ScrapeInfoAddedAlbums() follows only when the SCAN_ONLINE flag is present. Album and artist information can also be requested separately: the scanning job dispatches those requests to FetchAlbumInfo() or FetchArtistInfo() instead of rescanning the music files. See the scanner source, especially Process(), DoScan() and ScanTags().
Scraped metadata is merged into Kodi’s music database; this path does not rewrite the embedded tags in your audio files. The 18.5 settings definition defaults musiclibrary.downloadinfo and musiclibrary.overridetags to false. The override setting controls particular merge rules, not an unrestricted replacement of every field. For example, CAlbum::MergeScrapedAlbum() protects release IDs read from file tags, can replace a nonempty title or year when override is enabled, and applies additional conditions before updating artist credits and song information. The selected scraper and any NFO data determine what information is available to merge.
MusicBrainz
MusicBrainz is a music-information database with identifiers for artists, releases, recordings and other entities. Picard is a separate tagging application that uses that database. An MBID is an identifier, not an audio fingerprint. Picard can identify recordings using AcoustID fingerprint matching and write metadata into supported file tags. The tag mappings are documented; these are not an opaque proprietary tagging format.
MusicBrainz IDs can affect Kodi before online scraping. FileItemsToAlbums() groups files carrying a MusicBrainz album ID by that ID. Files without it enter the album-name and artist fallback logic described in Phases 2 and 3. The database lookup during scraping is a later use of those identifiers. Picard is optional: Kodi can scan suitably tagged files made with other tag editors.
In my case, virtually all of my music files have been carefully, manually tagged. This creates the opportunity for a disconnect between how I think about music tagging and how Kodi thinks about music tagging.
Love It or Leave It
MusicBrainz is optional. I seem to be an odd-man out. I don't dislike MusicBrainz, but I'm less-than enamored with it. I applaud the concept, but the reality is there is no fool-proof solution for automatic music tagging. While it is a noble effort, it has a significantly high ratio of false positives for my taste (based on my experimentation with it). I am certain this is due to my tastes in music. Picard can use existing metadata or AcoustID matching to identify a recording, drawing on community-maintained MusicBrainz data. A fingerprint match can still lead to an unsuitable release choice for a particular collection. I've written about the weaknesses of the audio-identification systems such as AcoustID fingerprint matching in my blog, so I shall skip detouring to that discussion here. The bottom line is I know there are many MusicBrainz fan-boys out there, but I'm not one of them. I do appreciate the effort, but my particular music collection proved frustrating when I applied MusicBrainz to it. The process yielded hundreds of mis-identified tracks that I had to fix manually.
MusicBrainz too has a learning curve and there were no doubt mistakes made in my efforts. No matter. It underscores the impetus behind my entire efforts in the research I put forth that led to writing this series of articles - the fact that I struggled so much to automate what a human can do rather easily - indicates that before using these tools (such as Kodi's music file cataloging system and/or MusicBrainz), one should be certain it's the right tool for the job.
Why?
Everyone asks me this question. I began this project while attempting to understand how Kodi v17 (Krypton) cataloged music files. Its management and cataloging of music files didn't seem to work as well as it should have. Some files were duplicated, some were not read and added to the library. I couldn't figure out why it behaved the way it did under certain circumstances. It didn't handle some files as I expected. I also found Kodi's distribution of option switches to be confusing (where various switches were found and what they did).
As I was in the midst of fiddling with Kodi, Leia (v18) was released - after finally molding Krypton into submission - I decided to archive my work and start anew with v18 to determine if it was easier to work with. When I experienced similar challenges, I chose to dive in and figure out what was causing the disconnect between my mind's vision of how it should work and how Kodi actually did work.
Bear in mind the fact that Kodi is a community driven effort. It's code is comprised of work from thousands of contributors. While many portions of Kodi's code have been written by certain people, the vast majority of its code has been worked on by multiple software developers over time. This means different styles of programming may be present within the same section of code, orders of operation and various processes may be applied inconsistently, and other similar factors can and do occur in various parts of the code. Certain sections - such as the music metadata parsing portion - have undergone multiple stages of revamping and re-working over time, by multiple different "owners" of the code. Any way that you look at it, this type of shared programming environment tends to result in code that is more difficult to follow and maintain. Over time, as more and more people are involved in making updates to the same section, it gets worse and worse until and unless someone decides to make the effort to re-write large swaths of code. The greatest challenge in cleaning up code is most often coming to grips with how it works and all of its nuances that are the starting point of re-working it. In fact, most software developers spend more time reading code than writing it (usually because they didn't write the original code in the first place). This is also why commenting code is critically important. When it doesn't exist or is sub-par, many devs would rather spend their time completely re-writing it versus trying to understand what the other dev did.
The 10,000 Foot View
The following sequence describes the checked source. The ten numbered phases are this article series’ organization of the code, not ten independent Kodi operations that every request executes.
| Request or stage | Kodi 18.5 behavior |
|---|---|
| Library scan | Check source paths and folder hashes; read changed or explicitly rescanned music tags and cue data; construct albums and update the music database. |
| After a successful source scan | For albums added in that scan, retrieve local album/disc-set and album-artist artwork. |
| Optional additional information | With SCAN_ONLINE, use configured album/artist scrapers for eligible records; already-scraped records are ordinarily skipped in this automatic path. |
| Explicit album/artist refresh | Use the separate information-fetch path. A refresh can clear the prior scraped timestamp, allowing the record to be scraped again. |
| End of processing | Perform applicable cleanup, artist-sort updates and library notifications. |
The original high-level diagram remains available for comparison. Its refresh-to-stop shortcut does not represent the explicit refresh path, which can clear a previous scrape timestamp.
The 5,000 Foot View
Album lookup and merging involve more than a single “tags found” or “scraped before” decision. See DownloadAlbumInfo() in the scanner and the album merge implementation.
| Decision | What it means in the checked source |
|---|---|
| Embedded MusicBrainz album ID | Used during album grouping; it may also be resolved to a scraper URL when fetching album information. |
| album.nfo | A full NFO can supply details immediately. URL and combined NFO forms provide a lookup route; combined/override data has its own merge behavior. A user-driven refresh can offer to ignore the NFO. |
| No usable direct lookup | The configured scraper can search by album and artist names. Results depend on the scraper, matching and selection rules. |
| Merge into library | Field-specific CAlbum merge rules apply. The override setting affects selected tag-derived database values; it does not edit the audio files. |
The original lookup diagram is retained as historical material. Its “file scraped before” stop and simple overwrite box omit the separate refresh path and field-specific merge conditions. Neither historical diagram establishes a source revision from its line numbers alone.
The next article is Phase 1 (Scanning & Sorting by Album).