jetson nx 使用opencv和gstreamer 硬解码

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

 

c++ / opencv  / gstreamer

#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)

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NineDays66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值