• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Eyevinn / mp4ff / 11744569652

08 Nov 2024 03:06PM UTC coverage: 77.972% (+11.1%) from 66.885%
11744569652

push

github

tobbee
fix: ExtractInitProtectData was broken for video

4 of 6 new or added lines in 1 file covered. (66.67%)

474 existing lines in 38 files now uncovered.

15231 of 19534 relevant lines covered (77.97%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.92
/examples/resegmenter/resegment.go
1
package main
2

3
import (
4
        "fmt"
5
        "io"
6

7
        "github.com/Eyevinn/mp4ff/mp4"
8
)
9

10
// Resegment file into multiple segments
11
func Resegment(w io.Writer, in *mp4.File, chunkDur uint64, verbose bool) (*mp4.File, error) {
1✔
12
        if !in.IsFragmented() {
1✔
13
                return nil, fmt.Errorf("input file is not fragmented")
×
14
        }
×
15

16
        nrSamples := 0
1✔
17
        for _, iSeg := range in.Segments {
2✔
18
                for _, iFrag := range iSeg.Fragments {
2✔
19
                        trun := iFrag.Moof.Traf.Trun
1✔
20
                        nrSamples += int(trun.SampleCount())
1✔
21
                }
1✔
22
        }
23
        inSamples := make([]mp4.FullSample, 0, nrSamples)
1✔
24

1✔
25
        var trex *mp4.TrexBox
1✔
26
        if in.Init != nil {
1✔
27
                trex = in.Init.Moov.Mvex.Trex
×
28
        }
×
29
        for _, iSeg := range in.Segments {
2✔
30
                for _, iFrag := range iSeg.Fragments {
2✔
31
                        fSamples, err := iFrag.GetFullSamples(trex)
1✔
32
                        if err != nil {
1✔
UNCOV
33
                                return nil, err
×
UNCOV
34
                        }
×
35
                        inSamples = append(inSamples, fSamples...)
1✔
36
                }
37
        }
38
        inStyp := in.Segments[0].Styp
1✔
39
        inMoof := in.Segments[0].Fragments[0].Moof
1✔
40
        trackID := inMoof.Traf.Tfhd.TrackID
1✔
41

1✔
42
        nrChunksOut := uint64(nrSamples)*uint64(inSamples[0].Dur)/chunkDur + 1 // approximative, but good for allocation
1✔
43

1✔
44
        oFile := mp4.NewFile()
1✔
45
        oFile.Children = make([]mp4.Box, 0, 2+nrChunksOut*3) //  ftyp + moov + (styp+moof+mdat for each segment)
1✔
46
        if in.Init != nil {
1✔
47
                oFile.AddChild(in.Ftyp, 0)
×
48
                oFile.AddChild(in.Moov, 0)
×
49
        }
×
50

51
        currOutSeqNr := uint32(1)
1✔
52
        frag, err := addNewSegment(oFile, inStyp, currOutSeqNr, trackID)
1✔
53
        if err != nil {
1✔
54
                return nil, err
×
55
        }
×
56
        if verbose {
2✔
57
                fmt.Fprintf(w, "Started segment %d at dts=%d pts=%d\n", 1, inSamples[0].DecodeTime, inSamples[0].PresentationTime())
1✔
58
        }
1✔
59
        nextSampleNrToWrite := 1
1✔
60

1✔
61
        for nr, s := range inSamples {
2✔
62
                if verbose && s.IsSync() {
2✔
63
                        fmt.Fprintf(w, "%4d DTS %d PTS %d\n", nr, s.DecodeTime, s.PresentationTime())
1✔
64
                }
1✔
65
                if s.PresentationTime() >= chunkDur*uint64(currOutSeqNr) && s.IsSync() {
2✔
66
                        err = addSamplesToFrag(frag, inSamples, nextSampleNrToWrite, nr+1, trackID)
1✔
67
                        if err != nil {
1✔
68
                                return nil, err
×
69
                        }
×
70
                        nextSampleNrToWrite = nr + 1
1✔
71
                        currOutSeqNr++
1✔
72
                        frag, err = addNewSegment(oFile, inStyp, currOutSeqNr, trackID)
1✔
73
                        if err != nil {
1✔
UNCOV
74
                                return nil, err
×
UNCOV
75
                        }
×
76
                        if verbose {
2✔
77
                                fmt.Fprintf(w, "Started segment %d at dts=%d pts=%d\n", currOutSeqNr, s.DecodeTime, s.PresentationTime())
1✔
78
                        }
1✔
79
                }
80
        }
81
        err = addSamplesToFrag(frag, inSamples, nextSampleNrToWrite, len(inSamples)+1, trackID)
1✔
82
        if err != nil {
1✔
UNCOV
83
                return nil, err
×
84
        }
×
85

86
        return oFile, nil
1✔
87
}
88

89
func addSamplesToFrag(frag *mp4.Fragment, samples []mp4.FullSample, nextSampleNrToWrite, stopNr int, trackID uint32) error {
1✔
90
        totSize := uint64(0)
1✔
91
        for nr := nextSampleNrToWrite; nr < stopNr; nr++ {
2✔
92
                totSize += uint64(samples[nr-1].Size)
1✔
93
        }
1✔
94
        frag.Mdat.Data = make([]byte, 0, totSize)
1✔
95
        frag.Moof.Traf.Trun.Samples = make([]mp4.Sample, 0, stopNr-nextSampleNrToWrite+2)
1✔
96
        for nr := nextSampleNrToWrite; nr < stopNr; nr++ {
2✔
97
                err := frag.AddFullSampleToTrack(samples[nr-1], trackID)
1✔
98
                if err != nil {
1✔
UNCOV
99
                        return err
×
100
                }
×
101
        }
102
        return nil
1✔
103
}
104

105
func addNewSegment(oFile *mp4.File, styp *mp4.StypBox, seqNr, trackID uint32) (*mp4.Fragment, error) {
1✔
106
        oFile.AddChild(styp, 0)
1✔
107
        frag, err := mp4.CreateFragment(seqNr, trackID)
1✔
108
        if err != nil {
1✔
109
                return nil, err
×
UNCOV
110
        }
×
111
        for _, box := range frag.GetChildren() {
2✔
112
                oFile.AddChild(box, 0)
1✔
113
        }
1✔
114
        return frag, nil
1✔
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc