⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cutfile.cpp

📁 播放H.264压缩算法文件(C++源程序)
💻 CPP
字号:
// CutFile.cpp : implementation file
//

#include "stdafx.h"
#include "player.h"
#include "CutFile.h"
#include"hikplaympeg4.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern UINT PORT;
/////////////////////////////////////////////////////////////////////////////
// CCutFile dialog


CCutFile::CCutFile(CWnd* pParent /*=NULL*/)
	: CDialog(CCutFile::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCutFile)
	m_nBegin = 0;
	m_nType = 0;
	m_nEnd = 0;
	m_csSaveFileName = _T("D:\\Clip.mp4");
	//}}AFX_DATA_INIT
}


void CCutFile::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCutFile)
	DDX_Text(pDX, IDC_BEGIN, m_nBegin);
	DDX_Radio(pDX, IDC_BYFRAME, m_nType);
	DDX_Text(pDX, IDC_END, m_nEnd);
	DDX_Text(pDX, IDC_FILENAME, m_csSaveFileName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCutFile, CDialog)
	//{{AFX_MSG_MAP(CCutFile)
	ON_BN_CLICKED(IDC_VIEW, OnView)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCutFile message handlers

void CCutFile::OnView() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(!CheckValue())
	{
		MessageBox("Input value over");
		return ;
	}
	
	DWORD nType;
	ZeroMemory(&m_strRealBegin,sizeof(FRAME_POS));
	ZeroMemory(&m_strRealBegin,sizeof(FRAME_POS));
	if(m_nType==0)
		nType=BY_FRAMENUM;
	else
	{
		nType=BY_FRAMETIME;
		//conver to ms
		m_nBegin*=1000;
		m_nEnd*=1000;
	}
	
	//locate the I-Frame .
	Hik_PlayM4_GetKeyFramePos(PORT,m_nBegin,nType,&m_strRealBegin);
	
	if(!Hik_PlayM4_GetNextKeyFramePos(PORT,m_nEnd,nType,&m_strRealEnd))
	{
		m_strRealEnd.nFilePos=SetFilePointer(m_hPlayFile,0,0,FILE_END);
		m_strRealEnd.nFrameNum=m_nMaxFrameNum;
		m_strRealEnd.nFrameTime=m_nMaxTime*1000;
	}
	DWORD nBegin=0,nEnd=0;
	if(nType==BY_FRAMENUM)
	{
		nBegin = m_strRealBegin.nFrameNum;
		nEnd = m_strRealEnd.nFrameNum;
	}
	else if(nType==BY_FRAMETIME)
	{
		nBegin = m_strRealBegin.nFrameTime/1000;
		nEnd = m_strRealEnd.nFrameTime/1000;
	}
	else
		MessageBox("Type is unknown");

	CString csReal;
	csReal.Format("%d",nBegin);
	GetDlgItem(IDC_REAL_BEGIN)->SetWindowText(csReal);
	csReal.Format("%d",nEnd);
	GetDlgItem(IDC_REAL_END)->SetWindowText(csReal);
}

void CCutFile::OnSave() 
{
	// TODO: Add your control notification handler code here
	OnView();
	int nNewFileLen=int(m_strRealEnd.nFilePos-m_strRealBegin.nFilePos);
	if(nNewFileLen<=0)
	{
		MessageBox("Stop position less than start position");
		return ;
	}
	//UpdateData(TRUE);
	HANDLE hNewFile=CreateFile(
		m_csSaveFileName,
		GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL,
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if(m_hPlayFile==INVALID_HANDLE_VALUE)
	{
		CString csErrorName;
		csErrorName.Format("Create file:%s Faild",m_csSaveFileName);
		MessageBox(csErrorName);
		return;
	}
	//Copy file
	DWORD nSize=2048;//Per 2k data;
	PBYTE pBuf=new BYTE [nSize];
	DWORD nFileHeadLen=Hik_PlayM4_GetFileHeadLength();
	DWORD nRet;
	DWORD nBlock=nNewFileLen/nSize;
	DWORD nRemain=nNewFileLen%nSize;
	DWORD nCount;
	//file header
	SetFilePointer(m_hPlayFile,0,0,FILE_BEGIN);
	if(!ReadFile(m_hPlayFile,pBuf,nFileHeadLen,&nRet,NULL))
	{
		MessageBox("Read file faild");
		goto end;
	}
	else
	{
		if(!WriteFile(hNewFile,pBuf,nFileHeadLen,&nRet,NULL))
		{
			MessageBox("Write file faild");
			goto end;
		}
	}
	SetFilePointer(m_hPlayFile,m_strRealBegin.nFilePos,0,FILE_BEGIN);

	
	for(nCount=0;nCount<nBlock;nCount++)
	{
		if(!ReadFile(m_hPlayFile,pBuf,nSize,&nRet,NULL))
		{
			MessageBox("Read file faild");
			break;
		}
		if(!WriteFile(hNewFile,pBuf,nSize,&nRet,NULL))
		{
			MessageBox("Write file faild");
			break;
		}
	}
	if(!ReadFile(m_hPlayFile,pBuf,nRemain,&nRet,NULL))
	{
		MessageBox("Read file faild");
	}
	else
	{
		if(!WriteFile(hNewFile,pBuf,nRemain,&nRet,NULL))
				MessageBox("Write file faild");
	}
end:
	delete []pBuf;
	CloseHandle(hNewFile);
}

void CCutFile::OnOK() 
{
	// TODO: Add extra validation here
	CloseHandle(m_hPlayFile);
	CDialog::OnOK();
}

BOOL CCutFile::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_nMaxTime=Hik_PlayM4_GetFileTime(PORT);
	m_nMaxFrameNum=	Hik_PlayM4_GetFileTotalFrames(PORT);
	CString csRange;
	csRange.Format("Frame number range:%d~%d\r\nTime range(seconds):%d~%d\r\n",0,m_nMaxFrameNum,0,m_nMaxTime);
	GetDlgItem(IDC_RANGE)->SetWindowText(csRange);


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CCutFile::CheckValue()
{
	DWORD nMaxValue;
	if(m_nType==0)
		nMaxValue=m_nMaxFrameNum;
	else
		nMaxValue=m_nMaxTime;
	if(m_nBegin>nMaxValue||m_nEnd>nMaxValue)
		return FALSE;
	return TRUE;

}

BOOL CCutFile::SetFileName(CString csName)
{
	m_csPlayFileName=csName;
	m_hPlayFile=CreateFile(
		csName,
		GENERIC_READ,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if(m_hPlayFile==INVALID_HANDLE_VALUE)
	{
		MessageBox("Open file faild");
		return FALSE;
	}
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -