forked from zergon321/reisen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.go
276 lines (216 loc) · 6.06 KB
/
media.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
package reisen
// #cgo pkg-config: libavformat libavcodec libavutil libswscale
// #include <libavcodec/avcodec.h>
// #include <libavformat/avformat.h>
// #include <libavutil/avconfig.h>
// #include <libswscale/swscale.h>
// #include <libavcodec/bsf.h>
import "C"
import (
"fmt"
"time"
"unsafe"
)
// Media is a media file containing
// audio, video and other types of streams.
type Media struct {
ctx *C.AVFormatContext
packet *C.AVPacket
streams []Stream
}
// StreamCount returns the number of streams.
func (media *Media) StreamCount() int {
return int(media.ctx.nb_streams)
}
// Streams returns a slice of all the available
// media data streams.
func (media *Media) Streams() []Stream {
streams := make([]Stream, len(media.streams))
copy(streams, media.streams)
return streams
}
// VideoStreams returns all the
// video streams of the media file.
func (media *Media) VideoStreams() []*VideoStream {
videoStreams := []*VideoStream{}
for _, stream := range media.streams {
if videoStream, ok := stream.(*VideoStream); ok {
videoStreams = append(videoStreams, videoStream)
}
}
return videoStreams
}
// AudioStreams returns all the
// audio streams of the media file.
func (media *Media) AudioStreams() []*AudioStream {
audioStreams := []*AudioStream{}
for _, stream := range media.streams {
if audioStream, ok := stream.(*AudioStream); ok {
audioStreams = append(audioStreams, audioStream)
}
}
return audioStreams
}
// Duration returns the overall duration
// of the media file.
func (media *Media) Duration() (time.Duration, error) {
dur := media.ctx.duration
tm := float64(dur) / float64(TimeBase)
return time.ParseDuration(fmt.Sprintf("%fs", tm))
}
// FormatName returns the name of the media format.
func (media *Media) FormatName() string {
if media.ctx.iformat.name == nil {
return ""
}
return C.GoString(media.ctx.iformat.name)
}
// FormatLongName returns the long name
// of the media container.
func (media *Media) FormatLongName() string {
if media.ctx.iformat.long_name == nil {
return ""
}
return C.GoString(media.ctx.iformat.long_name)
}
// FormatMIMEType returns the MIME type name
// of the media container.
func (media *Media) FormatMIMEType() string {
if media.ctx.iformat.mime_type == nil {
return ""
}
return C.GoString(media.ctx.iformat.mime_type)
}
// findStreams retrieves the stream information
// from the media container.
func (media *Media) findStreams() error {
streams := []Stream{}
status := C.avformat_find_stream_info(media.ctx, nil)
if status < 0 {
return fmt.Errorf(
"couldn't find stream information")
}
innerStreams := unsafe.Slice(
media.ctx.streams, media.ctx.nb_streams)
for _, innerStream := range innerStreams {
codecParams := innerStream.codecpar
codec := C.avcodec_find_decoder(codecParams.codec_id)
if codec == nil {
unknownStream := new(UnknownStream)
unknownStream.inner = innerStream
unknownStream.codecParams = codecParams
unknownStream.media = media
streams = append(streams, unknownStream)
continue
}
switch codecParams.codec_type {
case C.AVMEDIA_TYPE_VIDEO:
videoStream := new(VideoStream)
videoStream.inner = innerStream
videoStream.codecParams = codecParams
videoStream.codec = codec
videoStream.media = media
streams = append(streams, videoStream)
case C.AVMEDIA_TYPE_AUDIO:
audioStream := new(AudioStream)
audioStream.inner = innerStream
audioStream.codecParams = codecParams
audioStream.codec = codec
audioStream.media = media
streams = append(streams, audioStream)
default:
unknownStream := new(UnknownStream)
unknownStream.inner = innerStream
unknownStream.codecParams = codecParams
unknownStream.codec = codec
unknownStream.media = media
streams = append(streams, unknownStream)
}
}
media.streams = streams
return nil
}
// OpenDecode opens the media container for decoding.
//
// CloseDecode() should be called afterwards.
func (media *Media) OpenDecode() error {
media.packet = C.av_packet_alloc()
if media.packet == nil {
return fmt.Errorf(
"couldn't allocate a new packet")
}
return nil
}
// ReadPacket reads the next packet from the media stream.
func (media *Media) ReadPacket() (*Packet, bool, error) {
status := C.av_read_frame(media.ctx, media.packet)
if status < 0 {
if status == C.int(ErrorAgain) {
return nil, true, nil
}
// No packets anymore.
return nil, false, nil
}
// Filter the packet if needed.
packetStream := media.streams[media.packet.stream_index]
outPacket := media.packet
if packetStream.filter() != nil {
filter := packetStream.filter()
packetIn := packetStream.filterIn()
packetOut := packetStream.filterOut()
status = C.av_packet_ref(packetIn, media.packet)
if status < 0 {
return nil, false,
fmt.Errorf("%d: couldn't reference the packet",
status)
}
status = C.av_bsf_send_packet(filter, packetIn)
if status < 0 {
return nil, false,
fmt.Errorf("%d: couldn't send the packet to the filter",
status)
}
status = C.av_bsf_receive_packet(filter, packetOut)
if status < 0 {
return nil, false,
fmt.Errorf("%d: couldn't receive the packet from the filter",
status)
}
outPacket = packetOut
}
return newPacket(media, outPacket), true, nil
}
// CloseDecode closes the media container for decoding.
func (media *Media) CloseDecode() error {
C.av_free(unsafe.Pointer(media.packet))
media.packet = nil
return nil
}
// Close closes the media container.
func (media *Media) Close() {
C.avformat_free_context(media.ctx)
media.ctx = nil
}
// NewMedia returns a new media container analyzer
// for the specified media file.
func NewMedia(filename string) (*Media, error) {
media := &Media{
ctx: C.avformat_alloc_context(),
}
if media.ctx == nil {
return nil, fmt.Errorf(
"couldn't create a new media context")
}
fname := C.CString(filename)
status := C.avformat_open_input(&media.ctx, fname, nil, nil)
if status < 0 {
return nil, fmt.Errorf(
"couldn't open file %s", filename)
}
C.free(unsafe.Pointer(fname))
err := media.findStreams()
if err != nil {
return nil, err
}
return media, nil
}