From ca54ab5e0100adc28e06642d979eb6a7c9fdfd1e Mon Sep 17 00:00:00 2001 From: Christian Colglazier Date: Sat, 14 Aug 2021 14:21:17 -0400 Subject: [PATCH] Create output directory --- audible-converter.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/audible-converter.py b/audible-converter.py index 8614bb3..ea17ab1 100644 --- a/audible-converter.py +++ b/audible-converter.py @@ -1,14 +1,16 @@ import configparser from requests import get import os +import re import json import sys class AudibleBook(object): - def __init__(self, filePath, activationBytes): + def __init__(self, filePath, activationBytes, targetDir): self.filePath = filePath self.activationBytes = activationBytes + self.targetDir = targetDir self.metadata = json.loads(os.popen('ffprobe -i {} -show_format \ -print_format json'.format(filePath)).read())['format']['tags'] self.chapters = json.loads(os.popen('ffprobe -i {} -print_format json \ @@ -31,10 +33,15 @@ class AudibleBook(object): def getTitle(self): return self.getMetadata()['title'] + def setTargetDir(self, targetDir): + self.targetDir = targetDir + def removeDRM(self, fileName): - os.system('ffmpeg -activation_bytes {} -i {} -c copy "{}.m4b"' - .format(self.activationBytes, self.filePath, fileName)) - print('test') + filePath = '{}/{}.m4b'.format(self.targetDir, fileName) + if not os.path.exists(filePath): + os.system('ffmpeg -activation_bytes {} -i {} -c copy "{}"' + .format(self.activationBytes, self.filePath, filePath)) + self.convertedFilePath = filePath # getHash() returns the hash of a given file. def getHash(filePath): @@ -58,8 +65,13 @@ def createConfig(configFilePath, filePath): f.write('activationBytes={}'.format(activationBytes)) f.close() +# osSafeName() convertes a string to an OS safe name. +def osSafeName(name): + return re.sub(r'[^a-zA-Z0-9 -]', '', name).lower().replace(' ', '-').replace('--', '-') + configFilePath='./settings.conf' audibleBookPath=sys.argv[1] +targetDir='.' # Check if config file exists and creates one if needed. if not os.path.exists(configFilePath): @@ -71,7 +83,15 @@ configParser = configparser.RawConfigParser() configParser.read_string(configString) activationBytes=configParser.get('Settings', 'activationBytes') -book = AudibleBook(audibleBookPath, activationBytes) +book = AudibleBook(audibleBookPath, activationBytes, targetDir) + +name = osSafeName(book.getTitle()) +outputDir = '{}/{}'.format(targetDir, name) +if not os.path.exists(outputDir): + os.makedirs(outputDir) +book.setTargetDir(outputDir) + +book.removeDRM(name) for c in book.getChapters(): start=c['start'] @@ -81,7 +101,7 @@ for c in book.getChapters(): #os.system('ffmpeg -i {} -acodec copy -vcodec copy -ss {} -t {} OUTFILE-{}.m4a'.format(filePath, start, end, title)) -print(book.getTitle()) +print()