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
}
}
///
/// 开始录制
///
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);
}
}
///
/// 结束录制
///
public void StopRecordVideo()
{
_state = RecordState.Stop;
// 停止
_videoStreamer.Stop();
//结束写入
_videoWriter.Close();
_videoWriter.Dispose();
}
///
/// 暂停录制
///
public void PauseRecordVideo()
{
_state = RecordState.Pause;
}
///
/// 恢复录制
///
public void ResumeRecordVideo()
{
_state = RecordState.Start;
}
///
/// 生成缩略图
///
///
///
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
);
}
}
}