From 7f2ebb655e17635d0afb3bacb24eaf922b1437cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Verstraeten?= Date: Tue, 10 Feb 2026 15:56:10 +0000 Subject: [PATCH 1/6] Fix sidx.FirstOffset calculation and re-encode init segment for accurate MP4 structure --- machinery/src/video/mp4.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/machinery/src/video/mp4.go b/machinery/src/video/mp4.go index d179c1a..e7de42f 100644 --- a/machinery/src/video/mp4.go +++ b/machinery/src/video/mp4.go @@ -533,13 +533,38 @@ func (mp4 *MP4) Close(config *models.Config) { init.AddChild(sidx) } - // Encode the ftyp + moov init segment into a buffer so we know its size. + // Encode the ftyp + moov + sidx into a buffer to measure the total size. + // Then compute the correct sidx.FirstOffset (the gap between the end of + // the sidx box and the first moof, occupied by the trailing free box) + // and re-encode with the corrected value. var initBuf bytes.Buffer if err := init.Encode(&initBuf); err != nil { log.Log.Error("mp4.Close(): error encoding init segment: " + err.Error()) } initSize := int64(initBuf.Len()) + + // The sidx.FirstOffset is defined as the distance (in bytes) from the + // anchor point (first byte after the sidx box) to the first byte of + // the first referenced moof/mdat. Since sidx is the last box in init, + // the anchor point is at initSize, and the first moof is at FreeBoxSize. + if len(mp4.SegmentDurations) > 0 { + firstOffset := uint64(mp4.FreeBoxSize - initSize) + // Find the sidx we added and update its FirstOffset + for _, child := range init.Children { + if sidxBox, ok := child.(*mp4ff.SidxBox); ok { + sidxBox.FirstOffset = firstOffset + break + } + } + // Re-encode with the corrected FirstOffset (same size, no layout change) + initBuf.Reset() + if err := init.Encode(&initBuf); err != nil { + log.Log.Error("mp4.Close(): error re-encoding init segment: " + err.Error()) + } + initSize = int64(initBuf.Len()) + } + if initSize > mp4.FreeBoxSize { log.Log.Error(fmt.Sprintf("mp4.Close(): init segment (%d bytes) exceeds reserved space (%d bytes), file may be corrupt", initSize, mp4.FreeBoxSize)) } From 96df049e59239dc734fd40099f80d32eb9deb7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Verstraeten?= Date: Tue, 10 Feb 2026 15:59:59 +0000 Subject: [PATCH 2/6] Enhance MP4 initialization by adding max recording duration parameter, improving placeholder size calculation for segments. --- machinery/src/capture/main.go | 4 ++-- machinery/src/video/mp4.go | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/machinery/src/capture/main.go b/machinery/src/capture/main.go index 4315bfb..bdc591a 100644 --- a/machinery/src/capture/main.go +++ b/machinery/src/capture/main.go @@ -280,7 +280,7 @@ func HandleRecordStream(queue *packets.Queue, configDirectory string, configurat vpsNALUS := configuration.Config.Capture.IPCamera.VPSNALUs // Create a video file, and set the dimensions. - mp4Video = video.NewMP4(fullName, spsNALUS, ppsNALUS, vpsNALUS) + mp4Video = video.NewMP4(fullName, spsNALUS, ppsNALUS, vpsNALUS, configuration.Config.Capture.MaxLengthRecording) mp4Video.SetWidth(width) mp4Video.SetHeight(height) @@ -500,7 +500,7 @@ func HandleRecordStream(queue *packets.Queue, configDirectory string, configurat vpsNALUS := configuration.Config.Capture.IPCamera.VPSNALUs // Create a video file, and set the dimensions. - mp4Video := video.NewMP4(fullName, spsNALUS, ppsNALUS, vpsNALUS) + mp4Video := video.NewMP4(fullName, spsNALUS, ppsNALUS, vpsNALUS, configuration.Config.Capture.MaxLengthRecording) mp4Video.SetWidth(width) mp4Video.SetHeight(height) diff --git a/machinery/src/video/mp4.go b/machinery/src/video/mp4.go index e7de42f..fc23e23 100644 --- a/machinery/src/video/mp4.go +++ b/machinery/src/video/mp4.go @@ -67,14 +67,26 @@ type MP4 struct { SampleType string // Type of the sample (e.g., "video", "audio", "subtitle") } -// NewMP4 creates a new MP4 object -func NewMP4(fileName string, spsNALUs [][]byte, ppsNALUs [][]byte, vpsNALUs [][]byte) *MP4 { +// NewMP4 creates a new MP4 object. +// maxDurationSec is the maximum expected recording duration in seconds, +// used to calculate the free-box placeholder size for ftyp+moov+sidx. +func NewMP4(fileName string, spsNALUs [][]byte, ppsNALUs [][]byte, vpsNALUs [][]byte, maxDurationSec int64) *MP4 { - // Reserve space at the start of the file for the ftyp + moov boxes. - // We write a free box as a placeholder that will be overwritten at close - // with the actual init segment. 8KB is generous enough for ftyp + moov - // with video + audio tracks, mvex, and a UUID signature box. - freeBoxSize := 8192 + // Calculate the placeholder size needed at the start of the file. + // Components: + // ftyp: ~32 bytes + // moov: ~1500 bytes (mvhd + mvex + video trak + audio trak + UUID) + // sidx: 24 bytes fixed + 12 bytes per segment reference + // Segments are ~FragmentDurationMs each, so: + // numSegments = maxDurationSec * 1000 / FragmentDurationMs + // sidxSize = 24 + 12 * numSegments + baseSize := int64(2048) // ftyp + moov + headroom + numSegments := int64(0) + if maxDurationSec > 0 { + numSegments = (maxDurationSec*1000)/FragmentDurationMs + 1 + } + sidxSize := int64(24 + 12*numSegments) + freeBoxSize := int(baseSize + sidxSize) free := mp4ff.NewFreeBox(make([]byte, freeBoxSize)) // Create a writer From 0c460efea63a9c7954801f0a04c41cb20d2faf8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Verstraeten?= Date: Tue, 10 Feb 2026 16:17:10 +0000 Subject: [PATCH 3/6] Refactor PR description workflow to include organization variable and correct pull request URL format --- .github/workflows/pr-description.yaml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pr-description.yaml b/.github/workflows/pr-description.yaml index 984eaf4..3e4b398 100644 --- a/.github/workflows/pr-description.yaml +++ b/.github/workflows/pr-description.yaml @@ -3,8 +3,9 @@ name: Autofill PR description on: pull_request env: - PR_NUMBER: ${{ github.event.number }} + ORGANIZATION: uugai PROJECT: ${{ github.event.repository.name }} + PR_NUMBER: ${{ github.event.number }} jobs: openai-pr-description: @@ -20,5 +21,6 @@ jobs: azure_openai_api_key: ${{ secrets.AZURE_OPENAI_API_KEY }} azure_openai_endpoint: ${{ secrets.AZURE_OPENAI_ENDPOINT }} azure_openai_version: ${{ secrets.AZURE_OPENAI_VERSION }} - pull_request_url: https://pr${{ env.PR_NUMBER }}.app.kerberos.lol - overwrite_description: true \ No newline at end of file + openai_model: ${{ secrets.OPENAI_MODEL }} + pull_request_url: https://pr${{ env.PR_NUMBER }}.api.kerberos.lol + overwrite_description: true From edfa6ec4c619a57495de4b85bd05e90936b4a646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Verstraeten?= Date: Tue, 10 Feb 2026 17:20:16 +0100 Subject: [PATCH 4/6] Update machinery/src/video/mp4.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- machinery/src/video/mp4.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/machinery/src/video/mp4.go b/machinery/src/video/mp4.go index fc23e23..d5c49ce 100644 --- a/machinery/src/video/mp4.go +++ b/machinery/src/video/mp4.go @@ -78,12 +78,13 @@ func NewMP4(fileName string, spsNALUs [][]byte, ppsNALUs [][]byte, vpsNALUs [][] // moov: ~1500 bytes (mvhd + mvex + video trak + audio trak + UUID) // sidx: 24 bytes fixed + 12 bytes per segment reference // Segments are ~FragmentDurationMs each, so: - // numSegments = maxDurationSec * 1000 / FragmentDurationMs + // numSegments = ceil(maxDurationSec * 1000 / FragmentDurationMs) + 1 (safety margin) // sidxSize = 24 + 12 * numSegments baseSize := int64(2048) // ftyp + moov + headroom numSegments := int64(0) if maxDurationSec > 0 { - numSegments = (maxDurationSec*1000)/FragmentDurationMs + 1 + // Use integer ceiling division to avoid underestimating the number of segments. + numSegments = ((maxDurationSec*1000)+FragmentDurationMs-1)/FragmentDurationMs + 1 } sidxSize := int64(24 + 12*numSegments) freeBoxSize := int(baseSize + sidxSize) From 5d745fc989deb0cc959a096721694035dad2dfb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Verstraeten?= Date: Tue, 10 Feb 2026 17:20:29 +0100 Subject: [PATCH 5/6] Update machinery/src/video/mp4.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- machinery/src/video/mp4.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/machinery/src/video/mp4.go b/machinery/src/video/mp4.go index d5c49ce..74b5f0a 100644 --- a/machinery/src/video/mp4.go +++ b/machinery/src/video/mp4.go @@ -562,20 +562,25 @@ func (mp4 *MP4) Close(config *models.Config) { // the first referenced moof/mdat. Since sidx is the last box in init, // the anchor point is at initSize, and the first moof is at FreeBoxSize. if len(mp4.SegmentDurations) > 0 { - firstOffset := uint64(mp4.FreeBoxSize - initSize) - // Find the sidx we added and update its FirstOffset - for _, child := range init.Children { - if sidxBox, ok := child.(*mp4ff.SidxBox); ok { - sidxBox.FirstOffset = firstOffset - break + if mp4.FreeBoxSize < initSize { + // Avoid computing a negative offset and wrapping it to uint64. + log.Log.Error("mp4.Close(): FreeBoxSize is smaller than initSize; skipping sidx FirstOffset adjustment") + } else { + firstOffset := uint64(mp4.FreeBoxSize - initSize) + // Find the sidx we added and update its FirstOffset + for _, child := range init.Children { + if sidxBox, ok := child.(*mp4ff.SidxBox); ok { + sidxBox.FirstOffset = firstOffset + break + } } + // Re-encode with the corrected FirstOffset (same size, no layout change) + initBuf.Reset() + if err := init.Encode(&initBuf); err != nil { + log.Log.Error("mp4.Close(): error re-encoding init segment: " + err.Error()) + } + initSize = int64(initBuf.Len()) } - // Re-encode with the corrected FirstOffset (same size, no layout change) - initBuf.Reset() - if err := init.Encode(&initBuf); err != nil { - log.Log.Error("mp4.Close(): error re-encoding init segment: " + err.Error()) - } - initSize = int64(initBuf.Len()) } if initSize > mp4.FreeBoxSize { From 32b471f5708aedb3010f3204506668321d084b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Verstraeten?= Date: Tue, 10 Feb 2026 17:20:40 +0100 Subject: [PATCH 6/6] Update machinery/src/video/mp4.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- machinery/src/video/mp4.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machinery/src/video/mp4.go b/machinery/src/video/mp4.go index 74b5f0a..7331e81 100644 --- a/machinery/src/video/mp4.go +++ b/machinery/src/video/mp4.go @@ -80,7 +80,7 @@ func NewMP4(fileName string, spsNALUs [][]byte, ppsNALUs [][]byte, vpsNALUs [][] // Segments are ~FragmentDurationMs each, so: // numSegments = ceil(maxDurationSec * 1000 / FragmentDurationMs) + 1 (safety margin) // sidxSize = 24 + 12 * numSegments - baseSize := int64(2048) // ftyp + moov + headroom + baseSize := int64(2560) // ftyp + moov + extra headroom for large UUID signatures numSegments := int64(0) if maxDurationSec > 0 { // Use integer ceiling division to avoid underestimating the number of segments.