audiobook_tools/03_rename.py
2024-03-21 13:53:08 +00: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()