应某用户的需求,编写了这款根据音视频时长分类小工具。
实际效果如下:
显示的是时分秒:
核心代码:
MediaInfo MI;
if (MI.Open(strPathInput.c_str()) == 0)
{
return -1;
}
_tstring stDuration = MI.Get(stream_t::Stream_Audio,0,_T("Duration/String3")).c_str();
if (stDuration.length() == 0)
{
return -1;
}
//去掉小数点后面的
stDuration = CStdStr::GetNameOfFile(stDuration, false);
//合法
stDuration = CStdStr::ToValidFileName(stDuration, _T('-'));
MI.Close();
_tstring strDir = CStdStr::GetDirOfFile(strPathInput);
_tstring strRootDir = CStdStr::AddSlashIfNeeded(strDir);
if (_access(CStdStr::ws2s(strRootDir).c_str(), 0) != 0 && !CreateDirectory(CString(strRootDir.c_str()), NULL))
{
return -1;
}
_tstring stYearMonth = stDuration;
_tstring stSaveDir = strRootDir + stYearMonth;
const _tstring& sStr = strPathInput;
if (_access(CStdStr::ws2s(stSaveDir).c_str(), 0) != 0 && !CreateDirectory(CString(stSaveDir.c_str()), NULL))
{
return -1;
}
const _tstring stSaveDirBak(stSaveDir);
//如果当前目录已经存在超过nMaxCount个文件,则需要新建目录
int nDirIndex = 0;
std::vector<_tstring> vFilesExisted;
while (g_nMaxCount > 0 && getFiles(stSaveDir, vFilesExisted, "*") >= g_nMaxCount)
{
++nDirIndex;
_tstring stSaveDirNew = stSaveDirBak + _T("-") + CStdTpl::ConvertToString(nDirIndex);
if (_access(CStdStr::ws2s(stSaveDirNew).c_str(), 0) != 0 && !CreateDirectory(CString(stSaveDirNew.c_str()), NULL))
{
return -1;
}
vFilesExisted.clear();
stSaveDir = stSaveDirNew;
}
_tstring strSavePath = CStdStr::AddSlashIfNeeded(stSaveDir) + CStdStr::GetNameOfFile(sStr);
g_Mutex.Lock();
int nNum = 0;
if (CStdFile::IfAccessFile(strSavePath))
{
do
{
++nNum;
strSavePath = CStdStr::AddSlashIfNeeded(stSaveDir) + CStdStr::GetNameOfFile(sStr, false) +
CStdTpl::ConvertToString(nNum) + CStdStr::GetSuffixOfFile(sStr);
} while (CStdFile::IfAccessFile(strSavePath));
}
//查看同目录下,是否存在mp3, mp4, srt文件
MoveDefFile(sStr, strSavePath, _T(".mp3"));
MoveDefFile(sStr, strSavePath, _T(".mp4"));
MoveDefFile(sStr, strSavePath, _T(".srt"));
g_Mutex.Unlock();
return 0;
}
即可实现,移动到指定目录,上述代码还实现了同名srt等移动到相同目录,这也是因为用户的需求。
欢迎交流与讨论。