Skip to main content

A module for cleaning up artist, album, and song names.

Project description

music-metadata-filter Test PYPI

A module for cleaning up artist, album and song names.

Installation

The music-metadata-filter library can be found on PyPI, and is installable with pip:

pip install music-metadata-filter

Usage

Basic example

from music_metadata_filter.functions import remove_remastered

print(remove_remastered("Track Title 1 - Remastered 2015"))
# outputs "Track Title 1"
print(remove_remastered("Track Title 2 (2011 Remaster)"))
# outputs "Track Title 2"

Single filter functions

You can call filter functions for basic, one-line filter functionality. These filter functions are intended to be used on a single field, such as an artist, album or track.

However, it is possible (not officially supported) to use some of these on combined fields ("Artist - Song", "Artist - Album"), as in the third example below.

import music_metadata_filter.functions as functions

print(functions.remove_remastered("Jane Doe (Remastered)"))
# outputs "Jane Doe"
print(functions.remove_version("Get Lucky (Album Version)"))
# outputs "Get Lucky"
print(functions.youtube("Car Bomb - Scattered Sprites (Official Music Video)"))
# outputs "Car Bomb - Scattered Sprites"

See functions.py for more details.

Combine filter functions

You can also use multiple filter functions on a string at once by creating a MetadataFilter object which combines multiple functions from above, or by using one of the predefined filter objects.

First, create a filter set. This is a set of filter functions for different fields.

import music_metadata_filter.functions as functions
from music_metadata_filter.filter import FilterSet

filter_set: FilterSet = {
    "track": (
        functions.remove_remastered,
        functions.fix_track_suffix,
        functions.remove_live,
        functions.remove_version,
    ),
    "album": (
        functions.remove_remastered,
        functions.fix_track_suffix,
        functions.remove_live,
    ),
}

Then, construct a MetadataFilter using this filter set.

from music_metadata_filter.filter import MetadataFilter

metadata_filter = MetadataFilter(filter_set)

print(metadata_filter.filter_field("album", "Nevermind (Remastered)"))
# outputs "Nevermind"
print(metadata_filter.filter_field("track", "In Bloom - Nevermind Version"))
# outputs "In Bloom (Nevermind Version)"
print(metadata_filter.filter_field("track", "Won't Get Fooled Again - Album Version"))
# outputs "Won't Get Fooled Again"

Predefined filters

There are also predefined filters available for easy access. For example, the above filter set is actually the predefined Spotify filter:

from music_metadata_filter.filters import make_spotify_filter

metadata_filter = make_spotify_filter()

See filters.py for more details.

Extending filters

Finally, you can take existing MetadataFilter objects and extend them with another filter. This is done by providing the .extend() method with another MetadataFilter object.

from music_metadata_filter.filters import make_spotify_filter, make_amazon_filter

metadata_filter = make_spotify_filter()
metadata_filter.extend(make_amazon_filter())

print(metadata_filter.filter_field("track", "Seasons in the Abyss (Album Version)"))
# outputs "Seasons in the Abyss"

As an alternative, you can use the .append() method to apply a filter set to an existing MetadataFilter object.

metadata_filter = make_spotify_filter()
metadata_filter.append({"album": lambda x: f"{x} Album"})

Since these methods return a MetadataFilter instance, you can chain method calls.

metadata_filter = make_spotify_filter().append({
    "artist": lambda x: f"{x} The Artist",
})

Opinionated filters

As discussed below, this library aims to be a direct port from another language. However, as is to be expected, I don't agree 100% with every choice made by the upstream maintainers. In order to make updates easier and ensure compatibility with upstream, my opinions are therefore separated out into a dedicated set of modules labelled with "opinionated".

Right now, there isn't much that differs. The primary differences are that "Live" should not be stripped from Spotify metadata, and that "Live" suffixes should be normalised by the "fix suffix" ruleset (like "Instrumental" and "Remix").

from music_metadata_filter.opinionated_filters import make_spotify_filter

metadata_filter = make_spotify_filter()

print(metadata_filter.filter_field("track", "Track Title - Live / Remastered"))
# outputs "Track Title (Live)"

Development

This project uses invoke as a task runner.

# Initialise a virtualenv
> python3 -m venv venv --prompt music-metadata-filter
> source bin/activate

# Install dev dependencies
> pip install -r requirements-dev.txt

# Run tests
> inv test

# Run black and isort formatters
> inv reformat

# Run flake8 linter
> inv lint

# Run mypy type checker
> inv type-check

# Regenerate regular expressions and test fixtures from upstream
> ./regen.sh

License

Licensed under the MIT License.

Acknowledgements

This library is a (mostly) direct port of the original JS library metadata-filter. Some of the code in this library, including regular expressions and test fixtures, are taken directly from the upstream repository.

I can't thank the web-scrobbler team enough for creating such a fantastic piece of software, and for collaborating with me in the creation of this port to Python.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

music-metadata-filter-3.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

music_metadata_filter-3.0.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file music-metadata-filter-3.0.0.tar.gz.

File metadata

  • Download URL: music-metadata-filter-3.0.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for music-metadata-filter-3.0.0.tar.gz
Algorithm Hash digest
SHA256 0b46f7bf69aecac464ea1262fd2af133c5c0e4e71d8124ba8096bb90441543f3
MD5 1ee03ad088d7f6c9fbefcbcfed09c680
BLAKE2b-256 042c98cfb93e0ea56ebe2a17519e1bdbbb9726f2b9f0ad5b5910e8f63f23d55c

See more details on using hashes here.

File details

Details for the file music_metadata_filter-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: music_metadata_filter-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for music_metadata_filter-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 410e633b9881ef05181233badf39b1beca2460d9d3e496c2b7c07ae40444370b
MD5 7a8de9b082f0c9864d09059be04dfdaf
BLAKE2b-256 4e1ad029653405e17d5b898f7b3e11516413303b4638a9d4e2df5115ef1f919e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page