当前位置:网站首页>保存视频 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;
}
}
边栏推荐
- Flink - use the streaming batch API to count the number of words
- Matplotlib swordsman - a stylist who can draw without tools and code
- Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
- 微服务实战|原生态实现服务的发现与调用
- Knife4j 2. Solution to the problem of file control without selection when uploading x version files
- Oracle modify database character set
- Troubleshooting and handling of an online problem caused by redis zadd
- 上班第一天的报错(AWVS卸载不彻底)
- Thinkphp5 how to determine whether a table exists
- 每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
猜你喜欢
随机推荐
Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
C语言之二进制与十进制
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
FragmentTabHost实现房贷计算器界面
hystrix 实现请求合并
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
BugkuCTF-web21(详细解题思路及步骤)
Chrome browser tag management plug-in – onetab
别找了,Chrome浏览器必装插件都在这了
Chrome browser plug-in fatkun installation and introduction
Bold prediction: it will become the core player of 5g
Matplotlib剑客行——初相识Matplotlib
Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
Attributes of classfile
C语言之做木桶
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
《统计学习方法》——第五章、决策树模型与学习(上)
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
[go practical basis] how to install and use gin








