Sanitize filenames: replace spaces/punctuation with underscores
Replace all non-word characters (spaces, punctuation, brackets, etc.) with underscores in track titles, album names and artist names. Collapse consecutive underscores to one, strip leading/trailing ones. Umlauts (ä, ö, ü, ß) and digits are preserved. Also use underscore instead of space between track number and title. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
f4e49a3df6
commit
8a39f7c41e
1 changed files with 9 additions and 3 deletions
|
|
@ -38,8 +38,14 @@ class DiscCheck:
|
||||||
|
|
||||||
|
|
||||||
def _sanitize_filename(name: str) -> str:
|
def _sanitize_filename(name: str) -> str:
|
||||||
"""Entfernt problematische Zeichen aus Dateinamen."""
|
"""Ersetzt Sonderzeichen und Leerzeichen durch Unterstriche.
|
||||||
return re.sub(r'[<>:"/\\|?*]', "_", name).strip()
|
|
||||||
|
Buchstaben (inkl. Umlaute), Ziffern und bestehende Unterstriche bleiben erhalten.
|
||||||
|
Mehrere aufeinanderfolgende Unterstriche werden zu einem zusammengezogen.
|
||||||
|
"""
|
||||||
|
name = re.sub(r'[^\w]', '_', name) # alles außer Buchstaben/Ziffern/_ → _
|
||||||
|
name = re.sub(r'_+', '_', name) # mehrere _ → einer
|
||||||
|
return name.strip('_')
|
||||||
|
|
||||||
|
|
||||||
def discover_audio_files(directory: Path) -> list[Path]:
|
def discover_audio_files(directory: Path) -> list[Path]:
|
||||||
|
|
@ -118,7 +124,7 @@ def build_mapping(
|
||||||
|
|
||||||
for audio_file, track in zip(audio_files, disc.tracks):
|
for audio_file, track in zip(audio_files, disc.tracks):
|
||||||
safe_title = _sanitize_filename(track.title)
|
safe_title = _sanitize_filename(track.title)
|
||||||
new_name = f"{track.track_number:02d} {safe_title}{audio_file.suffix}"
|
new_name = f"{track.track_number:02d}_{safe_title}{audio_file.suffix}"
|
||||||
mapping[audio_file] = target_dir / new_name
|
mapping[audio_file] = target_dir / new_name
|
||||||
|
|
||||||
return mapping
|
return mapping
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue