Chris Pomeroy
2021-11-21 82a55f1dada836797472da4485997fa9d73374b1
aaxConvert.py
@@ -3,13 +3,12 @@
import os
import glob
import subprocess
import shlex
import json
import re
import requests
import unicodedata
import sys
from querysubsonic import findalbumbyname
from time import sleep
# arguments
# activation_key, file name, codec(default to mp3)
@@ -45,28 +44,40 @@
def getmetadata(aaxfile):
# Returns the metadata from an aax file
    ret = subprocess.run(["ffprobe", "-v", "info", "-hide_banner", "-show_format", "-show_chapters", "-print_format", "json", os.path.abspath(aaxfile)], capture_output=True)
    ret = subprocess.run(["ffprobe", "-v", "info", "-hide_banner", "-show_format", "-show_chapters",
                          "-print_format", "json", os.path.abspath(aaxfile)], capture_output=True)
    mdata = json.loads(ret.stdout)
    aret = ret.stderr.decode().split('\n')[0]
    mdata["checksum"] = aret.split()[-1]
    return mdata
def getmetabitrate():
#Return the bitrate of the media
    bit_rate = metadata['format']['bit_rate']
    return bit_rate[:2]
def getmetacopyright():
# Return normalized copyright data
    copyright = unicodedata.normalize('NFKD', metadata['format']['tags']['copyright']).encode('ascii','ignore')
    return copyright.decode()
def getmetadatatags(key):
# get specific data
    tag = metadata['format']['tags'][key]
    return " ".join(tag.split())
def normalize_data(data):
    # Return a normalized title
    data = data.replace(" ", "_")
    pattern = re.compile('\W')
    return re.sub(pattern, '', data)
def reencode(aaxfile, outpath):
# decrypt and reencode to mp3
    command = ("ffmpeg -loglevel error {} -activation_bytes {} -i {} -vn -codec:a libmp3lame -ab {}k -map_metadata -1 "
@@ -104,6 +115,7 @@
        if i['id'] == cid:
            return i[key]
def movetochapters(path, outpath, chapter, title, start,end):
# Creating individual chapters
@@ -133,9 +145,10 @@
                                                                                                               path, outpath)
    if args.verbose:
        print(command)
    process = process = subprocess.run(command, shell=True)
        subprocess.run(command, shell=True)
    return
def getcorrectkey():
#request the key for the checksum
    try:
@@ -145,20 +158,21 @@
        raise err
        return None
for rfile in glob.glob(args.filename):
    if rfile.find("aax") != -1 and os.path.isfile(rfile):
        metadata = getmetadata(rfile)
        album = getmetadatatags('album')
        #See if we got it already
        if (findalbumbyname(album) == False):
            artist = getmetadatatags('artist')
            title = getmetadatatags('title')
        if not findalbumbyname(album):
            artist = normalize_data(getmetadatatags('artist'))
            title = normalize_data(getmetadatatags('title'))
            act_byte = getcorrectkey()
            if act_byte == None:
            if act_byte is None:
                sys.exit("Can't continue with this file {rfile}")
            else:
                ddir = "%s/%s/%s" % (path, artist.replace(' ', '_'), title.replace(' ', '_'))
                single_file_path = "/processing/%s.mp3" % (title.replace(' ','_'))
                ddir = "%s/%s/%s" % (path, artist, title)
                single_file_path = "/processing/%s.mp3" % (title)
                if not os.path.exists(ddir):
                    os.makedirs(ddir)
                print(ddir)
@@ -177,4 +191,3 @@
                getcoverart(rfile, ddir)
        else:
            print('We have that book already')