Skip to content

Commit

Permalink
Merge pull request #120 from edgeware/mp4ff-crop
Browse files Browse the repository at this point in the history
New tool: mp4ff-crop
  • Loading branch information
tobbee authored Jan 7, 2022
2 parents 2412ef9 + 83dcf4b commit 1d36b8e
Show file tree
Hide file tree
Showing 6 changed files with 601 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,11 @@ Some useful command line tools are available in `cmd`.
2. `mp4ff-pslister` extracts and displays SPS and PPS for AVC in a mp4 file. Partial information is printed for HEVC.
3. `mp4ff-nallister` lists NALUs and picture types for video in progressive or fragmented file
4. `mp4ff-wvttlister` lists details of wvtt (WebVTT in ISOBMFF) samples
5. `mp4ff-crop` shortens a progressive mp4 file to a specified duration

You can install these tools by going to their respective directory and run `go install .`.
You can install these tools by going to their respective directory and run `go install .` or directly from the repo with

go install github.com/edgeware/mp4ff/cmd/mp4ff-info@latest

## Example code

Expand Down
30 changes: 30 additions & 0 deletions cmd/mp4ff-crop/byteranges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

type byteRange struct {
start uint64
end uint64 // Included
}

type byteRanges struct {
ranges []byteRange
}

func createByteRanges() *byteRanges {
return &byteRanges{}
}

func (b *byteRanges) addRange(start, end uint64) {
if len(b.ranges) == 0 || b.ranges[len(b.ranges)-1].end+1 != start {
b.ranges = append(b.ranges, byteRange{start, end})
return
}
b.ranges[len(b.ranges)-1].end = end
}

func (b *byteRanges) size() uint64 {
var totSize uint64 = 0
for _, br := range b.ranges {
totSize += br.end - br.start + 1
}
return uint64(totSize)
}
44 changes: 44 additions & 0 deletions cmd/mp4ff-crop/crop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"bytes"
"log"
"os"
"testing"

"github.com/edgeware/mp4ff/mp4"
)

// TestCroppedFileDuration - simple test to check that cropped file has right duration
// In general, this duration will not be exactly the same as the one asked for.
func TestCroppedFileDuration(t *testing.T) {
testFile := "../../mp4/testdata/prog_8s.mp4"
cropDur := 2000

ifh, err := os.Open(testFile)
if err != nil {
t.Error(err)
}
defer ifh.Close()
parsedMp4, err := mp4.DecodeFile(ifh, mp4.WithDecodeMode(mp4.DecModeLazyMdat))
if err != nil {
t.Error(err)
}

buf := bytes.Buffer{}

err = cropMP4(parsedMp4, cropDur, &buf, ifh)
if err != nil {
log.Fatal(err)
}

decCropped, err := mp4.DecodeFile(&buf)
if err != nil {
t.Error(err)
}
moovDur := decCropped.Moov.Mvhd.Duration
moovTimescale := decCropped.Moov.Mvhd.Timescale
if uint64(cropDur)*uint64(moovTimescale) != moovDur*1000 {
t.Errorf("got %d/%dms instead of %dms", moovDur, moovTimescale, cropDur)
}
}
Loading

0 comments on commit 1d36b8e

Please sign in to comment.