CutFile.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // CutFile.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "StoneU_HC_CARDOCX.h"
  5. #include "CutFile.h"
  6. #include <Shlwapi.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. #pragma comment(lib, "Shlwapi.lib")
  13. extern UINT PORT;
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CCutFile dialog
  16. CCutFile::CCutFile(CWnd* pParent /*=NULL*/)
  17. : CDialog(CCutFile::IDD, pParent)
  18. {
  19. //{{AFX_DATA_INIT(CCutFile)
  20. m_nBegin = 0;
  21. m_nType = 0;
  22. m_nEnd = 0;
  23. m_csSaveFileName = _T("D:\\Clip.mp4");
  24. //}}AFX_DATA_INIT
  25. }
  26. void CCutFile::DoDataExchange(CDataExchange* pDX)
  27. {
  28. CDialog::DoDataExchange(pDX);
  29. //{{AFX_DATA_MAP(CCutFile)
  30. DDX_Text(pDX, IDC_BEGIN, m_nBegin);
  31. DDX_Radio(pDX, IDC_BYFRAME, m_nType);
  32. DDX_Text(pDX, IDC_END, m_nEnd);
  33. DDX_Text(pDX, IDC_FILENAME, m_csSaveFileName);
  34. //}}AFX_DATA_MAP
  35. }
  36. BEGIN_MESSAGE_MAP(CCutFile, CDialog)
  37. //{{AFX_MSG_MAP(CCutFile)
  38. // ON_BN_CLICKED(IDC_VIEW, OnView)
  39. ON_BN_CLICKED(IDC_SAVE, OnSave)
  40. //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CCutFile message handlers
  44. BOOL CCutFile::OnView()
  45. {
  46. // TODO: Add your control notification handler code here
  47. UpdateData(TRUE);
  48. if(!CheckValue())
  49. {
  50. MessageBox("Input value over");
  51. return FALSE;
  52. }
  53. DWORD nType;
  54. ZeroMemory(&m_strRealBegin, sizeof(FRAME_POS));
  55. ZeroMemory(&m_strRealBegin, sizeof(FRAME_POS));
  56. if(m_nType == 0)
  57. nType = BY_FRAMENUM;
  58. else
  59. {
  60. nType = BY_FRAMETIME;
  61. //conver to ms
  62. m_nBegin *= 1000;
  63. m_nEnd *= 1000;
  64. }
  65. //locate the I-Frame .
  66. NAME(PlayM4_GetKeyFramePos)(PORT, m_nBegin, nType, &m_strRealBegin);
  67. if(!NAME(PlayM4_GetNextKeyFramePos)(PORT, m_nEnd, nType, &m_strRealEnd))
  68. {
  69. m_strRealEnd.nFilePos = SetFilePointer(m_hPlayFile, 0, 0, FILE_END);
  70. m_strRealEnd.nFrameNum = m_nMaxFrameNum;
  71. m_strRealEnd.nFrameTime = m_nMaxTime*1000;
  72. }
  73. DWORD nBegin = 0, nEnd = 0;
  74. if(nType == BY_FRAMENUM)
  75. {
  76. nBegin = m_strRealBegin.nFrameNum;
  77. nEnd = m_strRealEnd.nFrameNum;
  78. }
  79. else if(nType == BY_FRAMETIME)
  80. {
  81. nBegin = m_strRealBegin.nFrameTime / 1000;
  82. nEnd = m_strRealEnd.nFrameTime / 1000;
  83. }
  84. else
  85. {
  86. MessageBox("Type is unknown");
  87. return FALSE;
  88. }
  89. CString csReal;
  90. csReal.Format("%d", nBegin);
  91. GetDlgItem(IDC_REAL_BEGIN)->SetWindowText(csReal);
  92. csReal.Format("%d", nEnd);
  93. GetDlgItem(IDC_REAL_END)->SetWindowText(csReal);
  94. return TRUE;
  95. }
  96. void CCutFile::OnSave()
  97. {
  98. // TODO: Add your control notification handler code here
  99. if(!OnView())
  100. return;
  101. int nNewFileLen = int(m_strRealEnd.nFilePos - m_strRealBegin.nFilePos);
  102. if(nNewFileLen <= 0)
  103. {
  104. MessageBox("Stop position less than start position");
  105. return;
  106. }
  107. //UpdateData(TRUE);
  108. //Copy file
  109. HANDLE hNewFile = INVALID_HANDLE_VALUE;
  110. DWORD nSize = 2048; //Per 2k data;
  111. PBYTE pBuf = NULL;
  112. DWORD nFileHeadLen = NAME(PlayM4_GetFileHeadLength)();
  113. DWORD nRet;
  114. DWORD nBlock = nNewFileLen / nSize;
  115. DWORD nRemain = nNewFileLen % nSize;
  116. DWORD nCount;
  117. try
  118. {
  119. if(PathFileExists(m_csSaveFileName))
  120. {
  121. int nVal = MessageBox("The file already exists, are u sure of covering it now?", "Warning", MB_YESNO | MB_ICONQUESTION);
  122. if(nVal == IDNO)
  123. {
  124. throw 0;
  125. }
  126. }
  127. hNewFile = CreateFile(
  128. m_csSaveFileName,
  129. GENERIC_WRITE,
  130. FILE_SHARE_READ | FILE_SHARE_WRITE,
  131. NULL,
  132. CREATE_ALWAYS,
  133. FILE_ATTRIBUTE_NORMAL,
  134. NULL );
  135. if(hNewFile == INVALID_HANDLE_VALUE)
  136. {
  137. MessageBox("Create saving file faild");
  138. throw 0;
  139. }
  140. pBuf = new BYTE [nSize];
  141. if(NULL == pBuf)
  142. {
  143. MessageBox("Alloc memory failed!");
  144. throw 0;
  145. }
  146. //file header
  147. SetFilePointer(m_hPlayFile, 0, 0, FILE_BEGIN);
  148. if(!ReadFile(m_hPlayFile, pBuf, nFileHeadLen, &nRet, NULL))
  149. {
  150. MessageBox("Read file faild!");
  151. throw 0;
  152. }
  153. else
  154. {
  155. if(!WriteFile(hNewFile, pBuf, nFileHeadLen, &nRet, NULL))
  156. {
  157. MessageBox("Write file faild!");
  158. throw 0;
  159. }
  160. }
  161. SetFilePointer(m_hPlayFile, m_strRealBegin.nFilePos, 0, FILE_BEGIN);
  162. for(nCount = 0; nCount < nBlock; nCount ++)
  163. {
  164. if(!ReadFile(m_hPlayFile, pBuf, nSize, &nRet, NULL))
  165. {
  166. MessageBox("Read file faild");
  167. break;
  168. }
  169. if(!WriteFile(hNewFile, pBuf, nSize, &nRet, NULL))
  170. {
  171. MessageBox("Write file faild");
  172. break;
  173. }
  174. }
  175. if(!ReadFile(m_hPlayFile, pBuf, nRemain, &nRet, NULL))
  176. {
  177. MessageBox("Read file faild");
  178. }
  179. else
  180. {
  181. if(!WriteFile(hNewFile, pBuf, nRemain, &nRet, NULL))
  182. MessageBox("Write file faild");
  183. }
  184. }
  185. catch(...)
  186. {
  187. }
  188. if(pBuf != NULL)
  189. {
  190. delete []pBuf;
  191. pBuf = NULL;
  192. }
  193. if(hNewFile != INVALID_HANDLE_VALUE)
  194. {
  195. CloseHandle(hNewFile);
  196. hNewFile = INVALID_HANDLE_VALUE;
  197. }
  198. }
  199. void CCutFile::OnOK()
  200. {
  201. // TODO: Add extra validation here
  202. CloseHandle(m_hPlayFile);
  203. CDialog::OnOK();
  204. }
  205. BOOL CCutFile::OnInitDialog()
  206. {
  207. CDialog::OnInitDialog();
  208. // TODO: Add extra initialization here
  209. m_nMaxTime = NAME(PlayM4_GetFileTime)(PORT);
  210. m_nMaxFrameNum = NAME(PlayM4_GetFileTotalFrames)(PORT);
  211. CString csRange;
  212. csRange.Format("Ö¡ºÅ·¶Î§(Ö¡): %d~%d\r\nʱ¼ä·¶Î§(Ãë): %d~%d\r\n", 0, m_nMaxFrameNum, 0, m_nMaxTime);
  213. GetDlgItem(IDC_RANGE)->SetWindowText(csRange);
  214. return TRUE; // return TRUE unless you set the focus to a control
  215. // EXCEPTION: OCX Property Pages should return FALSE
  216. }
  217. BOOL CCutFile::CheckValue()
  218. {
  219. DWORD nMaxValue;
  220. if(m_nType == 0)
  221. nMaxValue = m_nMaxFrameNum;
  222. else
  223. nMaxValue = m_nMaxTime;
  224. if( (m_nBegin > nMaxValue) || (m_nEnd > nMaxValue) )
  225. return FALSE;
  226. return TRUE;
  227. }
  228. BOOL CCutFile::SetFileName(CString csName)
  229. {
  230. m_csPlayFileName = csName;
  231. m_hPlayFile = CreateFile(
  232. csName,
  233. GENERIC_READ,
  234. FILE_SHARE_READ|FILE_SHARE_WRITE,
  235. NULL,
  236. OPEN_EXISTING,
  237. FILE_ATTRIBUTE_NORMAL,
  238. NULL);
  239. if(m_hPlayFile == INVALID_HANDLE_VALUE)
  240. {
  241. MessageBox("Open file faild");
  242. return FALSE;
  243. }
  244. return TRUE;
  245. }