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

Preserving quality? #2

Open
John-Gee opened this issue Feb 24, 2025 · 8 comments
Open

Preserving quality? #2

John-Gee opened this issue Feb 24, 2025 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@John-Gee
Copy link

Hello,

I just found your script and I got interested so I dug a bit.

You say it preserves quality but that doesn't seem to be the case since you are re-encoding from MP3 to AAC, which are both lossy. You keep the bitrate but since it's a different codec I am not sure how meaningful that is, wouldn't it be cleaner to just keep the original audio codec as is and just switch the container?

If you do want to re-encode, it could be interesting to offer the choice of the encoder. For instance for AAC libfdk_aac is a better choice than the regular aac, though not always installed. Others may wish to use something like opus to drop bitrate and save on size while not dropping quality too much.

Thank you!

@cerinawithasea
Copy link

cerinawithasea commented Feb 25, 2025

Yeah i think. when i said preserve quality is that its set to use the same bitrate thats downloaded.
I think its actually a really good idea to give choices and i was thinking about at least offering the Mp3s with no convert
Also Feel free
Take it manipulate it make it for you, post it and share it. :)
I did this out of necessity and thought other ppl could use it. I dont know how much time i want to invest untill the libs are done shaking stuff up. The Script just stopped working for me, im sure the dev will do what needs to be done (please please) but until they relax I don't know how fancy it can get
what do you think?

original audio codec as is and just switch the container? I am very very new at this and i would love for you to explain this to me like im 5 ...or 55 :)

@John-Gee
Copy link
Author

John-Gee commented Feb 26, 2025

Alright I'll take another look at the code when I have more time.

I am very very new at this and i would love for you to explain this to me like im 5 ...or 55 :)

So the codec is what encodes or decodes the stream, in our case the audio stream. The container as the name implies contains various streams and maybe extra data. When you use ffmpeg you can re-encode the streams, switch the container, or both. In our case the mp3 file is pretty much the MP3 audio stream with id3 tags you can't have chapters and that's why you switched to using m4b (which is just an extension for audiobooks but really it's an MP4 container). To switch container is lossless (assuming the new container can do all the previous did, which is the case here) but to switch between lossy audio codecs isn't lossless. A lossy compression isn't like a lossless one, with lossy compression you will never be able to get the exact original data just an approximate, how close the approximate will be depends on the efficiency of the compressor you used (AAC is more efficient than MP3) and how much data you allow for the output file (the bitrate).

With your current script this is what happens:

  • Original audio was converted to mp3, incurring quality loss (hearable or not).
  • ffmpeg decodes that to raw audio which is an approximate of the original
  • ffmpeg then encodes that approximation to AAC incurring another quality loss (hearable or not)
  • you now have a 2nd approximation of the original audio

I haven't seen that test for audio so not sure how it'd be, but if you re-encode an image 100+ times with JPEG, your new 100+ pictures will look very similar to the ones right before them, but the last one will look hardly like the original (imagine going from red to orange in super small steps, you'd hardly notice the difference between each step but you'll easy tell between red and orange). The idea is lossy compression is an acceptable compromise, but as a rule try to not lossy compress something that was already lossy compressed if you can.

Wow that was a lot, I hope you're still here!

Actually let's add this too:

MP3 is deemed transparent at 192+kb/s, AAC at 128+kb/s and OPUS at 96+kb/s, transparent meaning a human ear will have trouble differentiating the compressed audio from the original one, but that depends on the equipment used to listen and the actual human, hence the + above. ;)

@cerinawithasea
Copy link

cerinawithasea commented Mar 9, 2025

In our case the mp3 file is pretty much the MP3 audio stream with id3 tags you can't have chapters and that's why you switched to using m4b (which is just an extension for audiobooks but really it's an MP4 container). To switch container is lossless (assuming the new container can do all the previous did, which is the case here) but to switch between lossy audio codecs isn't lossless. A lossy compression isn't like a lossless one, with lossy compression you will never be able to get the exact original data just an approximate, how close the approximate will be depends on the efficiency of the compressor you used (AAC is more efficient than MP3) and how much data you allow for the output file (the bitrate).

This part ive always understood in my head i see clear Tupperware containers inside each other, each convert your sticking it in another (so now the original audio is 3 Tupperware's deep and the clear is translucent at best)

But what to do when you are me and want m4b? Isnt thia the best option?

but as a rule try to not to lossy compress something that was already lossy compressed if you can.

This is the part i cant seem to wrap my head around

Sorry My windows laptop croaked ive been in panic mode (evenn tho i have 2 macs ...just no windows 'Till Tuesday (i love that band)
I truly appreciate you taking the time to explain all that for mw

@cerinawithasea cerinawithasea added the enhancement New feature or request label Mar 9, 2025
@John-Gee
Copy link
Author

John-Gee commented Mar 10, 2025

But what to do when you are me and want m4b? Isnt thia the best option?

Sorry I don't understand what's your question.

This is the part i cant seem to wrap my head around

Lossy compression is lossy, you don't retain all the original data. Say you create an MP3 out of a WAV, you're going to lose some data, then you re-compress that into an AAC stream you'll suffer more data loss, and so on. If you do it enough times you'll just have unintelligible garbage at the end.

You may think that an audio stream contains or doesn't contain all the words, but it's more complicated that that, we're talking about the clarity/quality of the audio signal.

Let's try another example: say you're looking at a painting, you hire someone to copy it but they're no perfect artist so the new painting is about 2% different, then again you make a copy of the copy with another ~2% difference, and so on, there will be a point where it will be very clear that what you're looking at isn't the original painting, depending on the original art, its size, your vision and brain health, etc. maybe it'll be when the copy is 5% different, maybe 20% different...

With lossy compression your hope is to keep as high quality you can for as small a size you can. AI can try to bring back some of the lost quality but it'll be fake as it has no actual idea of what was lost, it'll just imagine what it could have been.

Note: as it is with generic compression (ex: zip), audio compression doesn't need to be lossy it can be lossless, but that'll take more space. For example you can use FLAC to compress a WAV and then revert it back to the exact original WAV. Let's say an audio album in WAV would take ~600Mb, the same could take 300Mb in FLAC with 0 loss, 180Mb in MP3 or 120Mb in AAC both with some actual loss but likely not perceivable.

@cerinawithasea cerinawithasea removed their assignment Mar 10, 2025
@cerinawithasea1
Copy link
Owner

cerinawithasea1 commented Mar 10, 2025

HI Its Me , I fked my account somehow. If something can get screwed up i will figure out how to do it spectacularly!
what I am getting from all of this is.....
The best thing to do is extract the chapter as Mp3s and leave it at that. So everybody has the option to do as they wish

@cerinawithasea1
Copy link
Owner

MY account says ghost, i am so afraid rn

@John-Gee
Copy link
Author

ghost usually means the account is gone.

@cerinawithasea1
Copy link
Owner

Visual studio code said it needed a token< I clearly misunderstood the directions and made a business account.
I think i got this transfered into this account at least

@cerinawithasea1 cerinawithasea1 self-assigned this Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants