Skip to content

Commit a8be8af

Browse files
authored
Cleaning filenames from forbidden characters (#204)
1 parent 7387387 commit a8be8af

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

audio_separator/separator/common_separator.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,30 @@ def clear_file_specific_paths(self):
373373
self.primary_stem_output_path = None
374374
self.secondary_stem_output_path = None
375375

376+
def sanitize_filename(self, filename):
377+
"""
378+
Cleans the filename by replacing invalid characters with underscores.
379+
"""
380+
sanitized = re.sub(r'[<>:"/\\|?*]', '_', filename)
381+
sanitized = re.sub(r'_+', '_', sanitized)
382+
sanitized = sanitized.strip('_. ')
383+
return sanitized
384+
376385
def get_stem_output_path(self, stem_name, custom_output_names):
377386
"""
378387
Gets the output path for a stem based on the stem name and custom output names.
379388
"""
380389
# Convert custom_output_names keys to lowercase for case-insensitive comparison
381390
if custom_output_names:
382391
custom_output_names_lower = {k.lower(): v for k, v in custom_output_names.items()}
383-
if stem_name.lower() in custom_output_names_lower:
384-
return os.path.join(f"{custom_output_names_lower[stem_name.lower()]}.{self.output_format.lower()}")
392+
stem_name_lower = stem_name.lower()
393+
if stem_name_lower in custom_output_names_lower:
394+
sanitized_custom_name = self.sanitize_filename(custom_output_names_lower[stem_name_lower])
395+
return os.path.join(f"{sanitized_custom_name}.{self.output_format.lower()}")
396+
397+
sanitized_audio_base = self.sanitize_filename(self.audio_file_base)
398+
sanitized_stem_name = self.sanitize_filename(stem_name)
399+
sanitized_model_name = self.sanitize_filename(self.model_name)
385400

386-
return os.path.join(f"{self.audio_file_base}_({stem_name})_{self.model_name}.{self.output_format.lower()}")
401+
filename = f"{sanitized_audio_base}_({sanitized_stem_name})_{sanitized_model_name}.{self.output_format.lower()}"
402+
return os.path.join(filename)

0 commit comments

Comments
 (0)