aboutsummaryrefslogtreecommitdiff
path: root/track
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-09-15 00:06:16 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-09-15 00:06:16 +0200
commit641feb730b9984ae2a280ee61783b133ab949e5f (patch)
tree959e01125a7bfa7f5a2caa9c4c0d812582a182d1 /track
parent2f8840b24c94598ec9803c17e9e6a8e76f1855b9 (diff)
downloadmp4-641feb730b9984ae2a280ee61783b133ab949e5f.tar.gz
mp4-641feb730b9984ae2a280ee61783b133ab949e5f.zip
track: return movie timescale from ParseTracks
Diffstat (limited to 'track')
-rw-r--r--track/benchmark_test.go6
-rw-r--r--track/track.go15
2 files changed, 12 insertions, 9 deletions
diff --git a/track/benchmark_test.go b/track/benchmark_test.go
index 0006e62..12ed4ee 100644
--- a/track/benchmark_test.go
+++ b/track/benchmark_test.go
@@ -42,7 +42,7 @@ func BenchmarkParseTracks(b *testing.B) {
b.SetBytes(int64(len(moov)))
b.ReportAllocs()
for b.Loop() {
- tracks, _, err := track.ParseTracks(moov)
+ tracks, _, _, err := track.ParseTracks(moov)
if err != nil {
b.Fatal(err)
}
@@ -62,7 +62,7 @@ func BenchmarkParseTracksAndCodec(b *testing.B) {
b.ReportAllocs()
var sink int
for b.Loop() {
- tracks, _, err := track.ParseTracks(moov)
+ tracks, _, _, err := track.ParseTracks(moov)
if err != nil {
b.Fatal(err)
}
@@ -78,7 +78,7 @@ func BenchmarkParseTracksAndCodec(b *testing.B) {
func BenchmarkCollectTrackSampleStats(b *testing.B) {
for _, path := range benchFiles {
moov := loadMoov(b, path)
- tracks, _, err := track.ParseTracks(moov)
+ tracks, _, _, err := track.ParseTracks(moov)
if err != nil {
b.Fatal(err)
}
diff --git a/track/track.go b/track/track.go
index f80abcd..f2e23ec 100644
--- a/track/track.go
+++ b/track/track.go
@@ -356,11 +356,12 @@ var (
// ParseTracks parses a moov box buffer and returns the tracks found with
// their samples fully populated. The moov buffer must include the box header
-// (the full top-level moov box). The movie duration (from mvhd) is also returned.
+// (the full top-level moov box). The movie timescale and duration (from mvhd)
+// are also returned.
//
// Returns an error if the moov box is not found, if no playable tracks are
// found, or if sample tables cannot be parsed for any track.
-func ParseTracks(moovBuf []byte) ([]*Track, uint64, error) {
+func ParseTracks(moovBuf []byte) ([]*Track, uint32, uint64, error) {
return ParseTracksInto(nil, moovBuf)
}
@@ -368,13 +369,14 @@ func ParseTracks(moovBuf []byte) ([]*Track, uint64, error) {
// and their sample slices, which avoids most allocations when the new file has
// the same shape as the previous one. Pass the slice returned by an earlier
// call as dst. The tracks in dst must not be used after this call.
-func ParseTracksInto(dst []*Track, moovBuf []byte) ([]*Track, uint64, error) {
+func ParseTracksInto(dst []*Track, moovBuf []byte) ([]*Track, uint32, uint64, error) {
mr := mp4.NewReader(moovBuf)
if !mr.Next() || mr.Type() != mp4.TypeMoov {
- return nil, 0, ErrMoovNotFound
+ return nil, 0, 0, ErrMoovNotFound
}
var tracks []*Track
+ var timescale uint32
var duration uint64
reused := 0
@@ -382,7 +384,8 @@ func ParseTracksInto(dst []*Track, moovBuf []byte) ([]*Track, uint64, error) {
for mr.Next() {
switch mr.Type() {
case mp4.TypeMvhd:
- _, dur, _ := mr.ReadMvhd()
+ ts, dur, _ := mr.ReadMvhd()
+ timescale = ts
duration = dur
case mp4.TypeTrak:
var t *Track
@@ -416,7 +419,7 @@ func ParseTracksInto(dst []*Track, moovBuf []byte) ([]*Track, uint64, error) {
valid = append(valid, t)
}
- return valid, duration, nil
+ return valid, timescale, duration, nil
}
// resetTrack clears t for reuse while keeping the backing array of its sample