Added frame data example to demuxing/decoding example

This commit is contained in:
Quentin Renard
2024-05-19 15:51:33 +02:00
parent 1fae94ac8e
commit e0a9bdbd45

View File

@@ -4,6 +4,7 @@ import (
"errors"
"flag"
"fmt"
"image"
"log"
"strings"
@@ -106,6 +107,7 @@ func main() {
}
// Loop through packets
var i image.Image
for {
// Read frame
if err := inputFormatContext.ReadFrame(pkt); err != nil {
@@ -137,7 +139,34 @@ func main() {
}
// Do something with decoded frame
log.Printf("new frame: stream %d - pts: %d", pkt.StreamIndex(), f.Pts())
if s.inputStream.CodecParameters().MediaType() == astiav.MediaTypeVideo {
// In this example, we'll process the frame data but you can do whatever you feel like
// with the decoded frame
fd := f.Data()
// Image has not yet been initialized
// If the image format can change in the stream, you'll need to guess image format for every frame
if i == nil {
// Guess image format
// It might not return an image.Image in the proper format for your use case, in that case
// you can skip this step and provide .ToImage() with your own image.Image
var err error
if i, err = fd.GuessImageFormat(); err != nil {
log.Fatal(fmt.Errorf("main: guessing image format failed: %w", err))
}
}
// Copy frame data to the image
if err := fd.ToImage(i); err != nil {
log.Fatal(fmt.Errorf("main: copying frame data to the image failed: %w", err))
}
// Log
log.Printf("new video frame: stream %d - pts: %d - size: %dx%d - color at (0,0): %+v", pkt.StreamIndex(), f.Pts(), i.Bounds().Dx(), i.Bounds().Dy(), i.At(0, 0))
} else {
// Log
log.Printf("new audio frame: stream %d - pts: %d", pkt.StreamIndex(), f.Pts())
}
}
}