Opened 6 months ago

Last modified 5 months ago

#11030 new defect

Remuxing a damaged MP4 increases file size noticeably

Reported by: PumaD Owned by:
Priority: normal Component: avcodec
Version: git-master Keywords: aac
Cc: PumaD, MasterQuestionable Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Got a slightly damaged 1.9 GiB MP4 file. When remuxing with ffmpeg the resulting file is a whole 224 MiB larger than before (from 1.9 GiB to 2.2 GiB).

Remuxing the file with mkvtoolnix instead reduces the file size marginally.
(​​https://gitlab.com/mbunkus/mkvtoolnix; mkvmerge --output out.mkv slightly-damaged-audio-stops-at-00-43-05.mp4)

The effect on the sample below is a much more modest increase of ~30 MiB, but it does illustrate the problem.

Sample (from #11028):
https://streams.videolan.org/ffmpeg/incoming/11028/slightly-damaged-audio-stops-at-00-43-05.mp4 (~ 1 GiB)

Note: NSFW

How to reproduce:

$ ffmpeg -i slightly-damaged-audio-stops-at-00-43-05.mp4 -c copy out.mkv
ffmpeg version N-115363-g3146b77a7d Copyright (c) 2000-2024 the FFmpeg developers
  built with gcc 14.1.1 (GCC) 20240507
  configuration: 
  libavutil      59. 19.100 / 59. 19.100
  libavcodec     61.  5.104 / 61.  5.104
  libavformat    61.  3.103 / 61.  3.103
  libavdevice    61.  2.100 / 61.  2.100
  libavfilter    10.  2.102 / 10.  2.102
  libswscale      8.  2.100 /  8.  2.100
  libswresample   5.  2.100 /  5.  2.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x624f2ebf8840] Sample size 3043950162 is too large
[h264 @ 0x624f2ebf9800] concealing 1691 DC, 1691 AC, 1691 MV errors in I frame
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'slightly-damaged-audio-stops-at-00-43-05.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.62.100
  Duration: 01:28:36.03, start: 0.000000, bitrate: 1577 kb/s
  Stream #0:0[0x1](und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 960x720 [SAR 1:1 DAR 4:3], 3028 kb/s, 29.93 fps, 30 tbr, 90k tbn (default)
      Metadata:
        handler_name    : VideoHandler
        vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 93 kb/s (default)
      Metadata:
        handler_name    : SoundHandler
        vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Output #0, matroska, to 'out.mkv':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf61.3.103
  Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 960x720 [SAR 1:1 DAR 4:3], q=2-31, 3028 kb/s, 29.93 fps, 30 tbr, 1k tbn (default)
      Metadata:
        handler_name    : VideoHandler
        vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) ([255][0][0][0] / 0x00FF), 44100 Hz, stereo, fltp, 93 kb/s (default)
      Metadata:
        handler_name    : SoundHandler
        vendor_id       : [0][0][0][0]
Press [q] to stop, [?] for help
[h264 @ 0x624f2ef99e00] Invalid NAL unit size (-720787532 > 47705).
[h264 @ 0x624f2ef99e00] missing picture in access unit with size 47709
[h264 @ 0x624f2ef99e00] Invalid NAL unit size (1049218 > 11473).31e+03x    
[h264 @ 0x624f2ef99e00] missing picture in access unit with size 11477
[h264 @ 0x624f2ef99e00] Invalid NAL unit size (722808771 > 10603).
[h264 @ 0x624f2ef99e00] missing picture in access unit with size 10607
[h264 @ 0x624f2ef99e00] Invalid NAL unit size (526767940 > 14657).+03x    
[h264 @ 0x624f2ef99e00] missing picture in access unit with size 14661
[h264 @ 0x624f2ef99e00] Invalid NAL unit size (1063190835 > 51).
[h264 @ 0x624f2ef99e00] missing picture in access unit with size 6615
Truncating packet of size 268435713 to 355012330kbits/s speed=1.48e+03x    
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x624f2ebf8840] Packet corrupt (stream = 1, dts = 113707010).
[in#0/mov,mp4,m4a,3gp,3g2,mj2 @ 0x624f2ebf8580] corrupt input packet in stream 1
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x624f2ebf8840] Packet corrupt (stream = 0, dts = 240368850).
[h264 @ 0x624f2ef99e00] Invalid NAL unit size (15412 > 629).
[h264 @ 0x624f2ef99e00] missing picture in access unit with size 639
[in#0/mov,mp4,m4a,3gp,3g2,mj2 @ 0x624f2ebf8580] corrupt input packet in stream 0
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x624f2ebf8840] stream 0, offset 0x3e803ab9: partial file
[out#0/matroska @ 0x624f2ebfabc0] video:988025KiB audio:64471KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 0.138273%
size= 1053952KiB time=00:44:30.79 bitrate=3232.7kbits/s speed=1.45e+03x

Attachments (1)

audio-only.mka.txt (3.8 KB ) - added by MasterQuestionable 5 months ago.
͏    Filtered from: https://pastebin.com/raw/5XjGHj5z

Download all attachments as: .zip

Change History (19)

comment:1 by MasterQuestionable, 6 months ago

Cc: MasterQuestionable added
Component: undeterminedavformat

͏    Try "-map_metadata -1 -map_chapters -1"?

in reply to:  1 comment:2 by PumaD, 6 months ago

Replying to MasterQuestionable:

͏    Try "-map_metadata -1 -map_chapters -1"?

That only saves 155 bytes. No change in the terminal output either. The full command I tested was:
ffmpeg -i slightly-damaged-audio-stops-at-00-43-05.mp4 -c copy -map_metadata -1 -map_chapters -1 out.mkv

comment:3 by MasterQuestionable, 6 months ago

͏    Try also "-ignore_editlist"?
͏    https://trac.ffmpeg.org/ticket/11027#comment:4

comment:4 by PumaD, 6 months ago

No effect whatsoever. (For future readers: It needs to be used like -ignore_editlist true)

comment:5 by MasterQuestionable, 6 months ago

Component: avformatavcodec
Keywords: h264 added

͏    "-ignore_editlist 1" also OK.
͏    Caveat it's supposed to be used as input option (before ͏"-i").
͏    (using which as output doesn't cause failure... and seem no-op)

͏    Change the "Component" and "Keywords" back if:
͏    |*| You did it wrong.
͏    |*| ͏"-ignore_editlist" indeed doesn't help.

comment:6 by PumaD, 5 months ago

Yeah, of course I did it wrong but it still only saves 4 bytes (+ the 155 bytes from before).

I'm assuming you meant so say "indeed does help" and will leave Component and Keywords untouched.

comment:7 by MasterQuestionable, 5 months ago

͏    Last to make sure:
͏    Try remuxing with only audio/video for each.
͏    (using "-an" "-vn" respectively; or like [ https://trac.ffmpeg.org/ticket/11005#comment:18 ], but with ͏"-map")

comment:8 by PumaD, 5 months ago

It's the audio stream.

The audio-only file created by ffmpeg with:
ffmpeg -i slightly-damaged-audio-stops-at-00-43-05.mp4 -c copy -vn slightly-damaged-audio-stops-at-00-43-05-audio.mka

is 63.7 MiB. The audio-only file created by mkvtoolnix is just 30.4 MiB.
The video-only files are reasonably close at 965.5 MiB (ffmpeg) vs 965.3 MiB (mkvtoolnix).

Tested also on the full file where the audio-only files are 285.9 MiB (ffmpeg) vs 60.2 MiB (mkvtoolnix).

comment:9 by MasterQuestionable, 5 months ago

Keywords: aac added; h264 removed

͏    Note also:
͏    https://trac.ffmpeg.org/ticket/11005#comment:9

͏    What happens when further remuxing the problematic audio? (both FFmpeg, MKVToolNix)

comment:10 by PumaD, 5 months ago

full_video.mp4 to audio-only.mka => 285.9 MiB
full_video.mp4 to audio-only.mp4 => 285.6 MiB

audio-only.mp4 with ffmpeg to MKV => 285.9 MiB
audio-only.mp4 with ffmpeg to MP4 => 285.6 MiB

audio-only.mp4 with mkvtoolnix to MKV => 29.5 MiB

audio-only.mka with ffmpeg to MKV => 29.9 MiB
audio-only.mka with ffmpeg to MP4 => 29.6 MiB

audio-only.mka with mkvtoolnix to MKV => 285.5 MiB

Remuxing of audio-only.mka with ffmpeg shows this warning, which is not present when remuxing audio-only.mp4:
[matroska,webm @ 0x56bfff0f1840] Invalid length 0x10000105 > 0x10000000 for element with ID 0xA3 at 0x1dc7629
Truncating packet of size 104572254 to 49047102

That's likely related to #11028 as the audio for the full file should be ~60 MiB, not just ~30 MiB.

comment:11 by MasterQuestionable, 5 months ago

͏    Caveat implicit logic error in your statement:
͏    "audio-only.mka" definition overlap. (clean-up preferable)

͏    1 more test:
͏    Does zipping the problematic audios (separately for each) yield significant compression? (great size reduction)

in reply to:  11 comment:12 by PumaD, 5 months ago

Replying to MasterQuestionable:

Caveat implicit logic error in your statement:
͏"audio-only.mka" definition overlap. (clean-up preferable)

I don't see any error. Could you elaborate on what you think is wrong?

͏    1 more test:
͏    Does zipping the problematic audios (separately for each) yield significant compression? (great size reduction)

No, zipping them saves only a couple hundred KiB each. Nothing that would suggest large blocks of identical bytes.

comment:13 by MasterQuestionable, 5 months ago

[[
͏    "audio-only.mka" with FFmpeg to MKV => 29.9 MiB
͏    ...

͏    "audio-only.mka" with MKVToolNix to MKV => 285.5 MiB
]]
͏    How?

comment:14 by PumaD, 5 months ago

Answering the "how" is beyond my pay grade, but it's definitely what I'm seeing and not just a typo.

comment:15 by MasterQuestionable, 5 months ago

͏    I believe the 1st "audio-only.mka" is that implicitly derived from:
͏    "audio-only.mp4" with MKVToolNix to MKV => 29.5 MiB

͏    While the 2nd is:
͏    "full_video.mp4" to "audio-only.mka" => 285.9 MiB [ With FFmpeg. ]

comment:16 by PumaD, 5 months ago

No, I've double checked that they've been generated from the same audio-only.mka. Here's the output of the full process: https://pastebin.com/5XjGHj5z

comment:17 by MasterQuestionable, 5 months ago

͏    It appears remuxing twice to Matroska (with FFmpeg) causes the truncation in the 2nd run.
͏    What happens when "-c copy" twice with MP4?

comment:18 by PumaD, 5 months ago

That would be this line from the table above:
audio-only.mp4 with ffmpeg to MP4 => 285.6 MiB

by MasterQuestionable, 5 months ago

Attachment: audio-only.mka.txt added

͏    Filtered from: https://pastebin.com/raw/5XjGHj5z

Note: See TracTickets for help on using tickets.