因其作業需要使用C++讀取rtsp流,本文將針對linux下c++版讀取視頻流保存視頻,凌亂且各種錯誤,現將將其成功執行的流程和代碼記錄,順帶也寫了一份python讀取rtsp方法,供自己使用及有需之人參考,本文分三部分,第一部分呈現CMakeLists.txt與rtsp_video原始碼;第二部分插圖給出使用方法;第三部分順帶給出python方法的原始碼,
一.CMakeLists.txt與rtsp_video原始碼
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(rtsp)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(rtsp_demo rtsp_video.cpp)
target_link_libraries( rtsp_demo ${OpenCV_LIBS} )
rtsp_video.cpp
#include <iostream>
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main() {
std::string rtspPath = "rtsp://admin:[email protected]:554";
VideoCapture cap;
cap.open(rtspPath);
if (!cap.isOpened())
{
cout << "cannot open video!" << endl;
return 0;
}
VideoWriter writer = VideoWriter("./rtsp-video.avi",//path and filename
(int)cap.get(CAP_PROP_FOURCC),
(int)cap.get(CAP_PROP_FPS),
Size((int)cap.get(CAP_PROP_FRAME_WIDTH),
(int)cap.get(CAP_PROP_FRAME_HEIGHT)),
true//colorfull pic
);
if (!writer.isOpened()) {
cout << "create vedio failed!" << endl;
return 0;
}
while (1)
{
cv::Mat frame;
cap >> frame;
if (frame.empty())
{
cout << "frame is empty!" << endl;
break;
}
cv::Mat img = frame;
//cv::imshow("frame", img);
writer<<img;
//waitKey(1);
}
return 0;
}
注:opencv安裝參考網路方法
二.使用方法
步驟1:
打開rtsp_video.cpp檔案修改流介面:
std::string rtspPath = "rtsp://admin:[email protected]:554";

步驟2:
將整個檔案夾傳入需編譯服務器,檔案內容包括紅框內容:

以下類似編譯程序
步驟3:執行
cmake .
make
可出現rtsp_demo,以下紅框

無需錄視頻,執行 ctrl+c 按鍵
保存視頻為rtsp-video.avi
三.python方法的原始碼
import cv2 cap = cv2.VideoCapture('rtsp://admin:[email protected]:554') fourcc = cv2.VideoWriter_fourcc(*'XVID') size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter('./rtsp_video.avi', fourcc,10.0, size) while True: ret, frame = cap.read() out.write(frame) cv2.imshow('frame', frame) cv2.waitKey(1) cap.release() out.release() cv2.destroyAllWindows()
處理演算法通用的輔助的code,如讀取txt檔案,讀取xml檔案,將xml檔案轉換成txt檔案,讀取json檔案等
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/502715.html
標籤:其他
上一篇:1 分鐘在 Serverless 上部署現代化 Deno Web 應用
下一篇:SMO論文中文翻譯
