Skip to content

Add transform_mp4_cut to build the upload of a part of a video - #985

Merged
caglarpir merged 1 commit into
mapillary:mainfrom
caglarpir:cut-video-at-sync-samples
Dec 1, 2026
Merged

caglarpir merged 1 commit into
mapillary:mainfrom
caglarpir:cut-video-at-sync-samples

Conversation

@caglarpir

@caglarpir caglarpir commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

transform_mp4_cut() builds what transform_mp4() builds, for a part of a video. It is for uploading a video whose GPS track has gaps as one sequence per part (#988).

  • It keeps the samples from the last sync sample presented by the start of the part to the first sync sample from which every frame is presented after its end. The leading frames of an open GOP are presented before their sync sample, so the cut runs on past a sync sample whose leading frames are presented by the end. Every frame of the part decodes, nothing is re-encoded, and the source is read for those samples only.
  • It leaves out the leading frames of an open GOP at the start of the cut, which reference the GOP before it.
  • Other video tracks, such as the second lens of a 360 camera, are cut at the same time, or MP4CutError is raised. Each ends at a sync sample of its own, and MP4Cut.end is the end of the one that runs the longest.
  • The part starts at time 0. The sample generator is made for the decoding time of the start of the cut, which is the time of the frames in the new video, so the CAMM samples it adds are timed on them.
  • The duration of each track and its single edit span the frames it presents. The creation time of the new video is moved to the start of the cut, in whole seconds.
  • MP4CutError is raised for a part where a video track presents no frame, or with no time (NaN).
  • MP4Header holds the ftyp and moov boxes, read once for all the parts of a video.

Checked on H.264 with B-frames, HEVC with negative composition offsets, open-GOP HEVC and H.264, all-intra and single-GOP H.264, stream-copy cuts, two video tracks, a 261 s phone video and a 3.2 GB dual-lens 360 video:

  • Every frame of each piece decodes bit-exact against the source frames, and the frames presented are those whose composition times are in the part. With open-GOP H.264, a piece that starts at a non-IDR I frame makes ffmpeg log mmco: unref short failure, but its frames still decode bit-exact.
  • Each CAMM point lands on the frame it is on in the source, and locating the frames along each piece's CAMM track puts none outside its part.
  • Building a piece takes 0.01-0.14 s, and the pieces of a video read 0.5-0.76 of the file in total.

transform_mp4_cut() is transform_mp4() for a part of a video. It keeps
the samples from the last sync sample presented by the start of the part
to the first sync sample from which every frame is presented after its
end, so every frame of the part decodes, nothing is encoded again, and
the source is read for those samples only. The leading frames of an open
GOP are decoded after its sync sample but presented before it, so the
cut runs on past a sync sample whose leading frames are presented by the
end. The part starts at time 0 of the new video: the sample generator is
made for the decoding time of the start of the cut, which is the time of
the frames in the new video, so the CAMM samples it adds are timed on
them. The creation time of the new video is moved to the start of the
cut, in whole seconds.

In an open GOP, the frames decoded after the first sync sample but
presented before it reference the GOP before, which is not kept, so they
are left out. Other video tracks, such as the second lens of a 360
camera, are cut at the same time, or MP4CutError is raised; each ends at
a sync sample of its own. The duration of each track and its single edit
span the frames it presents, from the earliest to the end of the latest.
MP4CutError is also raised for a part where a video track presents no
frame, or with no time (NaN).

MP4Header holds the ftyp and moov boxes, read once for all the parts of
a video.

Nothing calls it yet: uploading a video in parts comes in a follow-up.
@caglarpir
caglarpir force-pushed the cut-video-at-sync-samples branch from a25dbb5 to 9ad2bc8 Compare December 1, 2026 15:32
@caglarpir
caglarpir merged commit 32eaffd into mapillary:main Dec 1, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant