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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. private RecordState _state = RecordState.stop;
  16. private string _filePath = "";
  17. private int _width = 0;
  18. private int _height = 0;
  19. //录制帧率
  20. private int _rate = 5;
  21. //录制质量
  22. private int _quality = 8;
  23. private 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. _state = RecordState.pause;
  35. try
  36. {
  37. // 打开写入
  38. lock (this)
  39. {
  40. videoWriter.Open(
  41. _filePath,
  42. _width,
  43. _height,
  44. _rate,
  45. VideoCodec.MPEG4,
  46. _width * _height * _quality
  47. );
  48. }
  49. System.Drawing.Rectangle rec = new System.Drawing.Rectangle(0, 0, _width, _height);
  50. videoStreamer = new ScreenCaptureStream(rec, 1000 / _rate);//帧间隔需要和帧率关联,不然录的10秒视频文件不是10s
  51. videoStreamer.NewFrame += VideoNewFrame;
  52. videoStreamer.Start();
  53. }
  54. catch (Exception ex)
  55. {
  56. }
  57. }
  58. /// <summary>
  59. /// 开始录制
  60. /// </summary>
  61. public bool StartRecordVideo()
  62. {
  63. _state = RecordState.start;
  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. }