Updated version of patch by John Stebbins [PATCH] avformat/matroskaenc: return error if aac extradata changes extradata changing would result in an invalid stream. also, as the code was written, rewriting extradata was corrupting the track header resulting in an invalid file. --- ffmpeg/libavformat/matroskaenc.c.orig 2020-06-18 01:03:40.000000000 -0400 +++ ffmpeg/libavformat/matroskaenc.c 2020-06-18 01:30:46.000000000 -0400 @@ -2193,6 +2193,16 @@ case AV_CODEC_ID_AAC: if (side_data_size && mkv->track.bc) { int filler, output_sample_rate = 0; + if (par->extradata && par->extradata_size) { + if (par->extradata_size != side_data_size || + memcmp(par->extradata, side_data, side_data_size)) { + av_log(s, AV_LOG_ERROR, "Error, AAC extradata changed mid-stream.\n"); + return AVERROR(EINVAL); + } else { + // Already written + break; + } + } ret = get_aac_sample_rates(s, mkv, side_data, side_data_size, &track->sample_rate, &output_sample_rate); if (ret < 0)