首页 百科大全文章正文

【OpenCV C++ 实战】RTSP 流处理:实时播放与逐帧保存终极指南

百科大全 2025年09月09日 16:30 1 admin

效果

【OpenCV C++ 实战】RTSP 流处理:实时播放与逐帧保存终极指南

项目

【OpenCV C++ 实战】RTSP 流处理:实时播放与逐帧保存终极指南

代码

 














// 线程安全的帧队列
class FrameQueue {
private:
std::queue
std::mutex mtx;
std::condition_variable cond;
bool stop_flag = false;

public:

void push(const cv::Mat& frame, const std::string& filename) {
std::unique_lock
// 深拷贝帧,因为原帧可能会被修改或释放
queue.push(std::make_pair(frame.clone, filename));
cond.notify_one;
}

bool pop(std::pair
std::unique_lock
cond.wait(lock, [this] { return !queue.empty || stop_flag; });

if (stop_flag && queue.empty) {
returnfalse;
}

item = queue.front;
queue.pop;
returntrue;
}

void stop {
std::unique_lock
stop_flag = true;
cond.notify_all;
}

bool empty {
std::unique_lock
return queue.empty;
}
};

// 全局变量
FrameQueue frameQueue;
std::atomic
std::atomic
std::string imgFolder = "img"; // 图片保存文件夹

// 检查文件夹是否存在 (C++14兼容方法)
bool folderExists(const std::string& folderPath) {
struct stat info;
returnstat(folderPath.c_str, &info) == 0 && (info.st_mode & S_IFDIR);
}

// 创建文件夹 (C++14兼容方法)
bool createFolder(const std::string& folderPath) {
_WIN32
return _mkdir(folderPath.c_str) == 0;

return mkdir(folderPath.c_str, 0733) == 0;

}

// 检查并创建图片保存文件夹
bool ensureImageFolderExists {
// 检查文件夹是否存在
if (folderExists(imgFolder)) {
std::cout "使用图片保存文件夹: "
returntrue;
}

// 如果不存在,尝试创建文件夹
if (createFolder(imgFolder)) {
std::cout "创建图片保存文件夹: "
returntrue;
}

std::cerr "错误: 无法创建图片保存文件夹"
returnfalse;
}

// 保存图像的线程函数
void saveFrameThread {
std::vector
compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);

while (true) {
std::pair
if (!frameQueue.pop(item)) {
break;
}

auto start_time = std::chrono::high_resolution_clock::now;

bool saveResult = cv::imwrite(item.second, item.first);
//bool saveResult = cv::imwrite(item.second, item.first, compression_params);

auto end_time = std::chrono::high_resolution_clock::now;
auto duration = std::chrono::duration_cast

if (!saveResult) {
std::cerr "错误: 无法保存帧: "
}
else {
long long save_time = duration.count;
total_save_time += save_time;
saved_frames_count++;

std::cout "帧已保存: " " (保存时间: " "ms)"
}
}
}

// 保存图像的线程函数
void saveFrameThread2 {
std::vector
compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
compression_params.push_back(100); // 质量

while (true) {
std::pair
if (!frameQueue.pop(item)) {
break;
}

auto start_time = std::chrono::high_resolution_clock::now;

// 编码图像
std::vector
auto encode_start = std::chrono::high_resolution_clock::now;
cv::imencode(".jpg", item.first, buffer, compression_params);
auto encode_end = std::chrono::high_resolution_clock::now;

// 写入文件
auto write_start = std::chrono::high_resolution_clock::now;
std::ofstream ofs(item.second, std::ios::binary);
ofs.write(reinterpret_cast
ofs.close;
auto write_end = std::chrono::high_resolution_clock::now;

auto end_time = std::chrono::high_resolution_clock::now;
auto total_duration = std::chrono::duration_cast
auto encode_duration = std::chrono::duration_cast
auto write_duration = std::chrono::duration_cast

if (!ofs) {
std::cerr "错误: 无法保存帧: "
}
else {
long long total_time = total_duration.count;
long long encode_time = encode_duration.count;
long long write_time = write_duration.count;
total_save_time += total_time;
saved_frames_count++;

std::cout "帧已保存: "
" (总时间: " "ms, 编码: " "ms, 写入: " "ms)"
}
}
}

// 显示使用说明
void printUsage(const std::string& programName) {
std::cout "使用方法: " "
std::cout "示例: " " rtsp://username:password@192.168.1.100:554/stream"
std::cout "注意: 如果URL中包含特殊字符(如&),请将整个URL用双引号括起来"
std::cout
std::cout "控制命令:"
std::cout " q/Q - 退出程序"
std::cout " 空格键 - 暂停/继续播放"
std::cout " s/S - 显示保存统计信息"
std::cout " c/C - 清零统计信息"
}

int main(int argc, char* argv[]) {

// 检查命令行参数
if (argc != 2) {
std::cerr "错误: 需要提供RTSP URL作为参数"
printUsage(argv[0]);
return -1;
}

// 从命令行参数获取RTSP URL
std::string rtspUrl = argv[1];
std::cout "使用RTSP URL: "


// 检查并创建图片保存文件夹
if (!ensureImageFolderExists) {
std::cerr "错误: 无法创建或访问图片保存文件夹,程序将退出"
return -1;
}

// 打开视频流
cv::VideoCapture cap(rtspUrl, cv::CAP_FFMPEG);

if (!cap.isOpened) {
std::cerr "错误: 无法打开RTSP流"
return -1;
}

// 获取视频流的基本信息
double fps = cap.get(cv::CAP_PROP_FPS);
int frameWidth = static_cast
int frameHeight = static_cast
std::cout "RTSP流信息: " "x" " at " " FPS"

// 创建保存线程
std::thread saveThread(saveFrameThread);

// 创建窗口
cv::namedWindow("RTSP Stream", cv::WINDOW_NORMAL);
cv::resizeWindow("RTSP Stream", frameWidth / 4, frameHeight / 4);

// 主循环
cv::Mat frame;
while (true) {
// 读取一帧
if (!cap.read(frame)) {
std::cerr "错误: 读取帧失败"
break;
}

// 显示帧
cv::imshow("RTSP Stream", frame);

// 获取当前时间(精确到毫秒)
auto now = std::chrono::system_clock::now;
auto now_ms = std::chrono::time_point_cast
auto epoch = now_ms.time_since_epoch;
auto value = std::chrono::duration_cast
long long milliseconds = value.count;

// 将毫秒时间戳转换为可读格式
std::time_t time_t_now = std::chrono::system_clock::to_time_t(now);
std::tm tm_struct;
localtime_s(&tm_struct, &time_t_now);
std::ostringstream oss;
oss "%Y%m%d_%H%M%S_") '0')
std::string timestampStr = oss.str;

// 构建保存图像的文件名(包含img文件夹路径)
std::string filename = imgFolder + "/frame_" + timestampStr + ".bmp";

// 将帧和文件名添加到队列(由保存线程处理)
frameQueue.push(frame, filename);

// 处理键盘输入
int key = cv::waitKey(1) & 0xFF;
if (key == 'q' || key == 'Q') {
break;
}
elseif (key == ' ') {
while (true) {
int innerKey = cv::waitKey(0) & 0xFF;
if (innerKey == ' ') {
break;
}
elseif (innerKey == 'q' || innerKey == 'Q') {
cv::destroyAllWindows;
cap.release;
frameQueue.stop;
saveThread.join;
return 0;
}
}
}
elseif (key == 's' || key == 'S') {
// 显示统计信息
if (saved_frames_count > 0) {
double avg_save_time = static_cast
std::cout "保存统计: " " 帧, 平均保存时间: ""ms"
}
}
elseif (key == 'c' || key == 'C') {
// 清空统计信息
total_save_time = 0;
saved_frames_count = 0;
std::cout "统计信息已清零"
}
}

// 释放资源
cap.release;
cv::destroyAllWindows;

// 停止保存线程并等待结束
frameQueue.stop;
saveThread.join;

// 显示最终统计信息
if (saved_frames_count > 0) {
double avg_save_time = static_cast
std::cout "最终统计: " " 帧已保存, 平均保存时间: ""ms"
}

return 0;
}

发表评论

泰日号Copyright Your WebSite.Some Rights Reserved. 网站地图 备案号:川ICP备66666666号 Z-BlogPHP强力驱动