@@ -104,7 +104,7 @@ func main() {
104
104
}
105
105
```
106
106
107
- ### Decoding - Video Frames
107
+ ### Decoding - Video
108
108
109
109
This example shows you how to decode video frames from a media file into images.
110
110
@@ -156,9 +156,75 @@ func main() {
156
156
}
157
157
```
158
158
159
- ### Encoding
159
+ ### Encoding - Audio and Video
160
160
161
- TODO
161
+ This example shows you how to encode video and audio frames into a media file.
162
+
163
+ ``` go
164
+ package main
165
+
166
+ import (
167
+ " io"
168
+ " log"
169
+ " os"
170
+ " time"
171
+
172
+ media " github.com/mutablelogic/go-media"
173
+ ffmpeg " github.com/mutablelogic/go-media/pkg/ffmpeg"
174
+ generator " github.com/mutablelogic/go-media/pkg/generator"
175
+ ff " github.com/mutablelogic/go-media/sys/ffmpeg61"
176
+ )
177
+
178
+ func main () {
179
+ // Create a new file with an audio and video stream
180
+ // 30fps and 22050Hz mono audio
181
+ file , err := ffmpeg.Create (os.Args [1 ],
182
+ ffmpeg.OptStream (1 , ffmpeg.VideoPar (" yuv420p" , " 640x480" , 30 )),
183
+ ffmpeg.OptStream (2 , ffmpeg.AudioPar (" fltp" , " mono" , 22050 )),
184
+ )
185
+ if err != nil {
186
+ log.Fatal (err)
187
+ }
188
+ defer file.Close ()
189
+
190
+ // Make an video generator which can generate YUV420P frames
191
+ // with the same parameters as the video stream
192
+ video , err := generator.NewYUV420P (file.Stream (1 ).Par ())
193
+ if err != nil {
194
+ log.Fatal (err)
195
+ }
196
+ defer video.Close ()
197
+
198
+ // Make an audio generator which can generate a 440Hz tone
199
+ // at -5dB with the same parameters as the audio stream
200
+ audio , err := generator.NewSine (440 , -5 , file.Stream (2 ).Par ())
201
+ if err != nil {
202
+ log.Fatal (err)
203
+ }
204
+ defer audio.Close ()
205
+
206
+ // Write 1 min of frames, passing video and audio frames to the encoder
207
+ // and returning io.EOF when the duration is reached
208
+ duration := time.Minute
209
+ if err := file.Encode (func (stream int ) (*ff.AVFrame , error ) {
210
+ var frame media.Frame
211
+ switch stream {
212
+ case 1 :
213
+ frame = video.Frame ()
214
+ case 2 :
215
+ frame = audio.Frame ()
216
+ }
217
+ if frame.Time () >= duration {
218
+ return nil , io.EOF
219
+ } else {
220
+ log.Println (" Frame" , stream, " =>" , frame.Time ().Truncate (time.Millisecond ))
221
+ return frame.(*ffmpeg.Frame ).AVFrame (), nil
222
+ }
223
+ }, nil ); err != nil {
224
+ log.Fatal (err)
225
+ }
226
+ }
227
+ ```
162
228
163
229
### Multiplexing
164
230
0 commit comments