[FFmpeg-user] How to reassemble raw packet data back into a new MP4 file?

Vincent Deconinck vdeconinck at gmail.com
Sun Sep 22 15:34:03 EEST 2024


> I have tried the following commands
>
> ffmpeg -err_detect ignore_err -i old.mp4 -c copy new.mp4
> ...
> [mov,mp4,m4a,3gp,3g2,mj2 @ 00158b00] stream 1, contradictionary STSC and
> STCO
> [mov,mp4,m4a,3gp,3g2,mj2 @ 00158b00] error reading header


Hmm, I see.

Maybe one last "cheap" attempt: if you are sure the issue is at the end,
did you try the -t option? There's a slight chance you might recover most
of the video...

Now if that doesn't work, I'd rather concentrate on the root cause notified
by ffmpeg: "contradictionary STSC and STCO".

You might want to analyze that issue further (using either open-source
software or trial of non-free products such as
https://www.jongbel.com/manual-analysis/atombox-studio/ ) and then fix the
issue by hand.
- STSC is the sample-to-chunk table and can be used to find the chunk that
contains a given sample
- STCO is the chunk offset table and gives the index of each chunk into the
file.
Of course they must be consistent (e.g. STSC must reference chunks that
exist in STCO, STCO indices must be < file size, etc.).
Probably fixing those tables would make the file readable (even if
incomplete).

The Mpeg4 specification is licensed, but if you can get hold of it, the
section that explains the structure of the ISO base media file format
(ISO-BMFF) is "ISO_IEC_14496-12_2015.pdf" (or later).
Alternatively, chatGPT is surprisingly good at explaining the ISOBMFF
structure.

There are many libraries that can help you either decode/edit the file or
just understand its structure. For example:
C: https://wiki.gpac.io/MP4Box/MP4Box
Javascript: https://github.com/gpac/mp4box.js
Rust: https://github.com/mozilla/mp4parse-rust
Python: https://github.com/essential61/mp4analyser
Java: https://github.com/sannies/mp4parser
and many others.

I personally used the last one because I have a Java background. It's not
perfect but allows you to read, modify and write mp4 files. It might crash
on inconsistencies in the file, but you are always free to change the code
and handle these cases if needed.

Hope it helps

Vincent
.


More information about the ffmpeg-user mailing list