Compare commits

...

3 Commits

Author SHA1 Message Date
ccc8d7c7a2 Export cover 2021-08-14 15:49:07 -04:00
86b5241f32 Split audiobook into several files 2021-08-14 15:41:41 -04:00
ca54ab5e01 Create output directory 2021-08-14 14:21:17 -04:00

View File

@ -1,14 +1,20 @@
import configparser import configparser
from requests import get from requests import get
import os import os
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): def __init__(self, filePath, activationBytes, targetDir):
self.filePath = filePath self.filePath = filePath
self.activationBytes = activationBytes self.activationBytes = activationBytes
self.targetDir = targetDir
self.metadata = json.loads(os.popen('ffprobe -i {} -show_format \ self.metadata = json.loads(os.popen('ffprobe -i {} -show_format \
-print_format json'.format(filePath)).read())['format']['tags'] -print_format json'.format(filePath)).read())['format']['tags']
self.chapters = json.loads(os.popen('ffprobe -i {} -print_format json \ self.chapters = json.loads(os.popen('ffprobe -i {} -print_format json \
@ -31,10 +37,32 @@ class AudibleBook(object):
def getTitle(self): def getTitle(self):
return self.getMetadata()['title'] return self.getMetadata()['title']
def setTargetDir(self, targetDir):
self.targetDir = targetDir
def removeDRM(self, fileName): def removeDRM(self, fileName):
os.system('ffmpeg -activation_bytes {} -i {} -c copy "{}.m4b"' filePath = '{}/{}.m4b'.format(self.targetDir, fileName)
.format(self.activationBytes, self.filePath, fileName)) if not os.path.exists(filePath):
print('test') os.system('ffmpeg -activation_bytes {} -i {} -c copy "{}"'
.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))
def getCover(self):
outFilePath = '{}/cover.jpg'.format(self.targetDir)
if not os.path.exists(outFilePath):
os.system('ffmpeg -i {} -an -vcodec copy {}'.
format(self.convertedFilePath, outFilePath))
# getHash() returns the hash of a given file. # getHash() returns the hash of a given file.
def getHash(filePath): def getHash(filePath):
@ -57,9 +85,10 @@ def createConfig(configFilePath, filePath):
f = open(configFilePath, 'w') f = open(configFilePath, 'w')
f.write('activationBytes={}'.format(activationBytes)) f.write('activationBytes={}'.format(activationBytes))
f.close() f.close()
configFilePath='./settings.conf' configFilePath='./settings.conf'
audibleBookPath=sys.argv[1] audibleBookPath=sys.argv[1]
targetDir='.'
# Check if config file exists and creates one if needed. # Check if config file exists and creates one if needed.
if not os.path.exists(configFilePath): if not os.path.exists(configFilePath):
@ -71,17 +100,15 @@ configParser = configparser.RawConfigParser()
configParser.read_string(configString) configParser.read_string(configString)
activationBytes=configParser.get('Settings', 'activationBytes') activationBytes=configParser.get('Settings', 'activationBytes')
book = AudibleBook(audibleBookPath, activationBytes) book = AudibleBook(audibleBookPath, activationBytes, targetDir)
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.getTitle())
name = osSafeName(book.getTitle())
outputDir = '{}/{}'.format(targetDir, name)
if not os.path.exists(outputDir):
os.makedirs(outputDir)
book.setTargetDir(outputDir)
book.removeDRM(name)
book.splitChapters()
book.getCover()