Skip to content

Commit fbf4c89

Browse files
diegohcedeadprogram
authored andcommitted
GStreamer VideoWriter example
1 parent dc6fbbe commit fbf4c89

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ build_cuda:
214214
mkdir build
215215
cd build
216216
rm -rf *
217-
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 ..
217+
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 ..
218218
$(MAKE) -j $(shell nproc --all --ignore 1)
219219
$(MAKE) preinstall
220220
cd -

cmd/gstreamer-writer/main.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
GStreamer Video Writer
3+
4+
What it does:
5+
6+
Captures video from webcam and outputs the frames to a gocv VideoWriter
7+
8+
How to run:
9+
10+
go run main.go
11+
12+
To receive the video feed run in a terminal:
13+
14+
gst-launch-1.0 udpsrc port=5000 ! application/x-rtp, encoding-name=MJPG ! rtpjpegdepay ! jpegdec ! videoconvert ! xvimagesink
15+
*/
16+
package main
17+
18+
import (
19+
"fmt"
20+
21+
"gocv.io/x/gocv"
22+
)
23+
24+
const (
25+
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"
26+
)
27+
28+
func main() {
29+
30+
cap, err := gocv.VideoCaptureDevice(0)
31+
if err != nil {
32+
panic(err)
33+
}
34+
defer cap.Close()
35+
cap.Set(gocv.VideoCaptureFrameWidth, 640.0)
36+
cap.Set(gocv.VideoCaptureFrameHeight, 480.0)
37+
38+
wrt, err := gocv.VideoWriterFileWithAPI(gstreamerPipe,
39+
gocv.VideoCaptureGstreamer, "YUY2", 30.0, 640, 480, true)
40+
if err != nil {
41+
panic(err)
42+
}
43+
defer wrt.Close()
44+
45+
if !wrt.IsOpened() {
46+
panic("wrt is not opened")
47+
}
48+
49+
frame := gocv.NewMat()
50+
defer frame.Close()
51+
52+
for {
53+
cap.Read(&frame)
54+
55+
if frame.Empty() {
56+
fmt.Println("empty frame")
57+
continue
58+
}
59+
60+
if err := wrt.Write(frame); err != nil {
61+
fmt.Println(err)
62+
}
63+
64+
}
65+
66+
}

0 commit comments

Comments
 (0)