Add new music file organization tool
This commit is contained in:
parent
b4c7b7ddde
commit
f14dbdb526
3 changed files with 82 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
.venv
|
||||
62
music/organize.py
Normal file
62
music/organize.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# Author: Jose Stovall
|
||||
## This Script goes through all songs in a given directory and reorganizes them like
|
||||
## iTunes' Library Management does, running just on the tags of the file alone.
|
||||
## This means the input will be <<input_dir>>, and the output will transform all files
|
||||
## in <<input_dir>> to <<input_dir/artist/album/files>>
|
||||
|
||||
|
||||
from os import listdir, makedirs
|
||||
from pathlib import Path
|
||||
from tinytag import TinyTag
|
||||
from argparse import ArgumentParser
|
||||
from pathvalidate import sanitize_filepath
|
||||
from shutil import move as mv
|
||||
|
||||
|
||||
def process_file(file: Path, output_root: Path) -> None:
|
||||
"""Processes a given file, grabbing metadata using TinyTag and then creating the subdirectories as needed
|
||||
|
||||
Args:
|
||||
file (Path): The music file being processed
|
||||
output_root (Path): Where the first level and beyond of subdirs should start from
|
||||
|
||||
Raises:
|
||||
Exception: If any required fields aren't propagated
|
||||
"""
|
||||
tag: TinyTag = TinyTag.get(file)
|
||||
|
||||
if not tag.albumartist:
|
||||
raise Exception("Tag is missing an Artist!")
|
||||
if not tag.album:
|
||||
raise Exception("Tag is missing an Album!")
|
||||
if not tag.title:
|
||||
raise Exception("Tag is missing a Title!")
|
||||
|
||||
output_dir = sanitize_filepath(output_root.joinpath(tag.albumartist).joinpath(tag.album).resolve())
|
||||
makedirs(output_dir, exist_ok=True)
|
||||
new_name = sanitize_filepath(output_dir.joinpath(tag.title.replace("/", "-")).with_suffix(file.suffix))
|
||||
mv(file, new_name)
|
||||
|
||||
|
||||
def main(args) -> None:
|
||||
input_dir = Path(args.input_dir).resolve()
|
||||
output_dir = Path(args.output or args.input_dir).resolve()
|
||||
|
||||
for file in listdir(input_dir):
|
||||
try:
|
||||
file_path = input_dir.joinpath(input_dir).joinpath(file).resolve()
|
||||
process_file(file_path, output_dir)
|
||||
except Exception as e:
|
||||
print(f"Failed to process {file}: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = ArgumentParser(
|
||||
prog="Music Folder Organizer",
|
||||
description="See head of organize.py",
|
||||
)
|
||||
parser.add_argument("input_dir")
|
||||
parser.add_argument("-o", "--output", default=None)
|
||||
|
||||
args = parser.parse_args()
|
||||
main(args)
|
||||
19
music/requirements.txt
Normal file
19
music/requirements.txt
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
astroid==4.0.4
|
||||
black==26.3.1
|
||||
click==8.3.1
|
||||
dill==0.4.1
|
||||
isort==8.0.1
|
||||
mccabe==0.7.0
|
||||
mypy_extensions==1.1.0
|
||||
packaging==26.0
|
||||
pathspec==1.0.4
|
||||
pathvalidate==3.3.1
|
||||
platformdirs==4.9.4
|
||||
pylint==4.0.5
|
||||
pytokens==0.4.1
|
||||
pytoolconfig==1.3.1
|
||||
rope==1.14.0
|
||||
tinytag==2.2.1
|
||||
tomli==2.4.1
|
||||
tomlkit==0.14.0
|
||||
typing_extensions==4.15.0
|
||||
Loading…
Reference in a new issue