audiobook_tools/rename.py
2023-10-08 14:37:42 -04:00

45 lines
827 B
Python

import glob
import json
import os
import subprocess
import sys
def run():
if len(sys.argv) > 1:
files = sys.argv[1:]
else:
files = glob.glob("*.m4b")
for f in files:
rename(f)
def rename(f):
p = subprocess.Popen( ["exiftool","-json","-Artist","-Title",f],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = p.communicate()
meta = json.loads(out)[0]
artist = meta['Artist']
title = meta['Title'].split(":")[0]
try:
os.mkdir(f"{artist}")
except FileExistsError as e:
pass
try:
os.mkdir(f"{artist}/{title}")
except FileExistsError as e:
pass
new_path = f"{artist}/{title}/Audiobook.m4b"
os.rename(f, new_path)
if __name__ == "__main__":
run()