視頻按照尺寸分類小工具

先上界面:

核心代碼:

/******************************全局變量**********************************/
CString g_strDir;
static CMutex g_Mutex;
static std::map<_tstring, _tstring> g_mLogs;
/************************************************************************/

int CDllTestorDlg::MoveVideoByResolution(const _tstring& strPathInput, const _tstring& strFileOutput, const size_t& nIndex)
{
	MediaInfo MI;  

	if (MI.Open(strPathInput.c_str()) == 0)
	{
		return -1;
	}

	_tstring width = MI.Get(stream_t::Stream_Video,0,_T("Width")).c_str();
	_tstring height = MI.Get(stream_t::Stream_Video,0,_T("Height")).c_str();

	if (width.length() == 0 || height.length() == 0)
	{
		return -1;
	}

	MI.Close(); 

	_tstring strDir = CMfcStrFile::CString2string(g_strDir);
	_tstring strRootDir = CStdStr::AddSlashIfNeeded(strDir);

	if (_access(CStdStr::ws2s(strRootDir).c_str(), 0) != 0 && !CreateDirectory(CString(strRootDir.c_str()), NULL))
	{
		return -1;
	}

	_tstring strYearMonth = width + _T("_") + height;
	_tstring strSaveDir = strRootDir + strYearMonth;
	const _tstring& sStr = strPathInput;

	if (_access(CStdStr::ws2s(strSaveDir).c_str(), 0) != 0 && !CreateDirectory(CString(strSaveDir.c_str()), NULL))
	{
		return -1;
	}

	_tstring strSavePath = CStdStr::AddSlashIfNeeded(strSaveDir) + CStdStr::GetNameOfFile(sStr);
	g_Mutex.Lock();
	int nNum = 0;
	if (CStdFile::IfAccessFile(strSavePath))
	{
		do
		{
			++nNum;
			strSavePath = CStdStr::AddSlashIfNeeded(strSaveDir) + CStdStr::GetNameOfFile(sStr, false) + 
				CStdTpl::ConvertToString(nNum) + CStdStr::GetSuffixOfFile(sStr);

		} while (CStdFile::IfAccessFile(strSavePath));
	}

	if (rename(CStdStr::ws2s(sStr).c_str(), CStdStr::ws2s(strSavePath).c_str()) < 0)
	{
		rename(CStdStr::ws2s(sStr).c_str(), CStdStr::ws2s(strSavePath).c_str());
	}

	g_mLogs.insert(std::make_pair(sStr, strSavePath));
	g_Mutex.Unlock();

	return 0;
}

int CDllTestorDlg::RestoreFile(const _tstring& sStr, const _tstring& sSavePath, const size_t& nProcessIndex)
{
	_tstring strOldPath = g_mLogs[sStr];
	_tstring strOldDir = CStdStr::GetDirOfFile(strOldPath);
	if (_access(CStdStr::ws2s(strOldDir).c_str(), 0) != 0 && !CStdDir::CreateDir(strOldDir))
	{
		return -1;
	}

	if (strOldPath != sStr)
	{
		//路徑不同則移動文件
		rename(CStdStr::ws2s(sStr).c_str(), CStdStr::ws2s(strOldPath).c_str());
	}

	return 0;
}

歡迎交流與分享。

發佈了61 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章