Skip to content

Commit

Permalink
GStreamer VideoWriter example
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohce committed Oct 28, 2024
1 parent dc6fbbe commit 63ad366
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ build_cuda:
mkdir build
cd build
rm -rf *
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} -D OPENCV_EXTRA_MODULES_PATH=$(TMP_DIR)opencv/opencv_contrib-$(OPENCV_VERSION)/modules -D BUILD_DOCS=OFF -D BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=ON -D BUILD_opencv_java=NO -D BUILD_opencv_python=NO -D BUILD_opencv_python2=NO -D BUILD_opencv_python3=NO -D WITH_JASPER=OFF -D WITH_TBB=ON -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_CUDA=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=ON -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda/ -DBUILD_opencv_cudacodec=OFF -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D CUDA_GENERATION=Auto -DOPENCV_ENABLE_NONFREE=ON -D WITH_GSTREAMER=OFF ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} -D OPENCV_EXTRA_MODULES_PATH=$(TMP_DIR)opencv/opencv_contrib-$(OPENCV_VERSION)/modules -D BUILD_DOCS=OFF -D BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=ON -D BUILD_opencv_java=NO -D BUILD_opencv_python=NO -D BUILD_opencv_python2=NO -D BUILD_opencv_python3=NO -D WITH_JASPER=OFF -D WITH_TBB=ON -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_CUDA=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=ON -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda/ -DBUILD_opencv_cudacodec=OFF -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D CUDA_GENERATION=Auto -DOPENCV_ENABLE_NONFREE=ON -D WITH_GSTREAMER=ON ..
$(MAKE) -j $(shell nproc --all --ignore 1)
$(MAKE) preinstall
cd -
Expand Down
66 changes: 66 additions & 0 deletions cmd/gstreamer-writer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
GStreamer Video Writer
What it does:
Captures video from webcam and outputs the frames to a gocv VideoWriter
How to run:
go run main.go
To receive the video feed run in a terminal:
gst-launch-1.0 udpsrc port=5000 ! application/x-rtp, encoding-name=MJPG ! rtpjpegdepay ! jpegdec ! videoconvert ! xvimagesink
*/
package main

import (
"fmt"

"gocv.io/x/gocv"
)

const (
gstreamerPipe = "appsrc ! videoconvert ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! jpegenc ! rtpjpegpay ! udpsink host=127.0.0.1 port=5000"
)

func main() {

cap, err := gocv.VideoCaptureDevice(0)
if err != nil {
panic(err)
}
defer cap.Close()
cap.Set(gocv.VideoCaptureFrameWidth, 640.0)
cap.Set(gocv.VideoCaptureFrameHeight, 480.0)

wrt, err := gocv.VideoWriterFileWithAPI(gstreamerPipe,
gocv.VideoCaptureGstreamer, "YUY2", 30.0, 640, 480, true)
if err != nil {
panic(err)
}
defer wrt.Close()

if !wrt.IsOpened() {
panic("wrt is not opened")
}

frame := gocv.NewMat()
defer frame.Close()

for {
cap.Read(&frame)

if frame.Empty() {
fmt.Println("empty frame")
continue
}

if err := wrt.Write(frame); err != nil {
fmt.Println(err)
}

}

}

0 comments on commit 63ad366

Please sign in to comment.