-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg-io.go
69 lines (59 loc) · 1.22 KB
/
ffmpeg-io.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
package main
import (
"io"
"log"
gmf "github.com/3d0c/gmf"
)
type SourceReader struct {
reader io.ReadCloser
buffer []byte
}
func NewSourceReader(reader io.ReadCloser) *SourceReader {
return &SourceReader{
reader: reader,
buffer: make([]byte, gmf.IO_BUFFER_SIZE),
}
}
func (s *SourceReader) GetAVIOContext(ictx *gmf.FmtCtx) (*gmf.AVIOContext, error) {
return gmf.NewAVIOContext(
ictx,
&gmf.AVIOHandlers{
ReadPacket: s.Read,
},
)
}
func (s *SourceReader) Read() ([]byte, int) {
if n, err := s.reader.Read(s.buffer); err != nil && err == io.EOF {
s.reader.Close()
return s.buffer, n
} else if err != nil {
log.Printf("read error: %s", err)
return nil, n
} else {
return s.buffer, n
}
}
type DestinationWriter struct {
writer io.Writer
}
func NewDestinationWriter(writer io.Writer) *DestinationWriter {
return &DestinationWriter{
writer: writer,
}
}
func (d *DestinationWriter) GetAVIOContext(ictx *gmf.FmtCtx) (*gmf.AVIOContext, error) {
return gmf.NewAVIOContext(
ictx,
&gmf.AVIOHandlers{
WritePacket: d.Write,
},
)
}
func (d *DestinationWriter) Write(buf []byte) int {
if n, err := d.writer.Write(buf); err != nil {
log.Printf("write error: %s", err)
return 0
} else {
return n
}
}