当前位置:网站首页>保存视频 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;
}
}
边栏推荐
- Micro service practice | introduction and practice of zuul, a micro service gateway
- 每天睡觉前30分钟阅读_day3_Files
- Thinkphp5 how to determine whether a table exists
- 图像识别-数据增广
- [go practical basis] how can gin get the request parameters of get and post
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- JDBC回顾
- 微服务实战|熔断器Hystrix初体验
- C语言之分草莓
- hystrix 实现请求合并
猜你喜欢
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
JDBC回顾
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
Chrome视频下载插件–Video Downloader for Chrome
Chrome user script manager tempermonkey monkey
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
西瓜书--第五章.神经网络
图像识别-数据增广
Solutions to Chinese garbled code in CMD window
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
随机推荐
大学生四六级作文模板(自创版,成功跨过六级)
西瓜书--第六章.支持向量机(SVM)
Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
MySql报错:unblock with mysqladmin flush-hosts
Oracle modify database character set
Safety production early warning system software - Download safety production app software
C语言之到底是不是太胖了
Matplotlib剑客行——布局指南与多图实现(更新)
[go practical basis] how to customize and use a middleware in gin
CKEditor 4.10.1 上传图片提示“不正确的服务器响应” 问题解决
C语言之分草莓
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Matplotlib swordsman line - layout guide and multi map implementation (Updated)
图像识别-数据清洗
别找了,Chrome浏览器必装插件都在这了
MySQL multi column in operation
Typeerror: X () got multiple values for argument 'y‘
c语言编程题
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue