RtspDecoderByFFmpeg/main.cpp

91 lines
2.5 KiB
C++

#include "RTSPDecoder/RTSPDecoder.h"
#include <iostream>
#include <sys/time.h>
#include <opencv2/opencv.hpp>
std::string getDateTime_usec()
{
time_t timep = time(NULL);
struct tm *p = localtime(&timep);
struct timeval tv;
gettimeofday(&tv, NULL);
int msec = tv.tv_usec / 1000;
char tmp[30] = {0};
sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d.%03d", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, msec);
return std::string(tmp);
}
int main() {
RTSPDecoder decoder;
RTSPDecoder::DecoderConfig config;
config.max_streams = 10; // 最大10路流
config.use_hw_accel = true; // 使用硬件加速
config.width = 1920; // 输出宽度
config.height = 1080; // 输出高度
config.buffer_size = 5; // 每路流缓冲5帧
if (!decoder.init(config)) {
std::cerr << "Failed to initialize decoder" << std::endl;
return 1;
}
// 添加RTSP流
std::vector<std::string> rtsp_urls = {
"./1.mp4",
"./1.mp4",
"./1.mp4",
"./1.mp4",
"./1.mp4",
"./1.mp4",
"./1.mp4",
"./1.mp4",
"./1.mp4",
"./1.mp4",
// 添加更多流...
};
std::vector<int> stream_ids;
for (const auto& url : rtsp_urls) {
try {
int stream_id = decoder.addStream(url);
stream_ids.push_back(stream_id);
std::cout << "Added stream " << stream_id << ": " << url << std::endl;
} catch (const std::exception& e) {
std::cerr << "Failed to add stream: " << e.what() << std::endl;
}
}
int iID = 0;
// 主循环
while (true) {
for (int stream_id : stream_ids) {
++iID;
cv::Mat frame;
if (decoder.getFrame(stream_id, frame, 1)) {
// 处理帧...
if (!cv::imwrite("./jpg/Stream_" + std::to_string(stream_id) + "_" + std::to_string(iID) + ".jpg", frame))
{
std::cerr << "Save Failed ./jpg/Stream_" + std::to_string(stream_id) + "_" + std::to_string(iID) + ".jpg" << " size:" << frame.size() << std::endl;
}
std::cout << getDateTime_usec() << " Stream " << stream_id << " -- " << std::to_string(iID) << std::endl;
}
}
if (cv::waitKey(1) == 27) { // ESC键退出
break;
}
}
// 清理
for (int stream_id : stream_ids) {
decoder.removeStream(stream_id);
}
return 0;
}