2

So for various reasons I'm converting an MP3 to a FLAC with the following:

ffmpeg -i x.mp3 -map 0 -map -0:v -compression_level 5 x.flac

x.mp3 is 21MB and the resultant x.flac is 102MB

I assume, possibly incorrectly, that FFMPEG is creating a PCM stream from the MP3 and then coding, compressing and containerising the stream into a FLAC file.

The input file bitrate is 320kb/s and the output file bitrate is 1558kb/s.

Can someone please elucidate why the compression algorithm in FLAC doesn't do a better job with the PCM (or whatever it is) that is fed to it after the MP3 is decoded? Or is my command line wrong?

Another test I ran goes like:

File input.flac is 24MB

ffmpeg -i input.flac output.mp3

File output.mp3 is 3.5MB

ffmpeg -I output.mp3 output2.flac

File output2.flac is 41MB

Thanks

  • 2
    "for various reasons" - I'd love to know what they are, because I can see no sense whatsoever in mp3 to flac conversion. All you can ever gain is file-size. – Tetsujin Jun 5 at 12:42
  • 2
    @Tetsujin I'm loathe to answer your question lest I don't get an answer to my OP but here you go (and let be the end of it). I have old CD rips in MP3. I am re-ripping the same CDs in FLAC. I have an album with a single track that will no longer read (perhaps a scratch). I want to supplement the missing track with the old MP3 rip (hence the conversion to FLAC). I store lossless and lossy formats in different directory hierarchies. – Scott Jun 5 at 13:06
  • @Scott Not a direct answer, but FLAC compression levels in FFmpeg appear to be 0-12, with 5 as default. – Anaksunaman Jun 5 at 13:21
  • 1
    Scott - then that makes perfect sense to me, thanks. I didn't want you to be wasting your time for no gain, was all :) – Tetsujin Jun 5 at 13:31
  • 1
    Are you encoding flac as 24 bits-per-sample? – user1184674 Jun 6 at 3:29
3

First off, I suspect you are misunderstand the purposes of the different codecs. the job of FLAC is not better "sounding" audio (although it may sound a little better), its for archival audio. with FLAC, you can decode and reencode the file over and over without loosing any quality ever. With mp3, If you decode it and reencode it a few dozen times the final result would be unrecognizable. Encoding mp3 to FLAC is pointless because it will only take more space and sounds exactly the same as the mp3.

But to answer why: The full answer is vey complex, and requires understanding of information theory. But I will try to summaries it.

The short answer: MP3 is lossy, and flac is lossless, meaning mp3 has the option of removing or inserting information in the data, making the compression more efficient. FLAC can not do that.

The long answer: Let use English as a compression algorithm. follow these directions:

"Write 1000000 zeros to text file".

If you do that, the final file will be about 1Mb. But using english, I was able to describe (compress) it to just one short sentence that if written to a file would take only 32 bytes. Not let's change the sentence a bit.

"Write 500000 zeros to a text file, then write one 1, then write 499999 more zeros".

Now this sentence is MUCH longer, (82 bytes) and produces a file that is nearly identical, except for one value in the middle. That one random value in the middle made our sentence (compressed data) more than twice as large. You see, the more predictable something is, the better it compresses. How "compressible" a piece of data is determined by its "information entropy" or randomness. The higher the entropy, the worse it compresses. The limits of compression are known and can be calculated using information theory.

MP3 is lossy. Meaning it can look at that seconds sentence, and decide that the extra '1' in the middle of the files will never be noticed, and change it to a zero, Thus reducing its entropy, thus improving compression. The 1 is now lost forever though, and can never be recovered. This process is called 'quantization' an is one of several reason mp3 can achieve its compression ratios

But mp3 also does the opposite too. Due to mp3 conversion of data to the frequency domain, along with psychoacoustic optimizations, it can actually increase the entropy of the PCM data when decoding. If you then take the PCM and encode it with FLAC, FLAC will persevere the added entropy.

Flac uses a totally different compression technique to ensure entropy is preserved and not quantized out. This means flac needs more space to encode files. The fact that FLAC does not quantize is the reason for its existence in the first place and is its primary feature.

| improve this answer | |
1

By default, recent versions of ffmpeg decode mp3 to a floating point format; flac encodes linear PCM. In order to encode floating point as flac, ffmpeg must first convert the floating point format to an integer format. It chooses signed 32 bit (which results in an unnecessarily large file). There are two ways of getting a 16 bit output:

a) Use a decoder that outputs 16 bit:

ffmpeg -c:a mp3 -i x.mp3 x.flac

b) Explicitly convert to 16 bit (can also be done via aformat filter):

ffmpeg -i x.mp3 -sample_fmt s16 x.flac

Note: This won't increase the quality of the mp3 track - it would make more sense to just use the mp3 until you get a chance to re-rip the missing track.


ffmpeg's flac encoder supports the sample formats s16 and s32. There are two mp3 decoders - one outputs a floating point format, the other outputs a 16 bit integer format:

$ ffmpeg -h encoder=flac
    Supported sample formats: s16 s32

$ ffmpeg -codecs | grep -Fi mp3
 DEA.L. mp3    MP3 (MPEG audio layer 3) (decoders: mp3float mp3 ) (encoders: libmp3lame )

$ ffmpeg -h decoder=mp3float
    Supported sample formats: fltp flt

$ ffmpeg -h decoder=mp3
    Supported sample formats: s16p s16
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.