Skip to content

Instantly share code, notes, and snippets.

@MzHmO
Created January 25, 2024 12:33
Show Gist options
  • Save MzHmO/210e1b098930793913b8caae8c7c1c07 to your computer and use it in GitHub Desktop.
Save MzHmO/210e1b098930793913b8caae8c7c1c07 to your computer and use it in GitHub Desktop.
proctor
using Accord.Video.FFMPEG;
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
class Program
{
private static VideoFileWriter writer;
private static Bitmap videoFrame;
private static Bitmap webcamFrame;
private static readonly object videoFrameLock = new object();
private static readonly object webcamFrameLock = new object();
private static bool isRecording = true;
static void Main()
{
Rectangle bounds = GetScreenBounds();
int width = bounds.Width;
int height = bounds.Height;
writer = new VideoFileWriter();
writer.Open("desktop_with_webcam.avi", width, height, 10, VideoCodec.MPEG4, 10000000);
Thread screenThread = new Thread(() => CaptureDesktop(bounds));
screenThread.Start();
VideoCaptureDevice videoSource = StartWebCamCapture();
Thread.Sleep(20000); // Двадцать секунд для записи
isRecording = false;
videoSource.SignalToStop();
videoSource.WaitForStop();
screenThread.Join();
writer.Close();
Console.WriteLine("Recording finished.");
}
static void CaptureDesktop(Rectangle bounds)
{
while (isRecording)
{
Bitmap combinedFrame = null;
lock (videoFrameLock)
{
// Создаем битмап с размерами, охватывающими все мониторы
combinedFrame = new Bitmap(bounds.Width, bounds.Height);
using (var g = Graphics.FromImage(combinedFrame))
{
// Координаты начала захвата учитывают отрицательные координаты
g.CopyFromScreen(bounds.Left, bounds.Top, 0, 0, bounds.Size);
lock (webcamFrameLock)
{
if (webcamFrame != null)
{
// положение для кадра веб-камеры
int x = combinedFrame.Width - webcamFrame.Width - 10;
int y = combinedFrame.Height - webcamFrame.Height - 10;
g.DrawImage(webcamFrame, x, y, webcamFrame.Width, webcamFrame.Height);
}
}
}
}
writer.WriteVideoFrame(combinedFrame);
combinedFrame.Dispose();
Thread.Sleep(100);
}
}
static VideoCaptureDevice StartWebCamCapture()
{
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
throw new ApplicationException("No webcam found.");
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
videoSource.NewFrame += video_NewFrame;
videoSource.Start();
return videoSource;
}
static Rectangle GetScreenBounds()
{
Rectangle bounds = Rectangle.Empty;
foreach (Screen screen in Screen.AllScreens)
{
bounds = Rectangle.Union(bounds, screen.Bounds);
}
return bounds;
}
static void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
lock (webcamFrameLock)
{
webcamFrame?.Dispose();
webcamFrame = (Bitmap)eventArgs.Frame.Clone();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment