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 json
import sys 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): class AudibleBook(object):
def __init__(self, filePath, activationBytes, targetDir): def __init__(self, filePath, activationBytes, targetDir):
@ -43,6 +47,18 @@ class AudibleBook(object):
.format(self.activationBytes, self.filePath, filePath)) .format(self.activationBytes, self.filePath, filePath))
self.convertedFilePath = 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. # getHash() returns the hash of a given file.
def getHash(filePath): def getHash(filePath):
with open(filePath, 'rb') as f: with open(filePath, 'rb') as f:
@ -65,10 +81,6 @@ def createConfig(configFilePath, filePath):
f.write('activationBytes={}'.format(activationBytes)) f.write('activationBytes={}'.format(activationBytes))
f.close() 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' configFilePath='./settings.conf'
audibleBookPath=sys.argv[1] audibleBookPath=sys.argv[1]
targetDir='.' targetDir='.'
@ -92,16 +104,5 @@ if not os.path.exists(outputDir):
book.setTargetDir(outputDir) book.setTargetDir(outputDir)
book.removeDRM(name) book.removeDRM(name)
book.splitChapters()
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()