Created
May 31, 2022 12:35
-
-
Save genert/9a4d8a93c09b3a54e81d2d2c8b99924a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/mike1808/h264decoder/decoder" | |
"gocv.io/x/gocv" | |
// "os" | |
"net" | |
) | |
func main() { | |
d, err := decoder.New(decoder.PixelFormatBGR) | |
if err != nil { | |
panic(err) | |
} | |
/*stream, err := os.Open("./artifacts/stream.h264") | |
if err != nil { | |
panic(err) | |
}*/ | |
pc, err := net.ListenPacket("udp4", ":25565") | |
if err != nil { | |
panic(err) | |
} | |
defer pc.Close() | |
window := gocv.NewWindow("H.264 decoder") | |
//buf := make([]byte, 2048) | |
// f, err := os.Create("stream.264") | |
// max := 0 | |
for { | |
buf := make([]byte, 1514) | |
nread, _, err := pc.ReadFrom(buf) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(nread) | |
// if nread > max{ | |
// max = nread | |
// } | |
//fmt.Println("Max: ", max) | |
// n2, err := f.Write(buf[72:nread]) | |
// fmt.Println(n2) | |
// fmt.Println("Dobijeno - zapisano %d", nread-n2) | |
// fmt.Println(buf[:76]) | |
if buf[75] != 1 { | |
fmt.Println("NESTOO", buf[75] ) | |
} | |
if nread > 72{ | |
frames, err := d.Decode(buf[72:nread]) | |
if err != nil { | |
fmt.Println(err) | |
} | |
if len(frames) == 0 { | |
fmt.Println("no frames") | |
} else { | |
for _, frame := range frames { | |
img, _ := gocv.NewMatFromBytes(frame.Height, frame.Width, gocv.MatTypeCV8UC3, frame.Data) | |
if img.Empty() { | |
continue | |
} | |
window.IMShow(img) | |
window.WaitKey(10) | |
} | |
fmt.Println("found %d frames", len(frames)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment