From a35f9d05d49b1de10d245bf664078f183ca8223e Mon Sep 17 00:00:00 2001 From: Christian Colglazier Date: Sat, 14 Aug 2021 09:01:09 -0400 Subject: [PATCH] Started moving to a python script --- audible-converter.py | 34 ++++++++++++++++++++++++++++++++++ audible-converter.sh | 25 +++++++++++++++++++------ 2 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 audible-converter.py diff --git a/audible-converter.py b/audible-converter.py new file mode 100644 index 0000000..053c9a7 --- /dev/null +++ b/audible-converter.py @@ -0,0 +1,34 @@ +import os +import json +import sys + +class AudibleBook(object): + + def __init__(self, filePath): + self.filePath = filePath + 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 \ + -show_chapters -loglevel error -sexagesimal'.format(self.filePath)) + .read())['chapters'] + + # getMetadata() returns audio metadata for a given filepath. + def getMetadata(self): + return self.metadata + + # getChapters() returns chapter metadata for a given filepath. + def getChapters(self): + return self.chapters + +filePath='' +book = AudibleBook(sys.argv[1]) + +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.getMetadata()) diff --git a/audible-converter.sh b/audible-converter.sh index 5eb1340..d709ab9 100755 --- a/audible-converter.sh +++ b/audible-converter.sh @@ -31,11 +31,11 @@ function checkAudibleFile() { } function removeDRM() { - ffmpeg -activation_bytes ${2} -i ${1} -c copy "$(basename ${1} | cut -f 1 -d '.').m4b" + ffmpeg -activation_bytes ${2} -i ${1} -c copy "${3}.m4b" } function getChapterJSON() { - echo $(ffprobe -i ${1} -print_format json -show_chapters -loglevel error -sexagesimal) + echo $(ffprobe -i ${1} -print_format json -show_chapters -loglevel error -sexagesimal | jq '.chapters') } function getMetadataJSON() { @@ -46,6 +46,10 @@ function getTitle() { echo $(echo ${1} | jq '.title' | cleanString) } +function fileSafe() { + sed -e 's/[^A-Za-z0-9._-]/-/g' | tr '[:upper:]' '[:lower:]' | sed -e 's/--/-/g' +} + settingsFile='./settings.conf' audibleFile=${1} @@ -61,9 +65,18 @@ fi . $settingsFile metadataJSON=$(getMetadataJSON $audibleFile) -json=$(getMetadataJSON $audibleFile) -getTitle "${json}" +chapterJSON=$(getChapterJSON $audibleFile) +name=$(getTitle "${metadataJSON}" | fileSafe) -if [ ! -f "$(basename ${audibleFile} | cut -f 1 -d '.').m4b" ]; then - removeDRM $audibleFile $activationBytes +if [ ! -f "${name}.m4b" ]; then + removeDRM $audibleFile $activationBytes $name fi + +mkdir -p $name + +data=$(echo "${chapterJSON}" | jq '.[] | "\(.start),\(.end),\(.tags.title)"') +echo $data +IFS=' ' read -r -a array <<< "$data" + + +echo $(echo "${chapterJSON}" | jq '.[] | .start,.end,.tags.title')