@@ -101,15 +101,25 @@ func Test_decoder_003(t *testing.T) {
101
101
// Decode video frames and resize them
102
102
assert := assert .New (t )
103
103
104
+ // Open the file
104
105
manager := NewManager ()
105
106
media , err := manager .Open ("./etc/test/sample.mp4" , nil )
106
107
if ! assert .NoError (err ) {
107
108
t .SkipNow ()
108
109
}
109
110
defer media .Close ()
110
111
112
+ // Initialize
113
+ n := 0
114
+ // tmpdir := t.TempDir()
115
+ tmpdir , err := os .MkdirTemp ("" , "media_test" )
116
+ if ! assert .NoError (err ) {
117
+ t .SkipNow ()
118
+ }
119
+
120
+ // Create a decoder which resizes the video frames
111
121
decoder , err := media .Decoder (func (stream Stream ) (Parameters , error ) {
112
- // Make greyscale images
122
+ // Rescale the video
113
123
if stream .Type () == VIDEO {
114
124
return manager .VideoParameters (640 , 480 , "yuv420p" )
115
125
}
@@ -120,23 +130,17 @@ func Test_decoder_003(t *testing.T) {
120
130
t .SkipNow ()
121
131
}
122
132
123
- // Frame function
124
- n := 0
125
- // tmpdir := t.TempDir()
126
- tmpdir , err := os .MkdirTemp ("" , "media_test" )
127
- if ! assert .NoError (err ) {
128
- t .SkipNow ()
129
- }
133
+ // This is the function which processes the frames
130
134
framefn := func (frame Frame ) error {
131
- if frame .Type () != VIDEO {
132
- return nil
133
- }
135
+ // Create an output file
134
136
filename := filepath .Join (tmpdir , fmt .Sprintf ("frame%03d.jpg" , n ))
135
137
w , err := os .Create (filename )
136
138
if err != nil {
137
139
return err
138
140
}
139
141
defer w .Close ()
142
+
143
+ // Decode the frame into an image, save as JPEG
140
144
if image , err := frame .Image (); err != nil {
141
145
return err
142
146
} else if err := jpeg .Encode (w , image , nil ); err != nil {
@@ -145,6 +149,7 @@ func Test_decoder_003(t *testing.T) {
145
149
t .Logf ("Frame %d: %dx%d (%q) => %s" , n , frame .Width (), frame .Height (), frame .PixelFormat (), filename )
146
150
n ++
147
151
}
152
+
148
153
// Stop after 10 frames
149
154
if n >= 10 {
150
155
return io .EOF
@@ -153,6 +158,41 @@ func Test_decoder_003(t *testing.T) {
153
158
}
154
159
}
155
160
156
- // decode frames from the stream
161
+ // Finally, this is where we actually decode frames from the stream
162
+ assert .NoError (decoder .Decode (context .Background (), framefn ))
163
+ }
164
+
165
+ func Test_decoder_004 (t * testing.T ) {
166
+ // Decode audio frames
167
+ assert := assert .New (t )
168
+
169
+ // Open the file
170
+ manager := NewManager ()
171
+ media , err := manager .Open ("./etc/test/sample.mp4" , nil )
172
+ if ! assert .NoError (err ) {
173
+ t .SkipNow ()
174
+ }
175
+ defer media .Close ()
176
+
177
+ // Create a decoder to decompress the audio
178
+ decoder , err := media .Decoder (func (stream Stream ) (Parameters , error ) {
179
+ // Audio - pass through
180
+ if stream .Type () == AUDIO {
181
+ return stream .Parameters (), nil
182
+ }
183
+ // Ignore other streams
184
+ return nil , nil
185
+ })
186
+ if ! assert .NoError (err ) {
187
+ t .SkipNow ()
188
+ }
189
+
190
+ // This is the function which processes the audio frames
191
+ framefn := func (frame Frame ) error {
192
+ t .Log (frame )
193
+ return nil
194
+ }
195
+
196
+ // Finally, this is where we actually decode frames from the stream
157
197
assert .NoError (decoder .Decode (context .Background (), framefn ))
158
198
}
0 commit comments