Split audiobook into several files

This commit is contained in:
Christian Colglazier 2021-08-14 15:41:41 -04:00
parent ca54ab5e01
commit 86b5241f32

View File

@ -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()