为什么不用pr,剪映,PotPlayer呢?我手残,我不会
还有用这些去搞一条就15帧,1s不到的视频,真是杀鸡用牛刀
1.opencv读取视频帧并保存
将长视频打散成一帧一帧的
#include <opencv2/opencv.hpp>
#include<iostream>
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格式成功了,其他的没有试
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include<io.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void getAllFiles(string path, vector<string>& files);
int main()
{
int frames = 15;
string DATA_DIR = "C:/Users/wwy/Desktop/111/";
vector<string> files;
char * DistAll = (char*)"AllFiles.txt";
getAllFiles(DATA_DIR, files);
int size = files.size();
cout << "图片一共"<<size <<"张"<< endl;
int videoNum = size / frames;
int MaxFrame = videoNum * (frames-1);
cout << "可以制作" << videoNum << "条视频" << endl;
int video_wight = 480;
int video_hight = 320;
for (int i = 1; i < videoNum + 1; i++) {
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);
cout << path << endl;
Writer.write(src);
}
cout << "保存成功" << endl;
}
Writer.release();
}
waitKey(0);
return 0;
}
void getAllFiles(string path, vector<string>& files)
{
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
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);
_findclose(hFile);
}
}
|