Repository: Eyevinn/mp4ff
Pull request #424 (2025-05-08 19:32:13)

Remove more encrypted boxes (#421 with tests)
Added tests to #421.
This features also resolves the issue #422.


Patch:
diff --git a/mp4/traf.go b/mp4/traf.go
index 7f90b2c..8a16f4b 100644
--- a/mp4/traf.go
+++ b/mp4/traf.go
@@ -282,6 +282,16 @@ func (t *TrafBox) RemoveEncryptionBoxes() uint64 {
 				nrBytesRemoved += ch.Size()
 				t.UUIDSenc = nil
 			}
+		case *SbgpBox:
+			if box.GroupingType == "seig" {
+				nrBytesRemoved += ch.Size()
+				t.Sbgp = nil
+			}
+		case *SgpdBox:
+			if box.GroupingType == "seig" {
+				nrBytesRemoved += ch.Size()
+				t.Sgpd = nil
+			}
 		default:
 			remainingChildren = append(remainingChildren, ch)
 		}


Test patch:
diff --git a/mp4/traf_test.go b/mp4/traf_test.go
index e10bed6..a1947f9 100644
--- a/mp4/traf_test.go
+++ b/mp4/traf_test.go
@@ -75,6 +75,82 @@ func TestTrafTrunWithoutOptimization(t *testing.T) {
 		runEncodeDecode(t, test, optimization)
 	}
 }
+func TestRemoveEncryptionBoxes(t *testing.T) {
+	// Create a traf box with various child boxes
+	traf := &mp4.TrafBox{}
+
+	// Add boxes that should be removed
+	saizBox := &mp4.SaizBox{Version: 0}
+	saioBox := &mp4.SaioBox{Version: 0}
+	sencBox := &mp4.SencBox{Version: 0}
+	uuidSencBox := &mp4.UUIDBox{}
+	_ = uuidSencBox.SetUUID(mp4.UUIDPiffSenc)
+	uuidSencBox.Senc = &mp4.SencBox{Version: 0}
+	uuidSencBox.Senc = &mp4.SencBox{Version: 0}
+	sbgpSeigBox := &mp4.SbgpBox{GroupingType: "seig"}
+	sgpdSeigBox := &mp4.SgpdBox{GroupingType: "seig"}
+	boxesToBeRemoved := []mp4.Box{saizBox, saioBox, sencBox, uuidSencBox, sbgpSeigBox, sgpdSeigBox}
+
+	// Add boxes that should NOT be removed
+	tfhdBox := &mp4.TfhdBox{}
+	trunBox := mp4.CreateTrun(0)
+
+	// Add all boxes to traf
+	_ = traf.AddChild(saizBox)
+	_ = traf.AddChild(saioBox)
+	_ = traf.AddChild(sencBox)
+	_ = traf.AddChild(uuidSencBox)
+	_ = traf.AddChild(sbgpSeigBox)
+	_ = traf.AddChild(sgpdSeigBox)
+	_ = traf.AddChild(tfhdBox)
+	_ = traf.AddChild(trunBox)
+
+	// Original children count
+	originalChildrenCount := len(traf.Children)
+
+	//
+	expectedBytesRemoved := saizBox.Size() + saioBox.Size() + sencBox.Size() +
+		uuidSencBox.Size() + sbgpSeigBox.Size() + sgpdSeigBox.Size()
+
+	// Call RemoveEncryptionBoxes
+	bytesRemoved := traf.RemoveEncryptionBoxes()
+
+	// Verify expected number of bytes removed (6 boxes * mockBoxSize)
+	if bytesRemoved != expectedBytesRemoved {
+		t.Errorf("Expected %d bytes removed, got %d", expectedBytesRemoved, bytesRemoved)
+	}
+
+	// Verify correct number of children remain
+	expectedRemainingChildren := originalChildrenCount - len(boxesToBeRemoved)
+	if len(traf.Children) != expectedRemainingChildren {
+		t.Errorf("Expected %d children remaining, got %d", expectedRemainingChildren, len(traf.Children))
+	}
+
+	// Verify encryption box pointers are nil
+	if traf.Saiz != nil {
+		t.Error("Saiz box was not removed")
+	}
+	if traf.Saio != nil {
+		t.Error("Saio box was not removed")
+	}
+	if traf.Senc != nil {
+		t.Error("Senc box was not removed")
+	}
+	if traf.Sbgp != nil && traf.Sbgp.GroupingType == "seig" {
+		t.Error("Sbgp box with GroupingType 'seig' was not removed")
+	}
+	if traf.Sgpd != nil && traf.Sgpd.GroupingType == "seig" {
+		t.Error("Sgpd box with GroupingType 'seig' was not removed")
+	}
+
+	// Verify Tfhd and Trun are still present
+	if traf.Tfhd == nil {
+		t.Error("Tfhd was incorrectly removed")
+	}
+	if traf.Trun == nil {
+		t.Error("Trun was incorrectly removed")
+	}
+}
 
 func runEncodeDecode(t *testing.T, test testSamples, withOptimization bool) {
 	traf := createTestTrafBox()