10 Commits
v0.1 ... v0.2

Author SHA1 Message Date
f4e4aa51bc Updated version number 2023-01-28 18:31:55 +01:00
ee8753d561 Error prevention: invalid separator 2023-01-28 18:30:06 +01:00
f968b002a5 Merge pull request #2 from notssh/main
Avoid forbidden path symbols
2023-01-28 17:21:37 +00:00
e87cc49b28 Update musort.py 2023-01-28 16:44:53 +00:00
3c79631a84 Update - avoid forbidden path symbols 2023-01-28 14:48:39 +00:00
b13711f3d6 Removed unnessesary code 2023-01-27 21:04:28 +01:00
0aeb8a15ce Merge pull request #1 from dudozermaks/readability
Removed repeating code and improved readability
2023-01-27 21:02:58 +01:00
239a71bec8 removed repeting code and improved readability 2023-01-27 16:43:07 +03:00
6388a53867 Merge branch 'main' of github.com:tdeerenberg/Musort 2023-01-25 23:42:31 +01:00
dfde71a213 Update README.md 2023-01-25 13:44:54 +01:00
2 changed files with 27 additions and 56 deletions

View File

@ -1,7 +1,7 @@
https://user-images.githubusercontent.com/113618658/214463785-49c419e9-c959-4849-91d2-f3407ecaa73d.mp4 https://user-images.githubusercontent.com/113618658/214463785-49c419e9-c959-4849-91d2-f3407ecaa73d.mp4
# Musort # Musort
A Python3 program that renames all selected music/audio files in a folder with a specified naming convention. Names are generated from the metadata (ID3) from the audio files. Before using this program, use a metadata editor like MusicBrainz Picard, Beets or EasyTAG to add the correct metadata to the audio files. Ogranize your music library. A Python3 program that renames all selected music/audio files in a folder with a specified naming convention. Names are generated from the metadata (ID3) from the audio files. Before using this program, use a metadata editor like MusicBrainz Picard, Beets or EasyTAG to add the correct metadata to the audio files.
## Features ## Features

View File

@ -8,7 +8,9 @@ import os
import sys, getopt import sys, getopt
import logging import logging
version = "Musort v0.1 (c) tdeerenberg" supported_formats = ["flac", "mp3", "mp2", "mp1", "opus", "ogg", "wma"]
version = "Musort v0.2 (c) tdeerenberg"
help=\ help=\
"""Musort (c) 2023 tdeerenberg (github.com/tdeerenberg) """Musort (c) 2023 tdeerenberg (github.com/tdeerenberg)
@ -80,28 +82,21 @@ class Music:
def get_compatible(self): def get_compatible(self):
music = [] music = []
for file in self.files: for file in self.files:
match file.split("."): file_extension = file.split(".")[-1]
case [*_, "flac"]:
music.append(file) if file_extension in supported_formats:
case [*_, "mp3"]:
music.append(file)
case [*_, "mp1"]:
music.append(file)
case [*_, "mp2"]:
music.append(file)
case [*_, "opus"]:
music.append(file)
case [*_, "ogg"]:
music.append(file)
case [*_, "wma"]:
music.append(file) music.append(file)
self.compatible = music self.compatible = music
def set_separator(self, sep): def set_separator(self, sep):
"""Sets the separator for naming the audio files """Sets the separator for naming the audio files
(ex. 01-songname.mp3 or 01.songname.flac)""" (ex. 01-songname.mp3 or 01.songname.flac)"""
if sep in ['\\', '/', '|', '*', '<', '>', '"', '?']:
sep = "_"
logging.warning("Given separator contains invalid filename symbols, defaulting to .")
self.separator = sep self.separator = sep
self.separator_status = True
def set_format(self, val): def set_format(self, val):
"""Sets the naming convention of the audio files """Sets the naming convention of the audio files
(ex. title-artist or artist-track-title)""" (ex. title-artist or artist-track-title)"""
@ -125,46 +120,21 @@ class Music:
"""Uses the given format to set new filename""" """Uses the given format to set new filename"""
for f in self.format: for f in self.format:
match f:
case "track": if f == "track":
rename.append(f"{int(track.track):02}") rename.append(f"{int(track.track):02}")
case "album": else:
rename.append(track.album) """getattr gets attribute in track with name f"""
case "albumartist": rename.append(getattr(track, f))
rename.append(track.albumartist)
case "artist":
rename.append(track.artist)
case "audio_offset":
rename.append(track.audio_offset)
case "bitdepth":
rename.append(track.bitdepth)
case "bitrate":
rename.append(track.bitrate)
case "comment":
rename.append(track.commment)
case "composer":
rename.append(track.composer)
case "disc":
rename.append(track.disc)
case "disc_total":
rename.append(track.disc_total)
case "duration":
rename.append(track.duration)
case "filesize":
rename.append(track.filesize)
case "genre":
rename.append(track.genre)
case "samplerate":
rename.append(track.samplerate)
case "title":
rename.append(track.title)
case "track_total":
rename.append(track.track_total)
case "year":
rename.append(track.year)
rename.append(self.separator) rename.append(self.separator)
rename.pop() rename.pop()
rename = ''.join(rename)+ext rename = ''.join(rename)+ext
"""Replacing forbidden path characters in UNIX and Windows with underscores"""
for forbidden_character in ['\\', '/', '|', '*', '<', '>', '"', '?']:
if forbidden_character in rename:
logging.warning(f"Track contains forbidden path character ({forbidden_character}) in the new file name, replaced symbol with _")
rename = rename.replace(forbidden_character, "_")
"""Get the absolute path and rename the audio file""" """Get the absolute path and rename the audio file"""
dst = os.path.join(os.path.abspath(os.path.dirname(file)), rename) dst = os.path.join(os.path.abspath(os.path.dirname(file)), rename)
@ -212,5 +182,6 @@ def main():
music.set_format(sys.argv[2]) music.set_format(sys.argv[2])
music.rename_music() music.rename_music()
if __name__ == "__main__": if __name__ == "__main__":
main() main()