# 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.

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

• You're a one-man army. I'll fiddle and get back to you on this thread. Thank you again. – Hadrian Sep 12 '16 at 5:56
• I have another try encoding right now. Here's the command line I'm using per our last talk, plus the suggested changes from #2. See anything that needs fixing? -copyts -err_detect ignore_err -filter_complex "[0:a][1]amix[a]" -map 0:v -map "[a]" -c:v copy -c:a:0 aac -c:a:1 aac – Hadrian Sep 12 '16 at 6:14
• Forgot you were using that command. Just used the one in my answer. Use ffmpeg -i video.mp4 -i audio.wav -filter_complex "[0:a]aresample=async=1:min_comp=0.02[sync];[sync][1]amix[a]" -map 0:v -map "[a]" -c:v copy -c:a aac video-new.mp4 – Gyan Sep 12 '16 at 6:20
• ...that...appears to have fixed it. Bravo!! So, should I save this code for troublemaker files, or can I use it across the board and it'll simply apply the fix when there are corrupted frames? – Hadrian Sep 12 '16 at 6:30
• You can use it across the board. – Gyan Sep 12 '16 at 6:32