From 999a173001e8b687d17a135f0f9d6b795ffb88f3 Mon Sep 17 00:00:00 2001 From: Colin Date: Thu, 13 Jun 2024 08:22:57 +0000 Subject: [PATCH] sane-tag-music: `--trackno "" fix-tags FOO` can be used to clear FOOs track number field --- .../additional/sane-scripts/src/sane-tag-music | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pkgs/additional/sane-scripts/src/sane-tag-music b/pkgs/additional/sane-scripts/src/sane-tag-music index 4edc7d3c..75a35ae3 100755 --- a/pkgs/additional/sane-scripts/src/sane-tag-music +++ b/pkgs/additional/sane-scripts/src/sane-tag-music @@ -22,8 +22,8 @@ then this updates the tags on-disk to reflect their path. DIRECTORY: specify `.` to scan the entire library. options: ---dry-run: only show what would be done, don't actually do it. ---verbose + --dry-run: only show what would be done, don't actually do it. + --verbose --album ALBUM manually specify the tag, rather than guessing from path. --album-artist ARTIST often combined with DIRECTORY to tag an entire artist or album. --artist ARTIST @@ -31,7 +31,7 @@ options: --trackno TRACK_NUMBER fix-tags options: ---force: apply path-based tag to each file, even those which already have tags (only for fix-tags) + --force: apply path-based tag to each file, even those which already have tags (only for fix-tags) """ from dataclasses import dataclass @@ -118,6 +118,10 @@ class Tags: return f"artist:{self.artist}/{self.albumartist}, album:{self.album}, title:{self.title}, trackno:{self.tracknumber}" def union(self, fallback: 'Tags') -> 'Tags': + """ + substitute any tags missing tags in `self` with those from `fallback`. + i.e. `self` takes precedence over `fallback`. + """ def merge_field(primary: list[str], secondary: list[str]) -> list[str]: # primary_lower = [i.lower() for i in primary] # return primary + [i for i in secondary if i.lower() not in primary_lower] @@ -159,6 +163,9 @@ class Tags: if v.count("/") == 1: trackno, last = v.split("/") try: + if int(trackno) == int(last) == 1: + # track 1/1: useless; clear the field + self.tracknumber[i] = '' if int(trackno) <= int(last): self.tracknumber[i] = trackno.lstrip('0') except: pass @@ -401,9 +408,14 @@ class Tagger: path_tags = Tags.from_path(path_) additional_tags = self.manual_tags.union(path_tags) if self.force: + # additional_tags overrule old_tags new_tags = additional_tags.union(old_tags) else: + # old_tags overrule additional_tags new_tags = old_tags.union(additional_tags) + if additional_tags.tracknumber == [""]: + # special case that `--trackno ""` can be used to delete the track number without `--force`ing all fields + new_tags.tracknumber = [] new_tags = new_tags.union(self.manual_tags) new_tags.trim_fields() new_tags.cleanup_trackno()