yapingcat / gomedia

golang library for rtmp, mpeg-ts,mpeg-ps,flv,mp4,ogg,rtsp
MIT License
398 stars 68 forks source link

RTMP chromakey #45

Closed hasnhasan closed 1 year ago

hasnhasan commented 1 year ago

How can I apply chromakey to data coming from an RTMP packet and add another video to the transparent area? Can you please help? I can't find any resources

for {
    // Rtmp stream read packet
    pkt, err := c.ReadPacket()
    switch pkt.Type {
        case av.H264:
            naluSize := binary.BigEndian.Uint32(pkt.Data[:4])
            img := pkt.Data[4+naluSize:]
            //????
            newImg := image.NewRGBA(img.Bounds())
            backgroundColor, _ := colorful.Hex("#72971E") // yeşil renk
            usingPix(img, *newImg, backgroundColor)
    }
}

To apply to the frame;

func usingPix(img image.Image, newImg image.RGBA, targetColor colorful.Color) {

    bounds := img.Bounds()
    width, height := bounds.Max.X, bounds.Max.Y

    rgba := image.NewRGBA(bounds)
    draw.Draw(rgba, bounds, img, bounds.Min, draw.Src)
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            index := (y*width + x) * 4
            pix := rgba.Pix[index : index+4]
            clr := color.RGBA{pix[0], pix[1], pix[2], 255}
            clra,_ := colorful.MakeColor(clr)
            a := targetColor.DistanceCIE94(clra)

            if a < 0.11{
                newImg.Set(x, y, color.RGBA{0, 0, 0, 0})
            } else {
                newImg.Set(x, y, clr)
            }
        }
    }

}
yapingcat commented 1 year ago

sorry I don't know anything about image processing. in this case, i think you should use ffmpeg to transcoding h264 into jpeg. or other picture format