当前位置:网站首页>保存视频 opencv::VideoWriter
保存视频 opencv::VideoWriter
2022-07-02 06:34:00 【懵懂的梦花火】
// 视频参数
m_videoInfo.fps = m_capture.get(cv::CAP_PROP_FPS);
m_videoInfo.width = m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
m_videoInfo.height = m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
// 初始化VideoWriter
m_mapVideoWriter[_strVideoName] = cv::VideoWriter(_strVideoName,
cv::VideoWriter::fourcc('X', 'V', 'I', 'D'),
m_videoInfo.fps,
cv::Size(m_videoInfo.width, m_videoInfo.height)
);
// 写数据
for (auto videoWriter : m_mapVideoWriter)
{
videoWriter.second << _mat;
}
视频保存结果
while(true)
{
for (auto videoWriter : m_mapVideoWriter)
{
videoWriter.second << _mat;
funtion();
}
}
写数据的操作,运行如下,保存数据正常的前提,funtion()执行一次的时间不能过长(保存图片的频率要满足一定要求,120ms的保存会出错,其它没有测试)。上图video.mp4为正常保存的视频,15_9_36_model_zhiyin.mp4为错误保存的结果,打开会有下图错误。
VideoSaveFunTool文件夹创建,文件名称拼接类
VideoSaveFunTool.h
#pragma once
/*
保存视频中,文件路径处理类
*/
#ifndef VIDEOSAVEFUNTOOL_H
#define VIDEOSAVEFUNTOOL_H
#include <ctime>
#include <string>
namespace UsbCamera
{
class VideoSaveFunTool
{
public:
VideoSaveFunTool();
void GetSaveVidePath(std::string& _strVideoDir);
private:
std::string GetVideoDir();
bool CreateDir(std::string & _strDir);
void UpdateTime();
std::string m_fileDir;
struct tm* m_pTime; // 存储时间,用于保存视频文件
};
}
#endif // !VIDEOSAVEFUNTOOL_H
VideoSaveFunTool.cpp
#include "VideoSaveFunTool.h"
#include "io.h"
#include <ctime>
#include <direct.h>
#include <iostream>
#include "tinyxml2.h"
namespace UsbCamera
{
#define PATH_DELIMITER '\\'
void VideoSaveFunTool::UpdateTime()
{
time_t nowtime;
time(&nowtime);
//使用该函数就可得到当前系统时间,使用该函数需要将传入time_t类型变量nowtime的地址值。
m_pTime = localtime(&nowtime);
m_pTime->tm_year += 1900;
}
bool VideoSaveFunTool::CreateDir(std::string & _strDir)
{
std::string folder_builder;
std::string sub;
sub.reserve(_strDir.size());
for (auto it = _strDir.begin(); it != _strDir.end(); ++it) {
const char c = *it;
sub.push_back(c);
if (c == PATH_DELIMITER || it == _strDir.end() - 1) {
folder_builder.append(sub);
if (0 != ::_access(folder_builder.c_str(), 0)) {
// this folder not exist
if (0 != ::_mkdir(folder_builder.c_str())) {
// create failed
return false;
}
}
sub.clear();
}
}
return true;
}
std::string VideoSaveFunTool::GetVideoDir()
{
// 新建一个空文档
tinyxml2::XMLDocument xmlDoc;
// 读取指定的xml文件并判断读取是否成功
tinyxml2::XMLError eResult = xmlDoc.LoadFile("config/VideoFileConfig.cas");
if (eResult != tinyxml2::XML_SUCCESS)
{
printf("error code :%d\n", tinyxml2::XML_ERROR_FILE_NOT_FOUND);
return "";
}
// 获得该文件的第一个节点(根节点)
tinyxml2::XMLNode * pRoot = xmlDoc.FirstChildElement("VideoFileConfig");
if (pRoot == nullptr)
{
printf("error code :%d\n", tinyxml2::XML_ERROR_FILE_READ_ERROR);
return "";
}
else
{
tinyxml2::XMLElement * pVideoDirElement = pRoot->FirstChildElement("VideoDir");
return pVideoDirElement->GetText();
}
}
VideoSaveFunTool::VideoSaveFunTool()
{
m_fileDir = GetVideoDir();
if (m_fileDir == "")
m_fileDir = "D:\\Video";
}
void VideoSaveFunTool::GetSaveVidePath(std::string& _strVideoDir)
{
struct tm time; // 时间
std::string filePath; // 保存的路径
// 更新时间
UpdateTime();
// 构建存储目录
filePath = m_fileDir + "\\" + std::to_string(m_pTime->tm_year) + "\\" + std::to_string(m_pTime->tm_mon) + "\\"
+ std::to_string(m_pTime->tm_mday);
CreateDir(filePath);
filePath = filePath + "\\" + std::to_string(m_pTime->tm_hour) + "_" + std::to_string(m_pTime->tm_min)
+ "_" + std::to_string(m_pTime->tm_sec);
_strVideoDir = filePath;
}
}
边栏推荐
猜你喜欢
![[go practical basis] how to install and use gin](/img/0d/3e899bf69abf4e8cb7e6a0afa075a9.png)
[go practical basis] how to install and use gin

hystrix 实现请求合并

概念到方法,绝了《统计学习方法》——第三章、k近邻法

西瓜书--第六章.支持向量机(SVM)

Matplotlib swordsman line - layout guide and multi map implementation (Updated)

Learn combinelatest through a practical example

BugkuCTF-web21(详细解题思路及步骤)

Chrome视频下载插件–Video Downloader for Chrome

BugkuCTF-web24(解题思路及步骤)

知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
随机推荐
个人经历&&博客现状
Insight into cloud native | microservices and microservice architecture
告别996,IDEA中必装插件有哪些?
Activity的创建和跳转
Troubleshooting and handling of an online problem caused by redis zadd
图像识别-数据增广
What are the waiting methods of selenium
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
图像识别-数据标注
Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"
Solutions to Chinese garbled code in CMD window
VIM操作命令大全
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
双非本科生进大厂,而我还在底层默默地爬树(上)
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
Demand delineation executive summary
每天睡觉前30分钟阅读_day4_Files
C语言之分草莓