https://blog.csdn.net/jiexijihe945/article/details/125928135
RTSP:
"rtspsrc location=rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream latency=4000 ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width=1280, height=720, format=BGRx ! videoconvert ! appsink"
本地MP4:
"filesrc location=clip.mp4 ! qtdemux ! h264parse ! omxh264dec ! nvvidconv ! appsink"
USB摄像头:
"v4l2src device=/dev/video0 ! video/x-raw, width=1280, height=720 ! videoconvert ! appsink"
可以提前用 gst-launch-1.0 测试上面的pipeline是否有问题,例如测试 rtsp
gst-launch-1.0 rtspsrc location=rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream latency=4000 ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width=1280, height=720, format=BGRx ! videoconvert ! appsink
文章来源:https://www.toymoban.com/news/detail-715134.html
c++ / opencv / gstreamer文章来源地址https://www.toymoban.com/news/detail-715134.html
#include <iostream>
#include <thread>
#include <opencv2/opencv.hpp>
void startCamera() {
cv::VideoCapture cap;
cap.open("clip.mp4");
while (true) {
cv::Mat frame;
//方法一:>>析取器
cap >> frame; //每个循环从cap中解析一帧,赋给frame,
if (frame.empty()) {
break;
}
//cv::imshow("frame", frame);
//cv::waitKey(1);
std::cout<<"frame :"<<frame.cols<<" "<<frame.rows<<std::endl;
}
cap.release();
}
void startGStream(std::string gst_src) {
cv::VideoCapture cap;
// "rtspsrc location=rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream latency=4000 ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width=1280, height=720, format=BGRx ! videoconvert ! appsink"
// "filesrc location=clip.mp4 ! qtdemux ! h264parse ! omxh264dec ! nvvidconv ! appsink"
// "v4l2src device=/dev/video0 ! video/x-raw, width=1280, height=720 ! videoconvert ! appsink"
cap.open(gst_src, cv::CAP_GSTREAMER);
while (true) {
cv::Mat frame;
//方法一:>>析取器
cap >> frame; //每个循环从cap中解析一帧,赋给frame,
if (frame.empty()) {
break;
}
//cv::imshow("frame", frame);
//cv::waitKey(1);
std::cout<<"frame :"<<frame.cols<<" "<<frame.rows<<std::endl;
}
cap.release();
}
// "rtspsrc location=rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream latency=4000 ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width=1280, height=720, format=BGRx ! videoconvert ! appsink"
// "filesrc location=clip.mp4 ! qtdemux ! h264parse ! omxh264dec ! nvvidconv ! appsink"
// "v4l2src device=/dev/video0 ! video/x-raw, width=1280, height=720 ! videoconvert ! appsink"
std::string get_rtsp_h264_gst(std::string rtsp_uri, int width, int height, int latency)
{
std::string gst_str = "rtspsrc location=" + rtsp_uri+ " latency="+ std::to_string(latency)+ " ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width="+std::to_string(width)+", height="+std::to_string(height)+", format=BGRx ! videoconvert ! appsink";
std::cout<<"gst:"<<gst_str<<":"<<std::endl;
return gst_str;
}
std::string get_rtsp_h265_gst(std::string rtsp_uri, int width, int height, int latency)
{
std::string gst_str = "rtspsrc location=" + rtsp_uri+ " latency="+ std::to_string(latency)+ " ! rtph265depay ! h265parse ! omxh265dec ! nvvidconv ! video/x-raw, width="+std::to_string(width)+", height="+std::to_string(height)+", format=BGRx ! videoconvert ! appsink";
std::cout<<"gst:"<<gst_str<<":"<<std::endl;
return gst_str;
}
std::string get_mp4_h264_gst(std::string file_name, int width, int height)
{
std::string gst_str = "filesrc location=" + file_name+ " ! qtdemux ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width="+std::to_string(width)+", height="+std::to_string(height)+", format=BGRx ! videoconvert ! appsink";
std::cout<<"gst:"<<gst_str<<":"<<std::endl;
return gst_str;
}
std::string get_v4l2_gst(std::string device_id, int width, int height)
{
std::string gst_str = "v4l2src device=" + device_id+ " ! video/x-raw, width="+std::to_string(width)+", height="+std::to_string(height)+", format=BGRx ! videoconvert ! appsink";
std::cout<<"gst:"<<gst_str<<":"<<std::endl;
return gst_str;
}
void startGStream(std::string rtsp_uri, int width, int height, int latency) {
std::string gst_str = get_rtsp_h264_gst(rtsp_uri, width, height, latency);
start:
cv::VideoCapture capture;
capture.open(gst_str, cv::CAP_GSTREAMER);
while (true) {
cv::Mat frame;
//方法一:>>析取器
capture >> frame; //每个循环从cap中解析一帧,赋给frame,
if (frame.empty()) {
break;
}
//cv::imshow("frame", frame);
//cv::waitKey(1);
std::cout<<"frame :"<<frame.cols<<" "<<frame.rows<<std::endl;
}
capture.release();
std::cout<<" ...................................................release "<<std::endl;
goto start;
}
int main(int argc, char** argv){
//std::string gst_src = "rtspsrc location=rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream latency=4000 ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width=1280, height=720, format=BGRx ! videoconvert ! appsink";
// if (argc > 1){
// gst_src = argv[1];
// }
// startGStream(gst_src);
std::string file_src = "rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream";
int width = 1280;
int height = 720;
int latency = 5000;
if (argc > 4){
file_src = argv[1];
width = atoi(argv[2]);
height = atoi(argv[3]);
latency = atoi(argv[4]);
}
startGStream(file_src, width, height, latency);
std::cout<<"finished."<<std::endl;
return 0;
}
cmake_minimum_required(VERSION 2.6)
project(decoder_test)
add_definitions(-std=c++11)
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Release)
find_package(CUDA REQUIRED)
# if (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
# message("embed_platform on")
# include_directories(/usr/local/cuda/targets/aarch64-linux/include)
# link_directories(/usr/local/cuda/targets/aarch64-linux/lib)
# else()
# message("embed_platform off")
# include_directories(/usr/local/cuda/include)
# link_directories(/usr/local/cuda/lib64)
# # tensorrt
# include_directories(/usr/local/TensorRT-7.0.0.11/include)
# link_directories(/usr/local/TensorRT-7.0.0.11/lib)
# endif()
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(decoder_test ${PROJECT_SOURCE_DIR}/main.cpp)
target_link_libraries(decoder_test ${OpenCV_LIBS})
add_definitions(-O3 -Wall -pthread)
到了这里,关于jetson nx 使用opencv和gstreamer 硬解码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!