栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

OpenCV(c++)长视频截短

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenCV(c++)长视频截短

OpenCV长视频截短
    • 1.opencv读取视频帧并保存
    • 2.opencv多帧合成视频

为什么不用pr,剪映,PotPlayer呢?我手残,我不会
还有用这些去搞一条就15帧,1s不到的视频,真是杀鸡用牛刀

1.opencv读取视频帧并保存

将长视频打散成一帧一帧的

#include 
#include

using namespace std;
using namespace cv;

int main()
{
	VideoCapture capture("C:/Users/wwy/Desktop/test.mp4");//视频路径
	int i = 0;
	while (1)
	{
		i++;
		Mat img;
		capture >> img;
		if (img.empty()) {
			printf("播放完成n");
			break;
		}
		imshow("res", img);//显示图片
		string ii = std::to_string(i);
		string path = "C:/Users/wwy/Desktop/111/" + ii+ ".jpg";//保存路径
		imwrite(path,img);
		waitKey(1);
	}

	waitKey(0);
	system("path");
	getchar();
	return 0;
}


2.opencv多帧合成视频

把图片合成视频,图片放在文件夹里面,命名格式应该为:1.jpg,2.jpg,3.jpg…
目前只有MP4格式成功了,其他的没有试

// TestForOnlyC++.cpp : 定义控制台应用程序的入口点。
//



#include 
#include 
#include 
#include 
#include 
#include
#include 

using namespace std;
using namespace cv;
void getAllFiles(string path, vector& files);
//测试
int main()
{
	int frames = 15;//一条视频多少帧
	string DATA_DIR = "C:/Users/wwy/Desktop/111/";//图片所在文件夹

	vector files;
	char * DistAll = (char*)"AllFiles.txt";
	getAllFiles(DATA_DIR, files);//所有文件与文件夹的路径都输出
	int size = files.size();
	cout << "图片一共"<
		cv::VideoWriter Writer;
		string i_string= to_string(i);
		string filepath = "C:/Users/wwy/Desktop/111/1/"+i_string+".mp4";
		Writer.open(filepath, VideoWriter::fourcc('M', 'P', '4', '2'), 25, Size(video_wight, video_hight), 1);
		if (!Writer.isOpened())
		{
			cout << "无法保存视频" << endl;
		}
		else {
			for (int cou = frames*i-(frames-1); cou < frames * i+1&& MaxFrame; cou++)
			{
				string ii = to_string(cou);
				string path = DATA_DIR + ii + ".jpg";
				Mat src = imread(path);
				resize(src, src, Size(video_wight, video_hight), 0, 0, INTER_LINEAR);
				//imshow("res", src);//显示图片
				cout << path << endl;
				Writer.write(src);//输出视频
			}
			cout << "保存成功" << endl;
		}
		Writer.release();
	}
	waitKey(0);
	return 0;
}

void getAllFiles(string path, vector& files)
{
	//文件句柄  
	intptr_t hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;  //很少用的文件信息读取结构
	string p;  //string类很有意思的一个赋值函数:assign(),有很多重载版本
	if ((hFile = _findfirst(p.assign(path).append("\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			if ((fileinfo.attrib &  _A_SUBDIR))  //判断是否为文件夹
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
					files.push_back(p.assign(path).append("/").append(fileinfo.name));//保存文件夹名字
					getAllFiles(p.assign(path).append("/").append(fileinfo.name), files);//递归当前文件夹
				}
			}
			else    //文件处理
			{
				files.push_back(p.assign(path).append("/").append(fileinfo.name));//文件名
			}
		} while (_findnext(hFile, &fileinfo) == 0);  //寻找下一个,成功返回0,否则-1
		_findclose(hFile);
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835320.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号