sandreas / m4b-tool

m4b-tool is a command line utility to merge, split and chapterize audiobook files such as mp3, ogg, flac, m4a or m4b
MIT License
1.15k stars 76 forks source link

Corrupt files when merging many mp3 files #239

Open bbtufty opened 1 year ago

bbtufty commented 1 year ago

Unsure if this is an m4b-tool issue or something else, but I'm currently trying to merge the mp3s from a graphic audio novel into a single m4b. The total filesize is 2.2GB, and the file when I use the audio spec in the mp3s (2 channels, 256k bitrate, 44100kHz sampling) leads to an unopenable file. There are no errors throughout the run, the file produces successfully. Turning down e.g. bitrate or the number of files merged works, so it's not an issue with the files themselves. Are there any recommendations here?

sandreas commented 1 year ago

Unsure if this is an m4b-tool issue or something else, but I'm currently trying to merge the mp3s from a graphic audio novel into a single m4b. The total filesize is 2.2GB, and the file when I use the audio spec in the mp3s (2 channels, 256k bitrate, 44100kHz sampling) leads to an unopenable file. There are no errors throughout the run, the file produces successfully. Turning down e.g. bitrate or the number of files merged works, so it's not an issue with the files themselves. Are there any recommendations here?

I had this in a few cases. The ONLY solution I could find was to use fdkaac instead of ffmpeg for the encoding step (I would assume that it has something to do with ffmpeg). It's slower, but slightly better quality and it worked in every of my edge cases. You need fdkaac installed though, if you are using docker it is already packed in.

--audio-profile=aac_he 
bbtufty commented 1 year ago

I am using the docker, so can try this. I thought the aac_he profile is for low bitrates, so won't preserve the high quality I want to keep on encoding?

sandreas commented 1 year ago

I am using the docker, so can try this. I thought the aac_he profile is for low bitrates, so won't preserve the high quality I want to keep on encoding?

The selected --profile may be optimized for low bitrates, but you can still specify --audio-bitrate etc, so the output quality is more or less up to you. My experience is, that fdkaac is slightly higher quality on the same bitrate than ffmpeg, even if you use fdk_aac, but to be honest: A lower quality file still would be better than an unopenable file ;-)

For a list of profiles, see https://github.com/sandreas/m4b-tool/blob/ccfb548cda1bfc5f764771ca10d11ba153488476/src/library/Executables/Fdkaac.php#L25

While you are absolutely correct, that this bug is very annoying, my investigation did not have any results besides ffmpeg may have an issue, but I pretty much gave up doing research because there is a workaround and reporting issues to the ffmpeg project, because they've got lots of work to do and the response times are pretty high.

What you could try: Is a similar bug is happening using https://github.com/yermak/AudioBookConverter, another great project for audio book conversion. They internally are also using ffmpeg, so if ffmpeg is the problem, the result should be similar. I would really appreciate feedback on this for my investigation.

bbtufty commented 1 year ago

You're right, the fact the filesize was ~1/2 of the input mp3s was throwing me but everything seems to be working as expected. I'll try with audiobookconverter when I can and report back

bbtufty commented 1 year ago

Interesting, AudiobookConverter does work (running on Windows). Here's the afinfo output for the final file:

File type ID:   m4af
Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, aac  (0x00000000) 0 bits/channel, 0 bytes/packet, 1024 frames/packet, 0 bytes/frame
                no channel layout.
estimated duration: 70179.810000 sec
audio bytes: 2263607124
audio packets: 3022509
bit rate: 258025 bits per second
packet size upper bound: 1536
maximum packet size: 1536
audio data file offset: 13355075
optimized
audio 3094929621 valid frames + 0 priming + 119595 remainder = 3095049216
format list:
[ 0] format:      2 ch,  44100 Hz, aac  (0x00000000) 0 bits/channel, 0 bytes/packet, 1024 frames/packet, 0 bytes/frame
Channel layout: Stereo (L R)
Loudness Info:
    media kind                       : "Audiobook"

sound check volume normalization gain: 0.00 dB

and the first input file:

File:           101 - Opening Credits.mp3
File type ID:   MPG3
Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, .mp3 (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame
                no channel layout.
estimated duration: 99.004082 sec
audio bytes: 3168130
audio packets: 3790
bit rate: 255999 bits per second
packet size upper bound: 1052
maximum packet size: 836
audio data file offset: 91567
optimized
----

The filesize is also slightly larger than using m4b-tool, so it's apparently not a filesize thing. Hopefully that helps, happy to do more tests on my end if you think that would be helpful!