From 86b5241f3260b129369ae1617b7d5caf4c672243 Mon Sep 17 00:00:00 2001 From: Christian Colglazier Date: Sat, 14 Aug 2021 15:41:41 -0400 Subject: [PATCH] Split audiobook into several files --- audible-converter.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/audible-converter.py b/audible-converter.py index ea17ab1..644fcf4 100644 --- a/audible-converter.py +++ b/audible-converter.py @@ -5,6 +5,10 @@ import re import json import sys +# osSafeName() convertes a string to an OS safe name. +def osSafeName(name): + return re.sub(r'[^a-zA-Z0-9 -]', '', name).lower().replace(' ', '-').replace('--', '-') + class AudibleBook(object): def __init__(self, filePath, activationBytes, targetDir): @@ -43,6 +47,18 @@ class AudibleBook(object): .format(self.activationBytes, self.filePath, filePath)) self.convertedFilePath = filePath + def splitChapters(self): + for c in self.getChapters(): + start=c['start_time'] + end=c['end_time'] + title=c['tags']['title'] + outFilePath = '{}-{}.m4a'.format(os.path.splitext(self.convertedFilePath)[0], + osSafeName(title)) + if not os.path.exists(outFilePath): + os.system('ffmpeg -i {} -acodec copy -vcodec copy -ss {} -t {} {}'. + format(self.convertedFilePath, start, end, outFilePath)) + + # getHash() returns the hash of a given file. def getHash(filePath): with open(filePath, 'rb') as f: @@ -64,10 +80,6 @@ def createConfig(configFilePath, filePath): f = open(configFilePath, 'w') 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] @@ -92,16 +104,5 @@ if not os.path.exists(outputDir): book.setTargetDir(outputDir) book.removeDRM(name) - -for c in book.getChapters(): - start=c['start'] - end=c['end'] - title=c['tags']['title'] - print(start, end, title) - #os.system('ffmpeg -i {} -acodec copy -vcodec copy -ss {} -t {} OUTFILE-{}.m4a'.format(filePath, start, end, title)) - - -print() - - +book.splitChapters()