Added frame data SetBytes() and FromImage()

This commit is contained in:
Quentin Renard
2024-10-12 11:28:08 +02:00
parent 3e7aa4e83f
commit a5fe16a531
8 changed files with 444 additions and 93 deletions

View File

@@ -4,7 +4,6 @@ import (
"errors"
"flag"
"fmt"
"image"
"log"
"strings"
@@ -107,7 +106,6 @@ func main() {
}
// Loop through packets
var i image.Image
for {
// Read frame
if err := inputFormatContext.ReadFrame(pkt); err != nil {
@@ -138,35 +136,8 @@ func main() {
log.Fatal(fmt.Errorf("main: receiving frame failed: %w", err))
}
// Do something with decoded frame
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())
}
// Log
log.Printf("new %s frame: stream %d - pts: %d", s.inputStream.CodecParameters().MediaType(), pkt.StreamIndex(), f.Pts())
}
}