Skip to content

Instantly share code, notes, and snippets.

@drabaioli
Created December 7, 2021 19:29
Show Gist options
  • Save drabaioli/0bef3c3a1cdd91d9f9c86d699e9a0bb8 to your computer and use it in GitHub Desktop.
Save drabaioli/0bef3c3a1cdd91d9f9c86d699e9a0bb8 to your computer and use it in GitHub Desktop.
Create an RTP stream from a webcam with OpenCV and GStreamer
#include <opencv2/opencv.hpp>
int main( int argc, char ** argv )
{
cv::VideoCapture cap;
cv::Mat frame;
const std::string windowName{ "RTP Stream" };
cap.open( 0 );
cap.set( cv::CAP_PROP_FRAME_WIDTH, 640 );
cap.set( cv::CAP_PROP_FRAME_HEIGHT, 480 );
cv::VideoWriter outputVideo( "appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000",
cv::CAP_GSTREAMER,
0,
30,
cv::Size( 640, 480 ) );
for( cap >> frame; !frame.empty(); cap >> frame )
{
cv::imshow( windowName, frame );
outputVideo.write( frame );
char key = cv::waitKey( 1 );
if( key == 'q' || key == 27 )
break;
}
cv::destroyWindow( windowName );
}
// open with:
// gst-launch-1.0 -v udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! xvimagesink sync=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment