unsynchronized
unsynchronized

Reputation: 4938

Can you set an arbitrary byte offset in the mp4 header for the first frame?

TLDR: Can you set an arbitrary byte offset in the mp4 header for the first frame?

I have an idea for a utility program to work with mp4 files that have been recorded on a goPro, to assist in later time aligning of concurrently recorded files.

at the core of the idea is to take a large file (multiple gigabytes), and stream through it frame by frame from the start, until a frame with an embedded QR code is found, which would have been shot by pointing the camera at a smartphone or tablet, which is flicking through qr codes indicating a sort of timecode. this part of the project would be relatively straight forward, and simply be a matter of counting frames.

what i'd then like to be able to do is backup the mp4 file's header, and rewrite the header, telling it that the first x frames of the file is effectively unknown data, so the video content starts at frame x. (this way i can very quickly time align multiple files, so when they are loaded into an editor, they are all at the same timecode.

what i don't want to have to do is transcode the entire file, and ideally i'd like to be able to do it to files on a memory card.

my question: given a frame or byte offset that I want to call the "first frame", can i adjust the header somehow to nondestructively set the playback point for that file?

Upvotes: 0

Views: 1232

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69706

MP4 file has its internal structure and byte offset is not something you are interested in at all, since you typically cannot instruct players to play from specific byte offset. Instead, it is rather playback from specific time position.

In your case, I would consider the first step of identification of frame (time position from the beginning of the file) which has the code of interest. Note that this position is not necessarily playable from: H.264 encoding, typical for MP4 file, is temporal compression and playback might step back a few frames to start decoding/playback from. This holds a threat that your QR code is between neighboring key frames but still not on any of them, and you don't even have an option to make QR code the first playable frame without re-encoding the content (at least partially).

Knowing the position of QR code, the simplest is to simply keep it somewhere externally, to look up later and start playback (or, extract thumbnail) from. An alternate option is to re-multiplex data without re-encoding the content to trim everything before the position of interest (key frames thing from paragraph above applies here). Re-multiplexing with a proper tool is basically possible at the speed of file copy and does not cause quality loss (which might be a SuperUser question rather than StackOverflow if you are going to use existing pre-built tool such as FFmpeg).

Upvotes: 0

Related Questions