|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Drawing.Imaging;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using Accord.Video;
- using Accord.Video.FFMPEG;
-
- namespace XHWK.WKTool.Utils
- {
- using System.Linq;
-
- public class ZVideoRecordHelper
- {
- public enum RecordState
- {
- Stop = 0,
- Start = 1,
- Pause = 2
- }
-
- private RecordState _state;
-
- //录制帧率
-
- //录制质量
-
- private bool _captureMouse;
-
- private VideoFileWriter _videoWriter = new VideoFileWriter(); //视频写入
- private ScreenCaptureStream _videoStreamer; //视频捕获
-
- public ZVideoRecordHelper(string filePath, int width, int height, int rate = 5, int quality = 8, bool captureMouse = false)
- {
- int width1 = width / 2 * 2;
- int height1 = height / 2 * 2;
- int rate1 = rate;
- _captureMouse = captureMouse;
- _state = RecordState.Pause;
- try
- {
- // 打开写入
- lock (this)
- {
- _videoWriter.Open(
- filePath,
- width1,
- height1,
- rate1,
- VideoCodec.MPEG4,
- width1 * height1 * quality
- );
- }
- System.Drawing.Rectangle rec = new System.Drawing.Rectangle(
- 0,
- 0,
- width1,
- height1
- );
- _videoStreamer = new ScreenCaptureStream(rec, 1000 / rate1); //帧间隔需要和帧率关联,不然录的10秒视频文件不是10s
- _videoStreamer.NewFrame += VideoNewFrame;
- _videoStreamer.Start();
- }
- catch (Exception)
- {
- // ignored
- }
- }
-
- /// <summary>
- /// 开始录制
- /// </summary>
- public bool StartRecordVideo()
- {
- _state = RecordState.Start;
- return true;
- }
-
- [DllImport("user32.dll")]
- private static extern bool GetCursorInfo(out Cursorinfo pci);
-
- [StructLayout(LayoutKind.Sequential)]
- private struct Point
- {
- public Int32 x;
- public Int32 y;
- }
-
- [StructLayout(LayoutKind.Sequential)]
- private struct Cursorinfo
- {
- public Int32 cbSize;
- public Int32 flags;
- public IntPtr hCursor;
- public Point ptScreenPos;
- }
-
- // 帧返回时绘制上鼠标
- private void VideoNewFrame(object sender, NewFrameEventArgs e)
- {
- if (_state == RecordState.Start)
- {
- if (_captureMouse)
- {
- var g = System.Drawing.Graphics.FromImage(e.Frame);
- Cursorinfo pci;
- pci.cbSize = Marshal.SizeOf(typeof(Cursorinfo));
- GetCursorInfo(out pci);
- try
- {
- System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor);
- cur.Draw(
- g,
- new System.Drawing.Rectangle(
- System.Windows.Forms.Cursor.Position.X - 10,
- System.Windows.Forms.Cursor.Position.Y - 10,
- cur.Size.Width,
- cur.Size.Height
- )
- );
- }
- catch
- {
- // ignored
- } //打开任务管理器时会导致异常
- }
- _videoWriter.WriteVideoFrame(e.Frame);
- }
- }
-
- /// <summary>
- /// 结束录制
- /// </summary>
- public void StopRecordVideo()
- {
- _state = RecordState.Stop;
- // 停止
- _videoStreamer.Stop();
-
- //结束写入
- _videoWriter.Close();
- _videoWriter.Dispose();
- }
-
- /// <summary>
- /// 暂停录制
- /// </summary>
- public void PauseRecordVideo()
- {
- _state = RecordState.Pause;
- }
-
- /// <summary>
- /// 恢复录制
- /// </summary>
- public void ResumeRecordVideo()
- {
- _state = RecordState.Start;
- }
-
- /// <summary>
- /// 生成缩略图
- /// </summary>
- /// <param name="mp4Path"></param>
- /// <param name="picPath"></param>
- public static void GetVideoPic(string mp4Path, string picPath)
- {
- using (var video = new VideoFileReader())
- {
- video.Open(mp4Path);
- int frameNum = video.FrameCount > 3 ? 3 : 0;
- using (var bmp = video.ReadVideoFrame(frameNum))
- {
- SaveAsJpeg
- (
- bmp,
- picPath,
- 90
- );
- }
- video.Close();
- }
- }
-
- // 保存为jpeg格式,quality为图像质量(0-100)
- public static void SaveAsJpeg
- (
- Bitmap bitmap,
- string fileName,
- int quality
- )
- {
- var jpegEncoder = ImageCodecInfo.GetImageEncoders().First(codec => codec.FormatID == ImageFormat.Jpeg.Guid);
- var encoderParameters = new EncoderParameters(1);
- encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
- bitmap.Save
- (
- fileName,
- jpegEncoder,
- encoderParameters
- );
- }
- }
- }
|