星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ZVideoRecordHelper.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Accord.Video;
  4. using Accord.Video.FFMPEG;
  5. namespace XHWK.WKTool.Utils
  6. {
  7. public class ZVideoRecordHelper
  8. {
  9. public enum RecordState
  10. {
  11. stop = 0,
  12. start = 1,
  13. pause = 2
  14. }
  15. RecordState _state = RecordState.stop;
  16. string _filePath = "";
  17. int _width = 0;
  18. int _height = 0;
  19. //录制帧率
  20. int _rate = 5;
  21. //录制质量
  22. int _quality = 8;
  23. bool _captureMouse = false;
  24. private VideoFileWriter videoWriter = new VideoFileWriter();//视频写入
  25. private ScreenCaptureStream videoStreamer;//视频捕获
  26. public ZVideoRecordHelper(string filePath, int width,int height,int rate = 5,int quality=8, bool captureMouse = false)
  27. {
  28. _filePath = filePath;
  29. _width = width / 2 * 2;
  30. _height = height / 2 * 2;
  31. _rate = rate;
  32. _quality = quality;
  33. _captureMouse = captureMouse;
  34. }
  35. /// <summary>
  36. /// 开始录制
  37. /// </summary>
  38. public bool StartRecordVideo()
  39. {
  40. _state = RecordState.start;
  41. try
  42. {
  43. // 打开写入
  44. lock (this)
  45. {
  46. videoWriter.Open(
  47. _filePath,
  48. _width,
  49. _height,
  50. _rate,
  51. VideoCodec.MPEG4,
  52. _width * _height * _quality
  53. );
  54. }
  55. System.Drawing.Rectangle rec = new System.Drawing.Rectangle(0,0,_width,_height);
  56. videoStreamer = new ScreenCaptureStream(rec, 1000 / _rate);//帧间隔需要和帧率关联,不然录的10秒视频文件不是10s
  57. videoStreamer.NewFrame += VideoNewFrame;
  58. videoStreamer.Start();
  59. }
  60. catch (Exception ex)
  61. {
  62. return false;
  63. }
  64. return true;
  65. }
  66. [DllImport("user32.dll")]
  67. private static extern bool GetCursorInfo(out CURSORINFO pci);
  68. [StructLayout(LayoutKind.Sequential)]
  69. private struct POINT
  70. {
  71. public Int32 x;
  72. public Int32 y;
  73. }
  74. [StructLayout(LayoutKind.Sequential)]
  75. private struct CURSORINFO
  76. {
  77. public Int32 cbSize;
  78. public Int32 flags;
  79. public IntPtr hCursor;
  80. public POINT ptScreenPos;
  81. }
  82. // 帧返回时绘制上鼠标
  83. private void VideoNewFrame(object sender, NewFrameEventArgs e)
  84. {
  85. if (_state == RecordState.start)
  86. {
  87. if (_captureMouse)
  88. {
  89. var g = System.Drawing.Graphics.FromImage(e.Frame);
  90. CURSORINFO pci;
  91. pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
  92. GetCursorInfo(out pci);
  93. try
  94. {
  95. System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor);
  96. cur.Draw(
  97. g,
  98. new System.Drawing.Rectangle(
  99. System.Windows.Forms.Cursor.Position.X - 10,
  100. System.Windows.Forms.Cursor.Position.Y - 10,
  101. cur.Size.Width,
  102. cur.Size.Height
  103. )
  104. );
  105. }
  106. catch { }//打开任务管理器时会导致异常
  107. }
  108. videoWriter.WriteVideoFrame(e.Frame);
  109. }
  110. }
  111. /// <summary>
  112. /// 结束录制
  113. /// </summary>
  114. public void StopRecordVideo()
  115. {
  116. _state = RecordState.stop;
  117. // 停止
  118. videoStreamer.Stop();
  119. //结束写入
  120. videoWriter.Close();
  121. videoWriter.Dispose();
  122. }
  123. /// <summary>
  124. /// 暂停录制
  125. /// </summary>
  126. public void PauseRecordVideo()
  127. {
  128. _state = RecordState.pause;
  129. }
  130. /// <summary>
  131. /// 恢复录制
  132. /// </summary>
  133. public void ResumeRecordVideo()
  134. {
  135. _state = RecordState.start;
  136. }
  137. }
  138. }