From 3540adc22e76a26f97f9724593ea708d5d56f5c4 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Tue, 30 Jun 2026 15:05:43 +0200 Subject: mp4: add optional fields to tfhd and trun --- writer.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'writer.go') diff --git a/writer.go b/writer.go index e741d51..5d18b71 100644 --- a/writer.go +++ b/writer.go @@ -98,6 +98,11 @@ func (w *Writer) putBytes(p []byte) { w.pos += len(p) } +func (w *Writer) putString(s string) { + copy(w.buf[w.pos:], s) + w.pos += len(s) +} + // putFixedString writes a fixed-length string field with null padding. func (w *Writer) putFixedString(s string, length int) { n := copy(w.buf[w.pos:w.pos+length], s) @@ -246,7 +251,7 @@ func (w *Writer) WriteHdlr(handlerType [4]byte, name string) { w.putUint32(0) // predefined w.putBytes(handlerType[:]) w.putZeros(12) // reserved - w.putBytes([]byte(name)) + w.putString(name) w.putUint8(0) // null terminator w.EndBox() } @@ -415,9 +420,20 @@ func (w *Writer) WriteMfhd(sequenceNumber uint32) { } // WriteTfhd writes a complete tfhd box. -func (w *Writer) WriteTfhd(flags uint32, trackId uint32) { +// WriteTfhd writes a track fragment header. Default sample duration, size, and +// flags are written when their corresponding flag bits are set. +func (w *Writer) WriteTfhd(flags, trackId, defaultDuration, defaultSize, defaultFlags uint32) { w.StartFullBox(TypeTfhd, 0, flags) w.putUint32(trackId) + if flags&TfhdDefaultSampleDurationPresent != 0 { + w.putUint32(defaultDuration) + } + if flags&TfhdDefaultSampleSizePresent != 0 { + w.putUint32(defaultSize) + } + if flags&TfhdDefaultSampleFlagsPresent != 0 { + w.putUint32(defaultFlags) + } w.EndBox() } @@ -434,12 +450,17 @@ func (w *Writer) WriteTfdt(baseMediaDecodeTime uint64) { } // WriteTrun writes a complete trun box. -func (w *Writer) WriteTrun(flags uint32, dataOffset int32, entries []TrunEntry) { +// WriteTrun writes a track run. firstSampleFlags is written when +// TrunFirstSampleFlagsPresent is set, overriding the default for sample one. +func (w *Writer) WriteTrun(flags uint32, dataOffset int32, firstSampleFlags uint32, entries []TrunEntry) { w.StartFullBox(TypeTrun, 0, flags) w.putUint32(uint32(len(entries))) if flags&TrunDataOffsetPresent != 0 { w.putInt32(dataOffset) } + if flags&TrunFirstSampleFlagsPresent != 0 { + w.putUint32(firstSampleFlags) + } for _, e := range entries { if flags&TrunSampleDurationPresent != 0 { w.putUint32(e.Duration) -- cgit v1.3.1