当前位置:网站首页>保存视频 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;
}
}
边栏推荐
- "Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
- 一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
- Matplotlib剑客行——布局指南与多图实现(更新)
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- C语言之最小数
- FragmentTabHost实现房贷计算器界面
- Demand delineation executive summary
- What are the waiting methods of selenium
- Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
- Redis installation and deployment (windows/linux)
猜你喜欢

DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!

MySQL事务

From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method

Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)

Navicat 远程连接Mysql报错1045 - Access denied for user ‘root‘@‘222.173.220.236‘ (using password: YES)

洞见云原生|微服务及微服务架构浅析

盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘

西瓜书--第五章.神经网络

深入剖析JVM是如何执行Hello World的

Ora-12514 problem solving method
随机推荐
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
道阻且长,行则将至
Matplotlib剑客行——初相识Matplotlib
Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
Creation and jump of activity
Solution to amq4036 error in remote connection to IBM MQ
[go practical basis] how to install and use gin
Watermelon book -- Chapter 6 Support vector machine (SVM)
What is the function of laravel facade
Chrome browser plug-in fatkun installation and introduction
Chrome用户脚本管理器-Tampermonkey 油猴
自定义Redis连接池
西瓜书--第五章.神经网络
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
Don't look for it. All the necessary plug-ins for Chrome browser are here
Redis installation and deployment (windows/linux)
Customize redis connection pool
定时线程池实现请求合并
Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"
MySQL multi column in operation