FFMPEG - Malformed .aac tracks sound PERFECT in Movie Studio, but return "Input buffer exhausted before END element found" error when attempting mux

I am a novice ffmpeg user trying to mux h.264 video with a secondary .wav commentary track, without re-encoding anything on the video side. The video's audio is in .aac.

I use Bandicam to record footage; there is a setting to record a separate .wav file (the commentary) simultaneously.

When I initially tried to figure this out, I opened a thread (link at the bottom) and was quite thrilled with the helpfulness of the user that responded. The answer seemed to satisfy my question completely. Now, however, I have a new problem and I'd thus like to ask about it openly.

Some recordings are returning the error I mentioned in the header when I try to mux the commentary back onto the video. "Input buffer exhausted before END element found" is the error. The files finish muxing, but if the error happens enough times, noticeable video/audio desync occurs in the video. I updated my FFMPEG executables to the latest builds, and the previous helpful soul pointed out I might have "malformed audio streams."

But again, (1) it's not all of them, only some, and (2) both audio streams (original recording audio and commentary) sound flawless when I load the .mp4 into Movie Studio. So I'm tempted to say ffmpeg/avanti is the culprit.

Regardless, I have no idea what the solution could be. Please help if you have any idea.

Here's the original thread, for your reference.


Solution 1:

AAC audio is composed of frames. Each frame contains various types of elements. The last element in each frame is the END element. The error message you receive indicates that FFmpeg has surveyed the frame but hasn't found the END element. So, it skips that frame during remux, and that eventually breaks sync if too many of those occur. So here are a few attempts to work around that.

#1 Preserve original timestamps

ffmpeg -copyts -i video.mp4 -i audio.wav -map 0 -map 1:a -c:v copy -c:a:0 copy -c:a:1 aac video-new.mp4

#2 Ignore stream errors and preserve original timestamps

ffmpeg -copyts -err_detect ignore_err -i video.mp4 -i audio.wav -map 0 -map 1:a -c:v copy -c:a:0 copy -c:a:1 aac video-new.mp4

#3 Encode video's audio but preserve gaps due to corrupted frames

ffmpeg -i video.mp4 -i audio.wav -filter_complex "[0:a]aresample=async=1:min_comp=0.02[a]" -map 0:v -map "[a]" -map 1:a -c:v copy -c:a:0 aac -c:a:1 aac video-new.mp4