init
This commit is contained in:
7
README
Normal file
7
README
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# music-scripts
|
||||||
|
|
||||||
|
The name should be self-explainatory
|
||||||
|
|
||||||
|
## Scripts
|
||||||
|
|
||||||
|
``folder-to-genre.py``: Recurses through a given folder and adds the parent folder as genre to all found .flac files
|
||||||
55
folder-to-genre.py
Normal file
55
folder-to-genre.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from mutagen.flac import FLAC
|
||||||
|
|
||||||
|
|
||||||
|
def seek_and_tag(root_dir, replace=False, dry_run=False):
|
||||||
|
root = Path(root_dir).expanduser().resolve()
|
||||||
|
|
||||||
|
if not root.is_dir():
|
||||||
|
print(f"Error: not a directory: {root}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
for file in root.rglob("*.flac"):
|
||||||
|
folder = file.parent.name.strip()
|
||||||
|
if not folder:
|
||||||
|
print(f"{file}: no parent folder name found, skipping...")
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
audio = FLAC(file)
|
||||||
|
|
||||||
|
old_genres = list(audio.get("genre", []))
|
||||||
|
|
||||||
|
if folder in old_genres:
|
||||||
|
print(f"{file}: folder ({folder}) already in genres, skipping...")
|
||||||
|
continue
|
||||||
|
new_genres = [folder] if replace else [*old_genres, folder]
|
||||||
|
|
||||||
|
print(f"{file}:")
|
||||||
|
print(f" Old: {old_genres}")
|
||||||
|
print(f" New: {new_genres}")
|
||||||
|
if not dry_run:
|
||||||
|
audio["genre"] = new_genres
|
||||||
|
audio.save()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"{file}: Failed: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print(
|
||||||
|
f"Usage: python {sys.argv[0]} [--replace] [--dry-run] /path/to/music",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
print("", file=sys.stderr)
|
||||||
|
print("\t--dry-run: don't modify files", file=sys.stderr)
|
||||||
|
print("\t--replace: remove original genres", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
replace = "--replace" in sys.argv
|
||||||
|
dry_run = "--dry-run" in sys.argv
|
||||||
|
root_dir = sys.argv[-1]
|
||||||
|
|
||||||
|
seek_and_tag(root_dir, replace=replace, dry_run=dry_run)
|
||||||
Reference in New Issue
Block a user