|
@echo off |
|
setlocal enableextensions |
|
chcp 65001 |
|
set input=%1 |
|
set device=%2 |
|
set loglevel=quiet |
|
for %%i in ("%~f1") do set dirname=%%~dpi |
|
for %%i in ("%~f1") do set extension=%%~xi |
|
|
|
set tempfile=%dirname%%RANDOM%-temp.m4a |
|
set tempimage=%dirname%%RANDOM%-temp.png |
|
|
|
if /i "%1" EQU "" (echo Usage: %0 ^<filename^> ^<device^> & goto :eof) |
|
if /i "%extension%" NEQ ".aax" (echo Not an AAX file! & goto :eof) |
|
if /i "%2" EQU "" set device=0 |
|
if /i "%3" EQU "debug" set loglevel=info |
|
where /q ffmpeg || (echo Please install ffmpeg! & goto :eof) |
|
where /q jq || (echo Please install jq! & goto :eof) |
|
|
|
echo. |
|
rem http://stackoverflow.com/a/12730022/2710739 |
|
for /f "tokens=2*" %%a in ('reg.exe query "HKLM\SOFTWARE\WOW6432Node\Audible\SWGIDMAP" /v "%device%"') do set bytes=%%b |
|
set activation_bytes=%bytes:~6,2%%bytes:~4,2%%bytes:~2,2%%bytes:~0,2% |
|
if /i "%bytes%" EQU "" echo Device %device% does not exist! & goto :eof |
|
if /i "%activation_bytes%" EQU "FFFFFFFF" echo Device %device% is not activated! & goto :eof |
|
echo Using Device %device% (Activation Bytes: %activation_bytes%) |
|
|
|
echo. |
|
echo Reading audiobook information ... |
|
for /f "tokens=*" %%i in ('ffprobe "%input%" -v %loglevel% -print_format json -show_format ^| jq -r ".format.tags.artist"') do set artist=%%i |
|
for /f "tokens=*" %%i in ('ffprobe "%input%" -v %loglevel% -print_format json -show_format ^| jq -r ".format.tags.title"') do set title=%%i |
|
for /f "tokens=*" %%i in ('ffprobe "%input%" -v %loglevel% -print_format json -show_format ^| jq -r ".format.tags.date"') do set year=%%i |
|
for /f "tokens=*" %%i in ('ffprobe "%input%" -v %loglevel% -print_format json -show_format ^| jq -r """\(.format.duration|(tonumber/60/60)|floor)h\(.format.duration|(tonumber%%60))m"""') do set duration=%%i |
|
set filename=%artist% - %title% [%year%].m4a |
|
set filename=%filename:\=% |
|
set filename=%filename:/=% |
|
set filename=%filename::=% |
|
set filename=%filename:?=% |
|
set filename=%filename:"=% |
|
set filename=%filename:<=% |
|
set filename=%filename:>=% |
|
set filename=%filename:|=% |
|
rem crashes the cmd if variables contain umlauts ... |
|
rem echo %artist% - %title% [%year%] (Duration: %duration%) |
|
|
|
echo. |
|
echo Converting to m4a ... |
|
ffmpeg -y -v quiet -i "%input%" "%tempimage%" |
|
ffmpeg -y -v %loglevel% -stats -activation_bytes %activation_bytes% -i "%input%" -c:a copy -vn "%tempfile%" || (echo Audiobook can't be unlocked with this Activation Bytes: %activation_bytes% & goto :cleanup) |
|
|
|
echo. |
|
echo Adding looped cover image audiobook |
|
ffmpeg -y -v %loglevel% -stats -r 1 -loop 1 -i "%tempimage%" -i "%tempfile%" -c:a copy -shortest "%dirname%%filename%" |
|
echo. |
|
echo Done! |
|
goto :cleanup |
|
|
|
:cleanup |
|
del /q "%tempfile%" >nul 2>&1 |
|
del /q "%tempimage%" >nul 2>&1 |
|
goto :eof |
So I'll start by admitting I'm not too smart on all this. I created a GitHub account in order to comment here if that shows how new I am.
First, I couldn't run this from PowerShell--couldn't seem to find ffmpeg, even after adding to Path variable. This could just be my limited PowerShell knowledge, but I did get this to run in cmd. Maybe I have to restart PS for it to accept the updated Path variable. Also as a note on on the functionality of "where", when I downloaded jq, it was named jq-win64.exe by default, but your script didn't find it until I renamed it jq.exe. I'd suggest adding a comment that it might need to be renamed. I just guessed that might be what I needed to do.
Second, and my primary reason for wanting to comment, the script worked fine for a file with no spaces in the filename, but when I ran it on files with spaces, it gave me the error "[first word after space] was unexpected at this time." The filename was in double quotes when I ran the command, so I was surprised by this error. Have you encountered this? I don't know enough to fix this in the script for myself, so I'll just have to remove all space from the filenames.
Finally, would it be possible to add an option to bypass checking the registry key for a device and just input the activation_bytes manually? I think I can figure out how to comment all this out and just assign the activation bytes in the script. Not that this is a required function, I only noticed because the initial file I tested the script on was a book my mom had shared with me, and it wasn't from my audible account, so the activation_bytes were different. Additionally, I had already gone through the process of digging up my activation_bytes before I realized this script did it for me.
Thanks for the useful script.