-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmain.go
544 lines (489 loc) · 13.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"sort"
"github.com/Eyevinn/mp4ff/internal"
"github.com/Eyevinn/mp4ff/mp4"
)
const (
appName = "mp4ff-crop"
)
var usg = `%s crops a (progressive) mp4 file to just before a sync frame after specified number of milliseconds.
The goal is to leave the file structure intact except for cropping of samples and
moving mdat to the end of the file, if not already there.
Usage of %s:
`
type options struct {
durationMS uint
version bool
}
func parseOptions(fs *flag.FlagSet, args []string) (*options, error) {
fs.Usage = func() {
fmt.Fprintf(os.Stderr, usg, appName, appName)
fmt.Fprintf(os.Stderr, "\n%s [options] <inFile> <outFile>\n\noptions:\n", appName)
fs.PrintDefaults()
}
opts := options{}
fs.UintVar(&opts.durationMS, "d", 1000, "Duration in milliseconds")
fs.BoolVar(&opts.version, "version", false, "Get mp4ff version")
err := fs.Parse(args[1:])
return &opts, err
}
func main() {
if err := run(os.Args, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(args []string, stdout io.Writer) error {
fs := flag.NewFlagSet(appName, flag.ContinueOnError)
o, err := parseOptions(fs, args)
if err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
if o.version {
fmt.Fprintf(stdout, "%s %s\n", appName, internal.GetVersion())
return nil
}
if len(fs.Args()) != 2 {
fs.Usage()
return fmt.Errorf("must specify inFile and outFile")
}
if o.durationMS == 0 {
fs.Usage()
return fmt.Errorf("error: duration must be larger than 0: %dms", o.durationMS)
}
inFilePath := fs.Arg(0)
outFilePath := fs.Arg(1)
ifh, err := os.Open(inFilePath)
if err != nil {
return fmt.Errorf("error opening input file: %w", err)
}
defer ifh.Close()
parsedMp4, err := mp4.DecodeFile(ifh, mp4.WithDecodeMode(mp4.DecModeLazyMdat))
if err != nil {
return fmt.Errorf("error decoding mp4 file: %w", err)
}
ofh, err := os.Create(outFilePath)
if err != nil {
return fmt.Errorf("error creating output file: %w", err)
}
defer ofh.Close()
err = cropMP4(parsedMp4, int(o.durationMS), ofh, ifh)
if err != nil {
return fmt.Errorf("error cropping mp4 file: %w", err)
}
return nil
}
func cropMP4(inMP4 *mp4.File, durationMS int, w io.Writer, ifh io.ReadSeeker) error {
if inMP4.IsFragmented() {
return fmt.Errorf("only progressive files are supported")
}
inMoov := inMP4.Moov
inMoovDur := float64(inMoov.Mvhd.Duration) / float64(inMoov.Mvhd.Timescale)
fmt.Printf("input moov duration = %.3fs\n", inMoovDur)
endTime, endTimescale, err := findEndTime(inMoov, durationMS)
if err != nil {
return err
}
err = cropToTime(inMP4, endTime, endTimescale, w, ifh)
if err != nil {
return err
}
fmt.Printf("wrote output with endTime=%dms\n", (endTime*1000)/endTimescale)
return nil
}
// findEndTime - find closest video sync frame, or audio frame if no video
func findEndTime(moov *mp4.MoovBox, durationMS int) (endTime, endTimescale uint64, err error) {
var syncTrak *mp4.TrakBox
for _, trak := range moov.Traks {
if trak.Mdia.Hdlr.HandlerType == "vide" {
syncTrak = trak
break
}
}
if syncTrak == nil {
for _, trak := range moov.Traks {
if trak.Mdia.Hdlr.HandlerType == "soun" {
syncTrak = trak
fmt.Printf("Uses audio track %d for endtime\n", trak.Tkhd.TrackID)
break
}
}
}
if syncTrak == nil {
return 0, 0, fmt.Errorf("did not find any video or audio track")
}
//trakDur := float64(trak.Tkhd.Duration) / float64(moov.Mvhd.Timescale)
//fmt.Printf("video trak %d duration = %.3fs\n", trak.Tkhd.TrackID, trakDur)
endTimescale = uint64(syncTrak.Mdia.Mdhd.Timescale)
endTime = uint64(durationMS) * endTimescale / 1000
stbl := syncTrak.Mdia.Minf.Stbl
stts := stbl.Stts // TimeToSampleBox
lastSampleNr, err := stts.GetSampleNrAtTime(endTime)
if err != nil {
return 0, 0, err
}
stss := stbl.Stss
if stss != nil {
foundSyncFrame := false
for sampleNr := lastSampleNr; sampleNr <= stss.SampleNumber[len(stss.SampleNumber)-1]; sampleNr++ {
if stss.IsSyncSample(sampleNr) {
lastSampleNr = sampleNr - 1
foundSyncFrame = true
break
}
}
if !foundSyncFrame {
return 0, 0, fmt.Errorf("did not find any syncframe at or after time")
}
}
lastTime, lastDur := stts.GetDecodeTime(lastSampleNr)
endTime = lastTime + uint64(lastDur)
return endTime, endTimescale, nil
}
func cropToTime(inMP4 *mp4.File, endTime, endTimescale uint64, w io.Writer, ifh io.ReadSeeker) error {
traks := inMP4.Moov.Traks
tos, err := findTrakEnds(traks, endTime, endTimescale)
if err != nil {
return err
}
byteRanges := createByteRanges()
firstOffset, err := fillTrakOutsAndByteRanges(traks, tos, byteRanges)
if err != nil {
return err
}
err = cropStblChildren(traks, tos)
if err != nil {
return err
}
updateChunkOffsets(inMP4, firstOffset)
err = writeUptoMdat(inMP4, endTime, endTimescale, w)
if err != nil {
return err
}
err = writeMdat(byteRanges, inMP4.Mdat, w, ifh)
if err != nil {
return err
}
return nil
}
type trakOut struct {
lastSampleNr uint32
endTime uint64
lastChunk mp4.Chunk
nextInChunkNr uint32
chunkOffsets []uint64
}
// findTrakEnds - find where traks end in form of last chunk, lastSampleNr and endTime
func findTrakEnds(traks []*mp4.TrakBox, endTime, endTimescale uint64) (map[uint32]*trakOut, error) {
tos := make(map[uint32]*trakOut, len(traks))
for _, trak := range traks {
trackID := trak.Tkhd.TrackID
stbl := trak.Mdia.Minf.Stbl
tos[trackID] = &trakOut{
nextInChunkNr: 1,
}
to := tos[trackID]
trackTimeScale := trak.Mdia.Mdhd.Timescale
trackEndTime := endTime
if trackTimeScale != uint32(endTimescale) {
trackEndTime = endTime * uint64(trackTimeScale) / endTimescale
}
stts := stbl.Stts
endSampleNr, err := stts.GetSampleNrAtTime(trackEndTime)
if err != nil {
return nil, err
}
endSampleNr--
to.lastSampleNr = endSampleNr
decTime, dur := stts.GetDecodeTime(endSampleNr)
trackEndTime = decTime + uint64(dur)
tos[trackID].endTime = trackEndTime
stsc := stbl.Stsc
chunkNr, _, err := stsc.ChunkNrFromSampleNr(int(endSampleNr))
if err != nil {
return nil, err
}
chunk := stsc.GetChunk(uint32(chunkNr))
to.lastChunk = chunk
}
return tos, nil
}
func fillTrakOutsAndByteRanges(traks []*mp4.TrakBox, tos map[uint32]*trakOut, byteRanges *byteRanges) (firstOffset uint64, err error) {
var currentOutOffset uint64
var minChunkOffset uint64
for {
var trakIDMin uint32
var stblMin *mp4.StblBox
minChunkOffset = 1 << 62
for _, trak := range traks {
trakID := trak.Tkhd.TrackID
stbl := trak.Mdia.Minf.Stbl
to := tos[trakID]
nextChunkNr := int(to.nextInChunkNr)
if nextChunkNr > int(to.lastChunk.ChunkNr) {
continue
}
var chunkOffset uint64
var err error
if stbl.Stco != nil {
chunkOffset, err = stbl.Stco.GetOffset(nextChunkNr)
} else {
chunkOffset, err = stbl.Co64.GetOffset(nextChunkNr)
}
if err != nil {
return 0, err
}
if chunkOffset < minChunkOffset {
minChunkOffset = chunkOffset
trakIDMin = trakID
stblMin = stbl
}
}
if trakIDMin == 0 {
break //Done
}
if firstOffset == 0 {
firstOffset = minChunkOffset
currentOutOffset = firstOffset
}
to := tos[trakIDMin]
chunk := stblMin.Stsc.GetChunk(to.nextInChunkNr)
lastSampleInChunk := chunk.StartSampleNr + chunk.NrSamples - 1
sampleNrStart := chunk.StartSampleNr
sampleNrEnd := minUint32(lastSampleInChunk, to.lastSampleNr)
inOffset := minChunkOffset
outChunkSize, _ := stblMin.Stsz.GetTotalSampleSize(sampleNrStart, sampleNrEnd)
byteRanges.addRange(inOffset, inOffset+outChunkSize-1)
to.chunkOffsets = append(to.chunkOffsets, currentOutOffset)
currentOutOffset += outChunkSize
to.nextInChunkNr++
}
return firstOffset, nil
}
// updateChunkOffsets - calculate new moov size, and update stco/co64 (chunk offsets)
func updateChunkOffsets(inMP4 *mp4.File, firstOffset uint64) {
var sizeWithoutMdat uint64 = 0
for _, box := range inMP4.Children {
if box.Type() != "mdat" {
sizeWithoutMdat += box.Size()
}
}
mdatStart := sizeWithoutMdat
mdatPayloadStart := mdatStart + 8
deltaOffset := int64(mdatPayloadStart) - int64(firstOffset)
for _, trak := range inMP4.Moov.Traks {
stco := trak.Mdia.Minf.Stbl.Stco
if stco != nil {
for i := range stco.ChunkOffset {
stco.ChunkOffset[i] = uint32(int64(stco.ChunkOffset[i]) + deltaOffset)
}
} else {
co64 := trak.Mdia.Minf.Stbl.Co64
for i := range co64.ChunkOffset {
co64.ChunkOffset[i] = uint64(int64(co64.ChunkOffset[i]) + deltaOffset)
}
}
}
}
func writeUptoMdat(inMP4 *mp4.File, endTime, endTimescale uint64, w io.Writer) error {
pos := uint64(0)
mvhd := inMP4.Moov.Mvhd
newDur := endTime * uint64(mvhd.Timescale) / endTimescale
mvhd.Duration = newDur
for _, trak := range inMP4.Moov.Traks {
prevDur := trak.Tkhd.Duration
trak.Tkhd.Duration = newDur
if newDur > prevDur {
return fmt.Errorf("new duration %d larger than previous %d", newDur, prevDur)
}
durDiff := prevDur - newDur
if trak.Edts != nil {
for i := range trak.Edts.Elst {
for j := range trak.Edts.Elst[i].Entries {
prevDur := trak.Edts.Elst[i].Entries[j].SegmentDuration
if prevDur > durDiff {
trak.Edts.Elst[i].Entries[j].SegmentDuration -= durDiff
}
}
}
}
}
for _, box := range inMP4.Children {
if box.Type() != "mdat" {
pos += box.Size()
err := box.Encode(w)
if err != nil {
return err
}
}
}
return nil
}
func writeMdat(byteRanges *byteRanges, mdatIn *mp4.MdatBox, w io.Writer, ifh io.ReadSeeker) error {
// write mdat header
mdatPayloadSize := byteRanges.size()
if mdatPayloadSize+8 >= 1<<32 {
return fmt.Errorf("too big mdat size for 32 bits: %d", mdatPayloadSize)
}
err := mp4.EncodeHeaderWithSize("mdat", mdatPayloadSize+8, false, w)
if err != nil {
return err
}
// write mdat body
nrBytesWritten := int64(0)
for _, br := range byteRanges.ranges {
n, err := mdatIn.CopyData(int64(br.start), int64(br.end-br.start+1), ifh, w)
if err != nil {
return err
}
nrBytesWritten += n
}
if nrBytesWritten != int64(mdatPayloadSize) {
return fmt.Errorf("wrote %d instead of %d in mdat", nrBytesWritten, mdatPayloadSize)
}
return nil
}
func minUint32(a, b uint32) uint32 {
if a < b {
return a
}
return b
}
func cropStblChildren(traks []*mp4.TrakBox, trakOuts map[uint32]*trakOut) (err error) {
for _, trak := range traks {
trakID := trak.Tkhd.TrackID
stbl := trak.Mdia.Minf.Stbl
to := trakOuts[trakID]
for _, ch := range stbl.Children {
switch ch.Type() {
case "stts":
cropStts(ch.(*mp4.SttsBox), to.lastSampleNr)
case "stss":
cropStss(ch.(*mp4.StssBox), to.lastSampleNr)
case "ctts":
cropCtts(ch.(*mp4.CttsBox), to.lastSampleNr)
case "stsc":
err = cropStsc(ch.(*mp4.StscBox), to.lastSampleNr)
case "stsz":
cropStsz(ch.(*mp4.StszBox), to.lastSampleNr)
case "sdtp":
cropSdtp(ch.(*mp4.SdtpBox), to.lastSampleNr)
case "stco":
updateStco(ch.(*mp4.StcoBox), to.chunkOffsets)
case "co64":
updateCo64(ch.(*mp4.Co64Box), to.chunkOffsets)
}
}
}
return err
}
func cropStts(b *mp4.SttsBox, lastSampleNr uint32) {
var countedSamples uint32 = 0
lastEntry := -1
for i := 0; i < len(b.SampleCount); i++ {
if countedSamples < lastSampleNr {
lastEntry++
}
if countedSamples+b.SampleCount[i] >= lastSampleNr {
break
}
countedSamples += b.SampleCount[i]
}
remaining := lastSampleNr - countedSamples
if remaining > 0 {
b.SampleCount[lastEntry] = remaining
}
b.SampleCount = b.SampleCount[:lastEntry+1]
b.SampleTimeDelta = b.SampleTimeDelta[:lastEntry+1]
}
func cropStss(b *mp4.StssBox, lastSampleNr uint32) {
nrEntries := b.EntryCount()
nrEntriesToKeep := 0
for i := uint32(0); i < nrEntries; i++ {
if b.SampleNumber[i] > lastSampleNr {
break
}
nrEntriesToKeep++
}
b.SampleNumber = b.SampleNumber[:nrEntriesToKeep]
}
func cropCtts(b *mp4.CttsBox, lastSampleNr uint32) {
lastIdx := sort.Search(len(b.EndSampleNr), func(i int) bool { return b.EndSampleNr[i] >= lastSampleNr })
// Finally cut down the endSampleNr for this index
b.EndSampleNr[lastIdx] = lastSampleNr
b.EndSampleNr = b.EndSampleNr[:lastIdx+1]
b.SampleOffset = b.SampleOffset[:lastIdx]
}
func cropStsc(b *mp4.StscBox, lastSampleNr uint32) error {
entryIdx := b.FindEntryNrForSampleNr(lastSampleNr, 0)
lastEntry := b.Entries[entryIdx]
b.Entries = b.Entries[:entryIdx+1]
if len(b.SampleDescriptionID) > 0 {
b.Entries = b.Entries[:entryIdx+1]
}
samplesLeft := lastSampleNr - lastEntry.FirstSampleNr + 1
nrChunksInLast := samplesLeft / lastEntry.SamplesPerChunk
nrLeft := samplesLeft - nrChunksInLast*lastEntry.SamplesPerChunk
if nrLeft > 0 {
sdid := b.GetSampleDescriptionID(int(lastEntry.FirstChunk))
err := b.AddEntry(lastEntry.FirstChunk+nrChunksInLast, nrLeft, sdid)
if err != nil {
return fmt.Errorf("stsc AddEntry: %w", err)
}
}
return nil
}
func cropStsz(b *mp4.StszBox, lastSampleNr uint32) {
if b.SampleUniformSize == 0 {
b.SampleSize = b.SampleSize[:lastSampleNr]
}
b.SampleNumber = lastSampleNr
}
func cropSdtp(b *mp4.SdtpBox, lastSampleNr uint32) {
if len(b.Entries) > int(lastSampleNr) {
b.Entries = b.Entries[:lastSampleNr]
}
}
func updateStco(b *mp4.StcoBox, offsets []uint64) {
b.ChunkOffset = make([]uint32, len(offsets))
for i := range offsets {
b.ChunkOffset[i] = uint32(offsets[i])
}
}
func updateCo64(b *mp4.Co64Box, offsets []uint64) {
b.ChunkOffset = make([]uint64, len(offsets))
_ = copy(b.ChunkOffset, offsets)
}
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)
}