Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrypt without ffmpeg #130

Open
snowskeleton opened this issue Nov 14, 2022 · 9 comments
Open

Decrypt without ffmpeg #130

snowskeleton opened this issue Nov 14, 2022 · 9 comments

Comments

@snowskeleton
Copy link
Contributor

I just recently completed this project to decrypt aax and aaxc files in (mostly) pure python, without ffmpeg. In terms of speed, it can decrypt a 400MB book in less than 1 minute. Is this functionality you're interested in integrating into audible-cli? I see there's already a plugin command to remove encrypting using ffmpeg, and I'm thinking it might be nice to remove ffmpeg as a dependency. It may even be possible to decrypt in sync with downloading, but that would take a lot more work and re-architecture.

@mkb79
Copy link
Owner

mkb79 commented Nov 14, 2022

Hi @snowskeleton. Very good work. I'll take a deeper look on your project. Do you have some speed comparison against ffmpeg or AAXClean?

@snowskeleton
Copy link
Contributor Author

snowcrypt is about 3 times slower ffmpeg in real time. Below are the times for my server decrypting a 424MB file.

snowcrypt: real 0m26.349s
ffmpeg: real 0m8.403s

snowcrypt is superior to ffmpeg in that all metadata is preserved. No chapters need updating or renaming.

I haven't tested AAXClean, so I can't compare against that. (Although I found it invaluable as a reference while writing snowcrypt. The AES key derivation took me several days of frustration.)

@snowskeleton
Copy link
Contributor Author

I've been thinking about this some more for the past few days. I'm going to make snowcrypt a drop-in replacement for ffmpeg, specifically for converting aax/aaxc. That way, you can easily implement a toggle for whether ffmpeg is installed, and fallback to snowcrypt if you need to without changing any other code.

On a different note, I've spent the past couple days poring over the decryption code (which I didn't write, so I have to learn it). There are a few places I think I could make it faster, so I'm gonna play with that a bit too.

On another other note, if nothing else, you could steal the key derivation so you can use the same methods for decrypting aax vs aaxc.

@mkb79
Copy link
Owner

mkb79 commented Nov 17, 2022

On another other note, if nothing else, you could steal the key derivation so you can use the same methods for decrypting aax vs aaxc.

What do you mean with these?

@snowskeleton
Copy link
Contributor Author

My suggestion is that, if you want to keep using ffmpeg, you can get the key/iv from either the voucher or from snowcrypt. Then, instead of having two methods in this file

def decrypt_aax(...):
...
def decrypt_aaxc(...):

you could just have the one function for both files types.

Here's an example.

After importing

from snowcrypt import snowcrypt

this code block

snowcrypt.decrypt_aax(
  'input_file.aax', 
  'output_file.m4a',
  'activation_bytes')

is equivalent to

key, iv = snowcrypt.deriveKeyIV('input_file.aax', 'activation_bytes')
snowcrypt.decrypt_aaxc(
  'input_file.aax', 
  'output_file.m4a',
  key,
  iv)

I hope I was able to explain it a bit better that time.

@mkb79
Copy link
Owner

mkb79 commented Jan 16, 2024

@snowskeleton
Sorry for my late reply. I'll take a look at your project now. Can you tell me how long it takes to compute the key/iv from the activation bytes?

@snowskeleton
Copy link
Contributor Author

Basically no time at all. It takes longer to launch the python interpreter than to actually do the computation.

@mkb79
Copy link
Owner

mkb79 commented Jan 16, 2024

I'm thinking about adding a fallback to your Python implementation if ffmpeg is not installed on the target system. I'll have to compare the time difference between your package and ffmpeg. I'll see you use the threading module. Maybe this speed up some things.

@snowskeleton
Copy link
Contributor Author

I experimented with multithreading for decrypting the same book faster, but ended up only being able to decrypt multiple books at once.

In terms of speed compared to ffmpeg, snowcrypt is about half as fast. In exchange for this slowdown, you get much simpler code (you don't have to pass a bunch of flags to FFMPEG) and easier dependencies (you don't have to make sure the right version of FFMPEG is installed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants