Compare commits
13 Commits
1c5303898d
...
master
Author | SHA1 | Date | |
---|---|---|---|
e2552db1be | |||
3edfd24181 | |||
e62dec0833 | |||
ccc8d7c7a2 | |||
86b5241f32 | |||
ca54ab5e01 | |||
efdb203e2c | |||
1aaa28d98d | |||
aa7c0fb106 | |||
a35f9d05d4 | |||
9f34fe4a8a | |||
dce5236b1f | |||
0b04064550 |
5
.gitignore
vendored
5
.gitignore
vendored
@ -3,4 +3,7 @@
|
|||||||
*.m4b
|
*.m4b
|
||||||
|
|
||||||
# Config files
|
# Config files
|
||||||
settings.conf
|
settings.conf
|
||||||
|
|
||||||
|
# Other
|
||||||
|
**/
|
18
README.md
18
README.md
@ -1,2 +1,20 @@
|
|||||||
# audible-converter
|
# audible-converter
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
Install [FFmpeg](https://ffmpeg.org/download.html) and [jq](https://stedolan.github.io/jq/download/)
|
||||||
|
|
||||||
|
### Fedora
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo dnf install ffmpeg jq
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
1. Download audiobook from Audible in .aax format.
|
||||||
|
2. Run the following command
|
||||||
|
```sh
|
||||||
|
./audible-converter.sh [Path to Audiobook file]
|
||||||
|
```
|
||||||
|
3. A converted file will be saved in the current directory.
|
||||||
|
151
audible-converter.py
Normal file
151
audible-converter.py
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
import configparser
|
||||||
|
from requests import get
|
||||||
|
from feedgen.feed import FeedGenerator
|
||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
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):
|
||||||
|
self.filePath = filePath
|
||||||
|
self.activationBytes = activationBytes
|
||||||
|
self.targetDir = targetDir
|
||||||
|
self.convertedFilePath=''
|
||||||
|
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']
|
||||||
|
|
||||||
|
# getPath() returns path to origonal audible file.
|
||||||
|
def getPath(self):
|
||||||
|
return self.filePath
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# getTitle() returns audiobook title.
|
||||||
|
def getTitle(self):
|
||||||
|
return self.getMetadata()['title']
|
||||||
|
|
||||||
|
def getAuthor(self):
|
||||||
|
return self.getMetadata()['artist']
|
||||||
|
|
||||||
|
def setTargetDir(self, targetDir):
|
||||||
|
self.targetDir = targetDir
|
||||||
|
|
||||||
|
def removeDRM(self, fileName):
|
||||||
|
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
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
def deleteConverted(self):
|
||||||
|
if self.convertedFilePath is not '' and os.path.exists(self.configFilePath):
|
||||||
|
os.remove(self.convertedFilePath)
|
||||||
|
|
||||||
|
|
||||||
|
# getHash() returns the hash of a given file.
|
||||||
|
def getHash(filePath):
|
||||||
|
with open(filePath, 'rb') as f:
|
||||||
|
f.seek(653)
|
||||||
|
data = f.read(20)
|
||||||
|
f.close()
|
||||||
|
return data.hex()
|
||||||
|
|
||||||
|
# getActivationBytes returns the bytes needed to decrypt a given hash.
|
||||||
|
def getActivationBytes(filehash):
|
||||||
|
headers = {'User-Agent': 'audible-converter'}
|
||||||
|
response = get('https://aax.api.j-kit.me/api/v2/activation/{}'.format(filehash),
|
||||||
|
headers=headers)
|
||||||
|
return json.loads(response.text)['activationBytes']
|
||||||
|
|
||||||
|
# createConfig() creates a config file.
|
||||||
|
def createConfig(configFilePath, filePath):
|
||||||
|
activationBytes = getActivationBytes(getHash(filePath))
|
||||||
|
f = open(configFilePath, 'w')
|
||||||
|
f.write('activationBytes={}'.format(activationBytes))
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
configFilePath='./settings.conf'
|
||||||
|
audibleBookPath=sys.argv[1]
|
||||||
|
serverURL=sys.argv[2]
|
||||||
|
targetDir='./audiobooks'
|
||||||
|
|
||||||
|
# Check if config file exists and creates one if needed.
|
||||||
|
if not os.path.exists(configFilePath):
|
||||||
|
print('Creating config file')
|
||||||
|
createConfig(configFilePath, audibleBookPath)
|
||||||
|
|
||||||
|
configString = '[Settings]\n' + open(configFilePath).read()
|
||||||
|
configParser = configparser.RawConfigParser()
|
||||||
|
configParser.read_string(configString)
|
||||||
|
activationBytes=configParser.get('Settings', '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)
|
||||||
|
|
||||||
|
rssFilePath='{}/rss.xml'.format(outputDir)
|
||||||
|
|
||||||
|
if not os.path.exists(rssFilePath):
|
||||||
|
book.removeDRM(name)
|
||||||
|
book.splitChapters()
|
||||||
|
book.getCover()
|
||||||
|
|
||||||
|
fg = FeedGenerator()
|
||||||
|
fg.id(name)
|
||||||
|
fg.title(book.getTitle())
|
||||||
|
fg.author( {'name': book.getAuthor()} )
|
||||||
|
fg.link( href='https://audible.com', rel='alternate' )
|
||||||
|
fg.logo('{}/{}/cover.jpg'.format(serverURL, name))
|
||||||
|
fg.subtitle(book.getMetadata()['copyright'])
|
||||||
|
fg.language('en')
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
for c in book.getChapters():
|
||||||
|
key = osSafeName(c['tags']['title'])
|
||||||
|
fe = fg.add_entry()
|
||||||
|
fe.id(key)
|
||||||
|
fe.title(c['tags']['title'])
|
||||||
|
fe.link(href='{0}/{1}/{1}-{2}.m4a'.format(serverURL, name, key))
|
||||||
|
fe.enclosure('{0}/{1}/{1}-{2}.m4a'.format(serverURL, name, key), 0, 'audio')
|
||||||
|
fe.published(datetime.datetime(int(book.getMetadata()['date']), 6, 1, 0, count, tzinfo=datetime.timezone.utc))
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
rssfeed = fg.rss_str(pretty=True)
|
||||||
|
fg.rss_file(rssFilePath)
|
||||||
|
book.deleteConverted()
|
35
audible-converter.sh
Normal file → Executable file
35
audible-converter.sh
Normal file → Executable file
@ -1,5 +1,9 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
cleanString() {
|
||||||
|
tr -d '"'
|
||||||
|
}
|
||||||
|
|
||||||
# getHash returns the hash of a given file.
|
# getHash returns the hash of a given file.
|
||||||
function getHash() {
|
function getHash() {
|
||||||
echo $(xxd -p -l20 -s 653 ${1})
|
echo $(xxd -p -l20 -s 653 ${1})
|
||||||
@ -7,8 +11,8 @@ function getHash() {
|
|||||||
|
|
||||||
# getActivationBytes returns the bytes needed to decrypt a given hash.
|
# getActivationBytes returns the bytes needed to decrypt a given hash.
|
||||||
function getActivationBytes() {
|
function getActivationBytes() {
|
||||||
json=$(curl -A 'Christian Colglazier' https://aax.api.j-kit.me/api/v2/activation/${1})
|
json=$(curl -A 'audible-converter' https://aax.api.j-kit.me/api/v2/activation/${1})
|
||||||
echo $(echo $json | jq '.activationBytes' | tr -d '"')
|
echo $(echo $json | jq '.activationBytes' | cleanString)
|
||||||
}
|
}
|
||||||
|
|
||||||
# createConfig creates a config file.
|
# createConfig creates a config file.
|
||||||
@ -27,11 +31,11 @@ function checkAudibleFile() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function removeDRM() {
|
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() {
|
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() {
|
function getMetadataJSON() {
|
||||||
@ -39,7 +43,11 @@ function getMetadataJSON() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getTitle() {
|
function getTitle() {
|
||||||
echo $(echo ${1} | jq '.title')
|
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'
|
settingsFile='./settings.conf'
|
||||||
@ -57,5 +65,18 @@ fi
|
|||||||
. $settingsFile
|
. $settingsFile
|
||||||
|
|
||||||
metadataJSON=$(getMetadataJSON $audibleFile)
|
metadataJSON=$(getMetadataJSON $audibleFile)
|
||||||
json=$(getMetadataJSON $audibleFile)
|
chapterJSON=$(getChapterJSON $audibleFile)
|
||||||
removeDRM $audibleFile $activationBytes
|
name=$(getTitle "${metadataJSON}" | fileSafe)
|
||||||
|
|
||||||
|
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')
|
||||||
|
Reference in New Issue
Block a user