From 336b316f08e69c2ff54f563aba9f2fb593da7db2 Mon Sep 17 00:00:00 2001
From: Chris Pomeroy <chris.pomeroy@ercot.com>
Date: Thu, 10 Aug 2017 22:10:46 +0000
Subject: [PATCH] Updated authcode. updated to convert files in verbose Added example of json output to reference

---
 .authcode       |    2 
 exampleJson.txt |  107 ++++++++++++++++++++++++++
 aaxConvert.py   |   76 +++++++++++++++++-
 3 files changed, 179 insertions(+), 6 deletions(-)

diff --git a/.authcode b/.authcode
index bd41cba..3769522 100755
--- a/.authcode
+++ b/.authcode
@@ -1 +1 @@
-12345
\ No newline at end of file
+2982e205
\ No newline at end of file
diff --git a/aaxConvert.py b/aaxConvert.py
index 94fca03..06ca770 100755
--- a/aaxConvert.py
+++ b/aaxConvert.py
@@ -3,17 +3,76 @@
 import os
 import glob
 import subprocess
+import shlex
 import json
 
 # arguments
 # activation_key, file name, codec(default to mp3)
 parser = argparse.ArgumentParser()
 parser.add_argument("--code", help="Activation Code from audible to decrypt files")
-parser.add_argument("filename", help="Filename to convert")
+parser.add_argument("--single", help="Use this option to create a single file")
+parser.add_argument("--dpath", help="Use this to set the destination path. Otherwise I will use the current directory")
+parser.add_argument("-v", "--verbose", help="Send output to stdout", action="store_true")
+parser.add_argument("filename", help="Filename to convert, or directory to look in")
 
 args = parser.parse_args()
 
 act_byte = ""
+metadata = ""
+
+
+def mungetrack(track):
+    #Making sure the Track is correct
+    c = track.strip('Chapter ')
+    c = c.zfill(2)
+    return c
+
+
+def getmetadata(aaxfile):
+    ret = subprocess.check_output(["ffprobe", "-v", "quiet", "-hide_banner", "-show_format", "-show_chapters", "-print_format", "json", os.path.abspath(aaxfile)])
+    mdata = json.loads(ret)
+    return mdata
+
+
+def getmetabitrate():
+    bit_rate = metadata['format']['bit_rate']
+    return bit_rate[:2]
+
+
+def getmetadatatags(key):
+    #get specific data
+    tag = metadata['format']['tags'][key]
+    return tag
+
+
+def reencode(aaxfile, outpath):
+    # decrypt and reencode to mp3
+    command = ("ffmpeg -loglevel error -stats -activation_bytes {} -i {} -vn -codec:a libmp3lame -ab {}k -map_metadata -1 "
+               "-metadata 'title={}' -metadata 'artist={}' -metadata 'album_artist={}' -metadata 'album={}' -metadata 'date={}' "
+               "-metadata track=1/1 -metadata 'genre={}' -metadata 'copyright={}' '{}'" ).format(act_byte,aaxfile,getmetabitrate(),
+                                                                                           getmetadatatags('title'), getmetadatatags('artist'),
+                                                                                           getmetadatatags('album_artist'), getmetadatatags('album'),
+                                                                                           getmetadatatags('date'), getmetadatatags('genre'),
+                                                                                           getmetadatatags('copyright'),outpath)
+    if args.verbose:
+        print command
+        process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
+        while True:
+            output = process.stdout.readline()
+            if output == '' and process.poll() is not None:
+                break
+            if output:
+                print output.strip()
+        rc = process.poll()
+        return rc
+
+
+
+if args.dpath:
+    path = args.dpath
+else:
+    path = os.getcwd()
+
 if args.code:
     act_byte = args.code
 elif os.path.isfile('./.authcode'):
@@ -26,8 +85,15 @@
 
 for rfile in glob.glob(args.filename):
     if rfile.find("aax") != -1 and os.path.isfile(rfile):
-    #print os.path.abspath(rfile)
-        ret = subprocess.check_output(["ffprobe", "-v", "quiet", "-hide_banner", "-show_format", "-show_chapters", "-print_format", "json", os.path.abspath(rfile)])
-        metadata = json.load(ret)
-        print ret
+        metadata = getmetadata(rfile)
+        album = getmetadatatags('album')
+        artist = getmetadatatags('artist')
+        title = getmetadatatags('title')
+        ddir = "%s/%s/%s" % (path, artist, title)
+        single_file_path = "%s/%s.mp3" % (ddir, title)
+        if not os.path.exists(ddir):
+            os.makedirs(ddir)
+        print ddir
+        reencode(rfile, single_file_path)
+
 
diff --git a/exampleJson.txt b/exampleJson.txt
new file mode 100644
index 0000000..1169c29
--- /dev/null
+++ b/exampleJson.txt
@@ -0,0 +1,107 @@
+{
+    "chapters": [
+        {
+            "id": 0,
+            "time_base": "1/22050",
+            "start": 0,
+            "start_time": "0.000000",
+            "end": 13026304,
+            "end_time": "590.762086",
+            "tags": {
+                "title": "Chapter 1"
+            }
+        },
+        {
+            "id": 1,
+            "time_base": "1/22050",
+            "start": 13026304,
+            "start_time": "590.762086",
+            "end": 57922560,
+            "end_time": "2626.873469",
+            "tags": {
+                "title": "Chapter 2"
+            }
+        },
+        {
+            "id": 2,
+            "time_base": "1/22050",
+            "start": 57922560,
+            "start_time": "2626.873469",
+            "end": 114243584,
+            "end_time": "5181.114921",
+            "tags": {
+                "title": "Chapter 3"
+            }
+        },
+        {
+            "id": 3,
+            "time_base": "1/22050",
+            "start": 114243584,
+            "start_time": "5181.114921",
+            "end": 239122432,
+            "end_time": "10844.554739",
+            "tags": {
+                "title": "Chapter 4"
+            }
+        },
+        {
+            "id": 4,
+            "time_base": "1/22050",
+            "start": 239122432,
+            "start_time": "10844.554739",
+            "end": 320763904,
+            "end_time": "14547.115828",
+            "tags": {
+                "title": "Chapter 5"
+            }
+        },
+        {
+            "id": 5,
+            "time_base": "1/22050",
+            "start": 320763904,
+            "start_time": "14547.115828",
+            "end": 380763136,
+            "end_time": "17268.169433",
+            "tags": {
+                "title": "Chapter 6"
+            }
+        },
+        {
+            "id": 6,
+            "time_base": "1/22050",
+            "start": 380763136,
+            "start_time": "17268.169433",
+            "end": 383987712,
+            "end_time": "17414.408707",
+            "tags": {
+                "title": "Chapter 7"
+            }
+        }
+    ],
+    "format": {
+        "filename": "Downloads/TheLife-ChangingMagicofTidyingUpTheJapaneseArtofDeclutteringandOrganizingUnabridged_ep6_2Mt_bKhhb7GyX0FeBA63-K1aTWlmiGpxbuMEU0WvZd2MmsTUQ_eB9_bhAE_G.aax",
+        "nb_streams": 3,
+        "nb_programs": 0,
+        "format_name": "mov,mp4,m4a,3gp,3g2,mj2",
+        "format_long_name": "QuickTime / MOV",
+        "start_time": "0.000000",
+        "duration": "17414.400000",
+        "size": "140848089",
+        "bit_rate": "64704",
+        "probe_score": 100,
+        "tags": {
+            "major_brand": "aax ",
+            "minor_version": "1",
+            "compatible_brands": "aax M4B mp42isom",
+            "creation_time": "2017-08-08 19:24:33",
+            "genre": "Audiobook",
+            "title": "The Life-Changing Magic of Tidying Up: The Japanese Art of Decluttering and Organizing (Unabridged)",
+            "artist": "Marie Kondo",
+            "album_artist": "Marie Kondo",
+            "album": "The Life-Changing Magic of Tidying Up (Unabridged)",
+            "comment": "Japanese cleaning consultant Marie Kondo takes tidying to a whole new level, promising that if you properly simplify and organize your home once, you'll never have to do it again....",
+            "copyright": "&#169;2014 Marie Kondo ; (P)2015 Tantor",
+            "date": "2015"
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.10.0