-
Notifications
You must be signed in to change notification settings - Fork 31
/
write.go
68 lines (60 loc) · 1.4 KB
/
write.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
package mp4
import (
"errors"
"io"
)
type Writer struct {
writer io.WriteSeeker
biStack []*BoxInfo
}
func NewWriter(w io.WriteSeeker) *Writer {
return &Writer{
writer: w,
}
}
func (w *Writer) Write(p []byte) (int, error) {
return w.writer.Write(p)
}
func (w *Writer) Seek(offset int64, whence int) (int64, error) {
return w.writer.Seek(offset, whence)
}
func (w *Writer) StartBox(bi *BoxInfo) (*BoxInfo, error) {
bi, err := WriteBoxInfo(w.writer, bi)
if err != nil {
return nil, err
}
w.biStack = append(w.biStack, bi)
return bi, nil
}
func (w *Writer) EndBox() (*BoxInfo, error) {
bi := w.biStack[len(w.biStack)-1]
w.biStack = w.biStack[:len(w.biStack)-1]
end, err := w.writer.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
bi.Size = uint64(end) - bi.Offset
if _, err = bi.SeekToStart(w.writer); err != nil {
return nil, err
}
if bi2, err := WriteBoxInfo(w.writer, bi); err != nil {
return nil, err
} else if bi.HeaderSize != bi2.HeaderSize {
return nil, errors.New("header size changed")
}
if _, err := w.writer.Seek(end, io.SeekStart); err != nil {
return nil, err
}
return bi, nil
}
func (w *Writer) CopyBox(r io.ReadSeeker, bi *BoxInfo) error {
if _, err := bi.SeekToStart(r); err != nil {
return err
}
if n, err := io.CopyN(w, r, int64(bi.Size)); err != nil {
return err
} else if n != int64(bi.Size) {
return errors.New("failed to copy box")
}
return nil
}