做的实验需要大批量的处理视频,有的时候把视频重命名是很需要的,所以写了一个小算法:
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <windows.h>
#include <sstream>
#include <opencv.hpp>
using namespace std;
using namespace cv;
//函数声明
vector<string> get_filelist(char* foldname);
vector<string> createNewName();
string num2str(int num);
//待命名根目录
string root="D:\\FFOutput\\useful\\";
//注意要确保命名过程中不会有重名的文件,即最好做到待命名文件和指定命名完全不同
int main(int argc,char**argv)
{
//待命名的文件名
char* folder="D:\\FFOutput\\useful\\*.avi";
//获取目录下所有指定类型文件
vector<string> list = get_filelist(folder);
//得到视频新名字
vector<string> new_name = createNewName();
string tmp;
//将文件夹下所有指定文件命名
for(int i=0;i<list.size();++i)
{
//输出原文件名
cout << list[i] << endl;
//这里有两种命名方法
//这种是使用自定义命名方法,具体的名字可以在下面的createNewName()方法中自己定义
tmp = new_name[i];
//若使用下面这种就是简单的数字命名,i可以再加上一个数做偏移量
//tmp = num2str(i)+".avi";
//重命名文件
rename((root+list[i]).c_str(),(root+tmp).c_str());
}
return 0;
}
//获取文件夹下所有指定类型文件的函数,网上down的,主要函数应该是FindNextFile
vector<string> get_filelist(char* foldname)
{
vector<string> flist;
HANDLE file;
WIN32_FIND_DATA fileData;
char line[1024];
wchar_t fn[1000];
mbstowcs(fn,(const char*)foldname,999);
file = FindFirstFile(fn, &fileData);
do{
wcstombs(line,(const wchar_t*)fileData.cFileName,259);
flist.push_back(line);
}while(FindNextFile(file, &fileData));
return flist;
}
//给文件取新名字的函数
vector<string> createNewName()
{
int num = 10; //每类视频的数量
int type = 2; //视频的种类
vector<string> list_clips_name; //存储视频的新名字
vector<string> cliptypes; //存储视频类型名
cliptypes.push_back("normal");
cliptypes.push_back("fighting");
for(int act=0;act<type;act++)
{
for(int clip=0;clip<num;clip++)
{
list_clips_name.push_back("clip_" + cliptypes[act] + "00" + num2str(clip) + ".avi");
}
}
return list_clips_name;
}
//int转string函数
string num2str(int num)
{
//string流,用于将int转为string
stringstream sstr;
//返回值
string str;
sstr << num;
str = sstr.str();
return str;
}
其中get_filelist()这个方法是参照网上现成的code,最近老是觉得贴的代码没有什么技术含量,就没怎么更新,在想下一步怎么写,总是这样肯定是不行的。
Carry on!