From f622cc74218d6ed5bc7910eacfcc260314a400f3 Mon Sep 17 00:00:00 2001 From: Michael Klein Date: Sat, 16 Mar 2024 20:45:27 +0100 Subject: [PATCH] autotagger: match file name if the file has no title metadata --- beets/autotag/match.py | 4 +++- docs/changelog.rst | 1 + test/test_autotag.py | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/beets/autotag/match.py b/beets/autotag/match.py index 331a57596e..0460245e9d 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -19,6 +19,7 @@ from __future__ import annotations import datetime +import os import re from enum import IntEnum from typing import ( @@ -190,7 +191,8 @@ def track_distance( dist.add_ratio("track_length", diff, track_length_max) # Title. - dist.add_string("track_title", item.title, track_info.title) + cur_title = item.title or os.fsdecode(os.path.basename(item.path)) + dist.add_string("track_title", cur_title, track_info.title) # Artist. Only check if there is actually an artist in the track data. if ( diff --git a/docs/changelog.rst b/docs/changelog.rst index a9ed03e422..0393102853 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -23,6 +23,7 @@ New features: * Beets now uses ``platformdirs`` to determine the default music directory. This location varies between systems -- for example, users can configure it on Unix systems via ``user-dirs.dirs(5)``. +* The autotagger matches the file name if the track has no ``title`` metadata. Bug fixes: diff --git a/test/test_autotag.py b/test/test_autotag.py index 6e362f2f4b..cdff8f2cb7 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -354,6 +354,14 @@ def test_various_artists_tolerated(self): dist = match.track_distance(item, info, incl_artist=True) assert dist == 0.0 + def test_missing_metadata(self): + item = _make_item("", 0) + info = _make_trackinfo()[0] + dist1 = match.track_distance(item, info) + item.path = b"/tmp/01 One.mp3" + dist2 = match.track_distance(item, info) + assert dist2 < dist1 + class AlbumDistanceTest(BeetsTestCase): def _mapping(self, items, info):