Create output directory

This commit is contained in:
Christian Colglazier 2021-08-14 14:21:17 -04:00
parent efdb203e2c
commit ca54ab5e01

View File

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