mirror of
https://github.com/asticode/go-astiav.git
synced 2025-10-06 08:37:06 +08:00
Added frame data example to demuxing/decoding example
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -106,6 +107,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Loop through packets
|
// Loop through packets
|
||||||
|
var i image.Image
|
||||||
for {
|
for {
|
||||||
// Read frame
|
// Read frame
|
||||||
if err := inputFormatContext.ReadFrame(pkt); err != nil {
|
if err := inputFormatContext.ReadFrame(pkt); err != nil {
|
||||||
@@ -137,7 +139,34 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Do something with decoded frame
|
// 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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user