mirror of
https://github.com/tdeerenberg/Musort.git
synced 2025-07-17 19:04:18 +00:00
Compare commits
32 Commits
Author | SHA1 | Date | |
---|---|---|---|
3eb8557185 | |||
77c297e9af | |||
20e02f9523 | |||
8da70bddff | |||
fa5555cea4 | |||
12955f4792 | |||
5f9abeef32 | |||
fbc3c3aea3 | |||
f243f6da08 | |||
1b23e84331 | |||
ca41f7a5f3 | |||
915b4807f2 | |||
49d3f6e2be | |||
ff2b0ce943 | |||
860bc8bd68 | |||
63e9b3a9d7 | |||
d47e1ce80d | |||
8f7666005f | |||
5bd6788f13 | |||
d9dee8e00a | |||
bf3b769739 | |||
ce68184ca3 | |||
f4e4aa51bc | |||
ee8753d561 | |||
f968b002a5 | |||
e87cc49b28 | |||
3c79631a84 | |||
b13711f3d6 | |||
0aeb8a15ce | |||
239a71bec8 | |||
6388a53867 | |||
dfde71a213 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
src/__pycache__
|
63
README.md
63
README.md
@ -1,12 +1,14 @@
|
||||
https://user-images.githubusercontent.com/113618658/214463785-49c419e9-c959-4849-91d2-f3407ecaa73d.mp4
|
||||
# Musort
|
||||
> Backup/Mirror of this repository (in case anything happens to this repo on GitHub): <br> https://git.vigenere.nl/tdeerenberg/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.
|
||||
https://github.com/tdeerenberg/Musort/assets/113618658/38ce8dc2-7a5f-4edd-9d56-11408d1d56dd
|
||||
|
||||
# Musort
|
||||
Musort: Effortlessly organize your music library with this Python3 program. Rename selected music/audio files in a folder using a customizable naming convention based on metadata (ID3) from the audio files. Ensure accurate metadata by using popular tools like MusicBrainz Picard, Beets, or EasyTAG before running Musort. Simplify your music organization and enhance file names for a more enjoyable library experience.
|
||||
|
||||
## Features
|
||||
|
||||
* Rename many audio files at once
|
||||
* Rename all files in subdirectories as well (recursive)
|
||||
* Rename audio files in subdirectories as well (recursive)
|
||||
* Choose the naming convention (ex. track.title.flac or artist.track.year.mp3)
|
||||
* Give a separator for the naming of the file (ex. track.title.flac or track_title.flac)
|
||||
* Works on all systems that can run Python
|
||||
@ -21,21 +23,28 @@ A Python3 program that renames all selected music/audio files in a folder with a
|
||||
* AIFF/AIFF-C
|
||||
|
||||
## Dependencies
|
||||
**Note: When using the install script, TinyTag will be automatically installed**
|
||||
**Make sure to install these programs to be able to run Musort**
|
||||
- [Python3](https://www.python.org/)
|
||||
- [TinyTag](https://pypi.org/project/tinytag/) (Installable from Python Package Index)
|
||||
- [Docker (Optional)](https://docker.com)
|
||||
|
||||
The Python3 library TinyTag is also used, but is already included in this repository. Therefore, there is no need to install TinyTag for only this project.
|
||||
|
||||
## Installation and Usage
|
||||
|
||||
### Method 1: Run installation script (Unix/Linux based OS only)
|
||||
The installation script will move the python program to `~/.local/bin`. Make sure that `~/.local/bin` exists and that is added to $PATH.
|
||||
|
||||
The installation script will move the python program to `~/.local/bin`. The installation directory can be changed in the `install.sh` script. **Note: The installation directory should be added to $PATH**
|
||||
``` Bash
|
||||
git clone https://github.com/tdeerenberg/Musort.git
|
||||
cd Musort.git
|
||||
cd Musort
|
||||
chmod +x install.sh
|
||||
./install.sh
|
||||
```
|
||||
After that, simply use the command `musort` to use the program.
|
||||
<hr>
|
||||
|
||||
### Method 2: Clone repo and run manually (All Operating Systems)
|
||||
|
||||
Clone the repository and run the Python program
|
||||
``` Bash
|
||||
git clone https://github.com/tdeerenberg/Musort.git
|
||||
@ -43,21 +52,42 @@ cd Musort
|
||||
pip install requirements.txt
|
||||
```
|
||||
After that, run the program with `python3 musort.py`.
|
||||
## Manual (options and arguments) `musort --help`
|
||||
<hr>
|
||||
|
||||
### Method 3: Docker installation
|
||||
|
||||
``` Bash
|
||||
git clone https://github.com/tdeerenberg/Musort.git
|
||||
cd Musort
|
||||
docker build -t musort .
|
||||
```
|
||||
After the Docker installation/build is complete, Musort can be run with:
|
||||
|
||||
`docker run --name musort --rm -v "[music_directory_host]:[music_directory_container]" -it musort [music_directory_container]`
|
||||
|
||||
The music folder must be mounted to the Docker container, therefore the `-v` option must be used to mount the directory.
|
||||
|
||||
An example of running Musort in Docker, using `/home/user/music` as music folder:
|
||||
|
||||
`docker run --name musort --rm -v '/home/user/music:/music' -it musort /music`
|
||||
|
||||
## Manual with options and arguments (`musort --help`)
|
||||
```
|
||||
USAGE:
|
||||
musort [DIRECTORY] [NAMING_CONVENTION] [OPTIONAL_OPTIONS]...
|
||||
musort [DIRECTORY] [OPTIONAL_PARAMETERS]
|
||||
USAGE EXAMPLES:
|
||||
musort ~/music track.title.year -s _ -r
|
||||
musort /local/music disc.artist.title.album -r
|
||||
musort ~/my_music track.title
|
||||
musort ~/music
|
||||
musort /local/music -f disc.artist.title.album -r
|
||||
musort ~/my_music -s _ -r
|
||||
|
||||
OPTIONAL OPTIONS:
|
||||
-h, --help Show the help menu
|
||||
-f, --format set the naming convention (see 'NAMING CONVENTION:' below)
|
||||
-s, --separator Set the separator for the filename (ex. '-s .' -> 01.track.flac and '-s -' -> 01-track.mp3)
|
||||
Default separator ( . ) will be used if none is given
|
||||
Default separator '_' will be used if none is given
|
||||
-r, --recursive Rename files in subdirectories as well
|
||||
-v, --version Prints the version number
|
||||
|
||||
NAMING CONVENTION:
|
||||
FORMAT_OPTION.FORMAT_OPTION... The amount of format options does not matter.
|
||||
It can be one, two, three, even all of them.
|
||||
@ -85,12 +115,13 @@ year year or date as string
|
||||
|
||||
## Possible features to add
|
||||
* Rename single file
|
||||
* Other installation methods (Like AUR, Docker, etc.)
|
||||
* Other installation methods (e.g. via AUR)
|
||||
* Open for suggestions!
|
||||
* Feel free to open a pull request or issue!
|
||||
|
||||
## Authors
|
||||
|
||||
- [@tdeerenberg](https://www.github.com/tdeerenberg)
|
||||
- [@tdeerenberg](https://github.com/tdeerenberg)
|
||||
|
||||
## License
|
||||
|
||||
|
39
dockerfile
Normal file
39
dockerfile
Normal file
@ -0,0 +1,39 @@
|
||||
# Musort - Docker installation
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# Sources on github:
|
||||
# https://github.com/tdeerenberg/Musort
|
||||
#
|
||||
# Licensed under the GNU General Public License v3.0 (GPLv3)
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
FROM python:3.11
|
||||
|
||||
# Update and upgrade packages
|
||||
RUN apt-get update && apt-get upgrade -y
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /opt/musort/
|
||||
|
||||
# Copy project
|
||||
COPY ./src/config.py /opt/musort/config.py
|
||||
COPY ./src/variables.py /opt/musort/variables.py
|
||||
COPY ./src/tinytag.py /opt/musort/tinytag.py
|
||||
COPY ./src/musort.py /opt/musort/musort.py
|
||||
|
||||
# docker run --name musort --rm -it musort --help
|
||||
ENTRYPOINT ["python3", "musort.py"]
|
60
install.sh
60
install.sh
@ -1,5 +1,57 @@
|
||||
#!/bin/bash
|
||||
# Make sure to add ~/.local/bin to $PATH
|
||||
pip3 install -r requirements.txt
|
||||
cp src/musort.py ~/.local/bin/musort
|
||||
echo "IF NOT DONE ALREADY, ADD '~/local/bin' TO $ PATH"
|
||||
#
|
||||
# Musort - Install and Upgrade Script
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# Sources on github:
|
||||
# https://github.com/tdeerenberg/Musort
|
||||
#
|
||||
# Licensed under the GNU General Public License v3.0 (GPLv3)
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
# Define paths
|
||||
install_path=~/.local/bin
|
||||
script_name=musort
|
||||
script_path=src/musort.py
|
||||
script_config=src/config.py
|
||||
script_tinytag=src/tinytag.py
|
||||
script_variables=src/variables.py
|
||||
|
||||
# Check if script already exists
|
||||
if [ -e "$install_path/$script_name" ]; then
|
||||
read -p "Musort is already installed. Do you want to overwrite it with the new version? (y/n): " response
|
||||
if [[ $response =~ ^[Yy]$ ]]; then
|
||||
echo "Updating Musort..."
|
||||
cp "$script_path" "$install_path/$script_name"
|
||||
cp "$script_config" "$install_path/"
|
||||
cp "$script_tinytag" "$install_path/"
|
||||
cp "$script_variables" "$install_path/"
|
||||
chmod +x "$install_path/$script_name"
|
||||
echo "Musort updated successfully!"
|
||||
else
|
||||
echo "Upgrade canceled. Musort remains unchanged."
|
||||
fi
|
||||
else
|
||||
# Install Musort
|
||||
cp "$script_path" "$install_path/$script_name"
|
||||
cp "$script_config" "$install_path/"
|
||||
cp "$script_tinytag" "$install_path/"
|
||||
cp "$script_variables" "$install_path/"
|
||||
chmod +x "$install_path/$script_name"
|
||||
echo "Musort installed successfully!"
|
||||
fi
|
||||
|
||||
echo "If not done already, add '$install_path' to \$PATH"
|
||||
|
@ -1 +0,0 @@
|
||||
TinyTag
|
43
src/config.py
Normal file
43
src/config.py
Normal file
@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Musort - A command-line tool for effortlessly organizing and renaming your music files based on metadata
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# Sources on github:
|
||||
# https://github.com/tdeerenberg/Musort
|
||||
#
|
||||
# Licensed under the GNU General Public License v3.0 (GPLv3)
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
#
|
||||
# These are the default settings of Musort.
|
||||
# Default settings are used when no parameters are given.
|
||||
# The default settings may be changed to your liking.
|
||||
#
|
||||
|
||||
# Change separator between the naming format
|
||||
separator = "."
|
||||
|
||||
# Toggle recursively renaming though subdirectories
|
||||
recursive = False
|
||||
|
||||
# Format for renaming music files
|
||||
name_format = "track.title"
|
||||
|
||||
# Replacement for illegal characters
|
||||
forbidden_char_replace = "_"
|
309
src/musort.py
Executable file → Normal file
309
src/musort.py
Executable file → Normal file
@ -1,216 +1,151 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Licensed under GPLv3
|
||||
#
|
||||
# Musort - A command-line tool for effortlessly organizing and renaming your music files based on metadata
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# Sources on github:
|
||||
# https://github.com/tdeerenberg/Musort
|
||||
#
|
||||
# Licensed under the GNU General Public License v3.0 (GPLv3)
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from tinytag import TinyTag
|
||||
import os
|
||||
import sys, getopt
|
||||
import sys
|
||||
import getopt
|
||||
import logging
|
||||
from config import *
|
||||
from variables import *
|
||||
from tinytag import TinyTag
|
||||
|
||||
version = "Musort v0.1 (c) tdeerenberg"
|
||||
help=\
|
||||
"""Musort (c) 2023 tdeerenberg (github.com/tdeerenberg)
|
||||
class Musort:
|
||||
def __init__(self):
|
||||
"""Initial setup"""
|
||||
self.recursive = recursive
|
||||
self.separator = separator
|
||||
self.format = name_format.split('.')
|
||||
self.replace = forbidden_char_replace
|
||||
self.directory = None
|
||||
self.files = []
|
||||
logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] %(asctime)s - %(message)s')
|
||||
|
||||
DESCRIPTION:
|
||||
A Python3 program that renames all selected music/audio files in a folder with a specified naming convention
|
||||
def display_settings(self):
|
||||
"""Displays the settings"""
|
||||
logging.info(f"Recursive renaming: '{self.recursive}'.")
|
||||
logging.info(f"Separator: '{self.separator}'.")
|
||||
logging.info(f"Format: {self.format}.")
|
||||
|
||||
USAGE:
|
||||
musort [DIRECTORY] [NAMING_CONVENTION] [OPTIONAL_OPTIONS]...
|
||||
def get_files(self):
|
||||
"""Check files from the given directory"""
|
||||
if self.recursive:
|
||||
self.files = [
|
||||
os.path.join(root, filename)
|
||||
for root, _, files in os.walk(self.directory)
|
||||
for filename in files
|
||||
if self.is_compatible(filename)
|
||||
]
|
||||
else:
|
||||
self.files = [
|
||||
os.path.join(os.path.abspath(self.directory), filename)
|
||||
for filename in os.listdir(self.directory)
|
||||
if self.is_compatible(filename)
|
||||
]
|
||||
|
||||
USAGE EXAMPLES:
|
||||
musort ~/music track.title.year -s _ -r
|
||||
musort /local/music disc.artist.title.album -r
|
||||
musort ~/my_music track.title
|
||||
def is_compatible(self, filename):
|
||||
"""Check if a file is one of the compatible music files based on the extension"""
|
||||
file_extension = filename.split(".")[-1].lower()
|
||||
return file_extension in supported_formats
|
||||
|
||||
OPTIONAL OPTIONS:
|
||||
-h, --help Show the help menu
|
||||
-s, --separator Set the separator for the filename (ex. '-s .' -> 01.track.flac and '-s -' -> 01-track.mp3)
|
||||
Default separator ( . ) will be used if none is given
|
||||
-r, --recursive Rename files in subdirectories as well
|
||||
-v, --version Prints the version number
|
||||
|
||||
NAMING CONVENTION:
|
||||
FORMAT_OPTION.FORMAT_OPTION... The amount of format options does not matter.
|
||||
It can be one, two, three, even all of them.
|
||||
(See FORMAT OPTIONS below for all options)
|
||||
|
||||
FORMAT OPTIONS:
|
||||
album album as string
|
||||
albumartist album artist as string
|
||||
artist artist name as string
|
||||
audio_offset number of bytes before audio data begins
|
||||
bitdepth bit depth for lossless audio
|
||||
bitrate bitrate in kBits/s
|
||||
comment file comment as string
|
||||
composer composer as string
|
||||
disc disc number
|
||||
disc_total the total number of discs
|
||||
duration duration of the song in seconds
|
||||
filesize file size in bytes
|
||||
genre genre as string
|
||||
samplerate samples per second
|
||||
title title of the song
|
||||
track track number as string
|
||||
track_total total number of tracks as string
|
||||
year year or date as string
|
||||
|
||||
SUPPORTED AUDIO FORMATS:
|
||||
MP3/MP2/MP1 (ID3 v1, v1.1, v2.2, v2.3+)
|
||||
Wave/RIFF
|
||||
OGG
|
||||
OPUS
|
||||
FLAC
|
||||
WMA
|
||||
MP4/M4A/M4B/M4R/M4V/ALAC/AAX/AAXC"""
|
||||
|
||||
class Music:
|
||||
def get_files(self, directory):
|
||||
"""Scans the set directory for compatible audio files"""
|
||||
self.files = list(map(lambda x: os.path.join(os.path.abspath(directory), x),os.listdir(directory)))
|
||||
|
||||
def get_files_recursive(self, directory):
|
||||
"""Scans the set directory with subdirectory for compatible audio files"""
|
||||
files = []
|
||||
for a, b, c in os.walk(directory):
|
||||
for d in c:
|
||||
files.append(os.path.join(a,d))
|
||||
self.files = files
|
||||
|
||||
def get_compatible(self):
|
||||
music = []
|
||||
for file in self.files:
|
||||
match file.split("."):
|
||||
case [*_, "flac"]:
|
||||
music.append(file)
|
||||
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)
|
||||
self.compatible = music
|
||||
|
||||
def set_separator(self, sep):
|
||||
"""Sets the separator for naming the audio files
|
||||
(ex. 01-songname.mp3 or 01.songname.flac)"""
|
||||
self.separator = sep
|
||||
self.separator_status = True
|
||||
def set_format(self, val):
|
||||
"""Sets the naming convention of the audio files
|
||||
(ex. title-artist or artist-track-title)"""
|
||||
self.format = val.split(".")
|
||||
|
||||
# Rename files
|
||||
def rename_music(self):
|
||||
"""Rename all compatible music files"""
|
||||
"""Rename all provided music"""
|
||||
for file in self.files:
|
||||
# Get the file extension
|
||||
filename, extension = os.path.splitext(file)
|
||||
|
||||
"""Get the file extension (ex. .flac, .mp3, etc)"""
|
||||
for file in self.compatible:
|
||||
ext = file.split(".")
|
||||
ext = "." + ext[-1]
|
||||
|
||||
"""Let TinyTag module read the audio file"""
|
||||
# Read metadata
|
||||
track = TinyTag.get(file)
|
||||
|
||||
"""Print the progress (Current track)"""
|
||||
logging.info(f"Current track: '{track.artist}' - '{track.title}'")
|
||||
rename = []
|
||||
# Show progress
|
||||
logging.info(f"Renaming track: '{track.artist}' - '{track.title}'.")
|
||||
|
||||
"""Uses the given format to set new filename"""
|
||||
for f in self.format:
|
||||
match f:
|
||||
case "track":
|
||||
rename.append(f"{int(track.track):02}")
|
||||
case "album":
|
||||
rename.append(track.album)
|
||||
case "albumartist":
|
||||
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)
|
||||
# Use given format to set a new filename
|
||||
rename = []
|
||||
for metadata_field in self.format:
|
||||
if metadata_field == "track":
|
||||
rename.append(f"{int(track.track):02}")
|
||||
else:
|
||||
rename.append(getattr(track, metadata_field))
|
||||
rename.append(self.separator)
|
||||
rename.pop()
|
||||
rename = ''.join(rename)+ext
|
||||
|
||||
"""Get the absolute path and rename the audio file"""
|
||||
dst = os.path.join(os.path.abspath(os.path.dirname(file)), rename)
|
||||
os.rename(file, dst)
|
||||
logging.info("Actions finished")
|
||||
# Replace forbidden characters
|
||||
rename = ''.join(rename)
|
||||
for char in invalid_characters:
|
||||
rename = rename.replace(char, self.replace)
|
||||
|
||||
def main():
|
||||
level = logging.DEBUG
|
||||
logging.basicConfig(level=level, format='[%(levelname)s] %(asctime)s - %(message)s')
|
||||
"""Runs the whole program"""
|
||||
argv = sys.argv[3:]
|
||||
# Get absolute path and rename the audio file
|
||||
new_path = os.path.join(os.path.abspath(os.path.dirname(file)), rename + extension)
|
||||
os.rename(file, new_path)
|
||||
|
||||
logging.info(f"Track: '{track.artist}' - '{track.title}' contained an illegal character; the character has been replaced with: '{self.replace}'.")
|
||||
logging.info("Renaming finished.")
|
||||
|
||||
def parse_args(argv, m_class):
|
||||
"""Parse command line arguments"""
|
||||
try:
|
||||
opts, args = getopt.getopt(argv, "s:r", ["sep=", "recursive="])
|
||||
except getopt.GetoptError as err:
|
||||
logging.error(err)
|
||||
opts, args = getopt.getopt(argv[2:], "s:rf:", ["separator=", "recursive", "format="])
|
||||
except getopt.GetoptError as error_mesg:
|
||||
logging.error(error_mesg)
|
||||
exit()
|
||||
|
||||
music = Music()
|
||||
# Handle command line arguments
|
||||
for opt, arg in opts:
|
||||
if opt in ['-s', '--separator']:
|
||||
logging.info(f"Using {arg} as separator")
|
||||
music.set_separator(arg)
|
||||
if opt in ['-r', '--recursive']:
|
||||
logging.info("Running recursively")
|
||||
music.get_files_recursive(sys.argv[1])
|
||||
music.get_compatible()
|
||||
m_class.separator = arg if check_separator(arg) else default_separator
|
||||
elif opt in ['-r', '--recursive']:
|
||||
m_class.recursive = True
|
||||
elif opt in ['-f', '--format']:
|
||||
m_class.format = arg.split(".")
|
||||
|
||||
if sys.argv[1] == "-h" or sys.argv[1] == '--help':
|
||||
print(help)
|
||||
# Handle help and version options
|
||||
if '-h' in sys.argv or '--help' in sys.argv:
|
||||
print(help_text)
|
||||
exit()
|
||||
if sys.argv[1] == '-v' or sys.argv[1] == '--version':
|
||||
print(version)
|
||||
elif '-v' in sys.argv or '--version' in sys.argv:
|
||||
print(version_text)
|
||||
exit()
|
||||
try:
|
||||
music.compatible
|
||||
except:
|
||||
logging.info("Running not recursively")
|
||||
music.get_files(sys.argv[1])
|
||||
music.get_compatible()
|
||||
try:
|
||||
music.separator
|
||||
except:
|
||||
logging.info("Using default separator")
|
||||
music.set_separator(".")
|
||||
|
||||
music.set_format(sys.argv[2])
|
||||
music.rename_music()
|
||||
# Set directory
|
||||
m_class.directory = sys.argv[1]
|
||||
|
||||
if m_class.directory is None:
|
||||
logging.error("Please provide a music directory.")
|
||||
exit()
|
||||
|
||||
def check_separator(sep):
|
||||
if any(char in separator for char in invalid_characters):
|
||||
logging.warning(f"Given separator contains invalid filename symbols, defaulting to '{separator}'.\n")
|
||||
return False
|
||||
return True
|
||||
|
||||
def main():
|
||||
m_class = Musort()
|
||||
parse_args(sys.argv, m_class)
|
||||
m_class.get_files()
|
||||
m_class.display_settings()
|
||||
m_class.rename_music()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
1394
src/tinytag.py
Normal file
1394
src/tinytag.py
Normal file
File diff suppressed because it is too large
Load Diff
73
src/variables.py
Normal file
73
src/variables.py
Normal file
@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Musort - A command-line tool for effortlessly organizing and renaming your music files based on metadata
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# Sources on github:
|
||||
# https://github.com/tdeerenberg/Musort
|
||||
#
|
||||
# Licensed under the GNU General Public License v3.0 (GPLv3)
|
||||
# Copyright (C) 2023 tdeerenberg
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
version_text = "Musort v1.0 (C) tdeerenberg"
|
||||
supported_formats = ["flac", "mp3", "mp2", "mp1", "opus", "ogg", "wma"]
|
||||
invalid_characters = ["\\", "/", "|", "*", "<", ">", '"', "'", "?", "#", ":", ";"]
|
||||
default_separator = '_'
|
||||
help_text = \
|
||||
"""Musort (c) 2023 tdeerenberg (github.com/tdeerenberg)
|
||||
|
||||
DESCRIPTION:
|
||||
A command-line tool for effortlessly organizing and renaming your music files based on metadata
|
||||
|
||||
USAGE:
|
||||
musort [DIRECTORY] [OPTIONAL_PARAMETERS]
|
||||
|
||||
USAGE EXAMPLES:
|
||||
musort ~/music
|
||||
musort /local/music -f disc.artist.title.album -r
|
||||
musort ~/my_music -s _ -r
|
||||
|
||||
OPTIONAL OPTIONS:
|
||||
-h, --help Show the help menu
|
||||
-f, --format set the naming convention (see 'NAMING CONVENTION:' below)
|
||||
-s, --separator Set the separator for the filename (ex. '-s .' -> 01.track.flac and '-s -' -> 01-track.mp3)
|
||||
Default separator '_' will be used if none is given
|
||||
-r, --recursive Rename files in subdirectories as well
|
||||
-v, --version Prints the version number
|
||||
|
||||
NAMING CONVENTION:
|
||||
FORMAT_OPTION.FORMAT_OPTION... The amount of format options does not matter.
|
||||
It can be one, two, three, even all of them.
|
||||
(See FORMAT OPTIONS below for all options)
|
||||
FORMAT OPTIONS:
|
||||
album album as string
|
||||
albumartist album artist as string
|
||||
artist artist name as string
|
||||
audio_offset number of bytes before audio data begins
|
||||
bitdepth bit depth for lossless audio
|
||||
bitrate bitrate in kBits/s
|
||||
comment file comment as string
|
||||
composer composer as string
|
||||
disc disc number
|
||||
disc_total the total number of discs
|
||||
duration duration of the song in seconds
|
||||
filesize file size in bytes
|
||||
genre genre as string
|
||||
samplerate samples per second
|
||||
title title of the song
|
||||
track track number as string
|
||||
track_total total number of tracks as string
|
||||
year year or date as string"""
|
Reference in New Issue
Block a user