Home > Blockchain >  How to make my screen recorder program recognize the difference between screenshots and keep only th
How to make my screen recorder program recognize the difference between screenshots and keep only th

Time:07-02

I am making a video recorder, The app works by taking a lot of screenshots and puts them together in one video. Also, I am trying to make something like screen motion detection. I need the app to take screenshot only when difference in the screen is detected. I was thinking on how to do that and I believe I need to makes it still take screenshots but in the same time to compare them with the last one. I really need help on how to code something like this because I cant get my head to understand it.

Below is my code.

using System.IO;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using Accord.Video.FFMPEG;

namespace BenScreenRecorder
{
    class ScreenRecorder
    {
        //Video variables:
        private Rectangle bounds;
        private string outputPath = "";
        private string tempPath = "";
        private int fileCount = 1;
        private List<string> inputImageSequence = new List<string>();

        //File variables:
        private string audioName = "mic.wav";
        private string videoName = "video.mp4";
        private string finalName = "FinalVideo.mp4";

        //Time variable:
        Stopwatch watch = new Stopwatch();

        //Audio variables:
        public static class NativeMethods
        {
            [DllImport("winmm.dll", EntryPoint = "mciSendStringA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
            public static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
        }

        //ScreenRecorder Object:
        public ScreenRecorder(Rectangle b, string outPath)
        {
            //Create temporary folder for screenshots:
            CreateTempFolder("tempScreenCaps");

            //Set variables:
            bounds = b;
            outputPath = outPath;
        }

        //Create temporary folder:
        private void CreateTempFolder(string name)
        {
            //Check if a C or D drive exists:
            if (Directory.Exists("D://"))
            {
                string pathName = $"D://{name}";
                Directory.CreateDirectory(pathName);
                tempPath = pathName;
            }
            else
            {
                string pathName = $"C://Documents//{name}";
                Directory.CreateDirectory(pathName);
                tempPath = pathName;
            }
        }

        //Change final video name:
        public void setVideoName(string name)
        {
            finalName = name;
        }

        //Delete all files and directory:
        private void DeletePath(string targetDir)
        {
            string[] files = Directory.GetFiles(targetDir);
            string[] dirs = Directory.GetDirectories(targetDir);

            //Delete each file:
            foreach (string file in files)
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }

            //Delete the path:
            foreach (string dir in dirs)
            {
                DeletePath(dir);
            }

            Directory.Delete(targetDir, false);
        }

        //Delete all files except the one specified:
        private void DeleteFilesExcept(string targetDir, string excDir)
        {
            string[] files = Directory.GetFiles(targetDir);

            //Delete each file except specified:
            foreach (string file in files)
            {
                if (file != excDir)
                {
                    File.SetAttributes(file, FileAttributes.Normal);
                    File.Delete(file);
                }
            }
        }

        //Clean up program on crash:
        public void cleanUp()
        {
            if (Directory.Exists(tempPath))
            {
                DeletePath(tempPath);
            }
        }

        //Return elapsed time:
        public string getElapsed()
        {
            return string.Format("{0:D2}:{1:D2}:{2:D2}", watch.Elapsed.Hours, watch.Elapsed.Minutes, watch.Elapsed.Seconds);
        }

        //Record video:
        public void RecordVideo()
        {
            //Keep track of time:
            watch.Start();

            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    //Add screen to bitmap:
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
                //Save screenshot:

                string name = tempPath   "//screenshot-"   fileCount   ".png";
                bitmap.Save(name, ImageFormat.Png);
                inputImageSequence.Add(name);
                fileCount  ;

                //Dispose of bitmap:
                bitmap.Dispose();
            }
        }


        //Record audio:
        public void RecordAudio()
        {
            NativeMethods.record("open new Type waveaudio Alias recsound", "", 0, 0);
            NativeMethods.record("record recsound", "", 0, 0);
        }

        //Save audio file:
        private void SaveAudio()
        {
            string audioPath = "save recsound "   outputPath   "//"   audioName;
            NativeMethods.record(audioPath, "", 0, 0);
            NativeMethods.record("close recsound", "", 0, 0);
        }

        //Save video file:
        private void SaveVideo(int width, int height, int frameRate)
        {
            using (VideoFileWriter vFWriter = new VideoFileWriter())
            {
                //Create new video file:
                vFWriter.Open(outputPath   "//"   videoName, width, height, frameRate, VideoCodec.MPEG4);

                //Make each screenshot into a video frame:
                foreach (string imageLocation in inputImageSequence)
                {
                    Bitmap imageFrame = System.Drawing.Image.FromFile(imageLocation) as Bitmap;
                    vFWriter.WriteVideoFrame(imageFrame);
                    imageFrame.Dispose();
                }

                //Close:
                vFWriter.Close();
            }
        }

        //Combine video and audio files:
        private void CombineVideoAndAudio(string video, string audio)
        {
            //FFMPEG command to combine video and audio:
            string args = $"/c ffmpeg -i \"{video}\" -i \"{audio}\" -shortest {finalName}";
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                CreateNoWindow = false,
                FileName = "cmd.exe",
                WorkingDirectory = outputPath,
                Arguments = args
            };

            //Execute command:
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }

        public void Stop()
        {
            //Stop watch:
            watch.Stop();

            //Video variables:
            int width = bounds.Width;
            int height = bounds.Height;
            int frameRate = 10;

            //Save audio:
            SaveAudio();

            //Save video:
            SaveVideo(width, height, frameRate);

            //Combine audio and video files:
            CombineVideoAndAudio(videoName, audioName);

            //Delete the screenshots and temporary folder:
            DeletePath(tempPath);

            //Delete separated video and audio files:
            DeleteFilesExcept(outputPath, outputPath   "\\"   finalName);
        }
    }
}

CodePudding user response:

I have something that may be useful for you. The idea is save only the differences between the images and, with that, recreate later all images from starting image and saved changes.

To do this, you only need make a XOR operation in the image bytes. This method allow you get the difference (the array parameter) between two images:

protected void ApplyXor(Bitmap img1, Bitmap img2, byte[] array)
{
    const ImageLockMode rw = ImageLockMode.ReadWrite;
    const PixelFormat argb = PixelFormat.Format32bppArgb;

    var locked1 = img1.LockBits(new Rectangle(0, 0, img1.Width, img1.Height), rw, argb);
    var locked2 = img2.LockBits(new Rectangle(0, 0, img2.Width, img2.Height), rw, argb);

    try
    {
        ApplyXor(locked2, locked1, array);
    }
    finally
    {
        img1.UnlockBits(locked1);
        img2.UnlockBits(locked2);
    }
}

With the previous img1 bitmap and the array returned, you can get the img2 with this method:

protected void ApplyXor(Bitmap img1, byte[] array, Bitmap img2)
{
    const ImageLockMode rw = ImageLockMode.ReadWrite;
    const PixelFormat argb = PixelFormat.Format32bppArgb;

    var locked1 = img1.LockBits(new Rectangle(0, 0, img1.Width, img1.Height), rw, argb);
    var locked2 = img2.LockBits(new Rectangle(0, 0, img2.Width, img2.Height), rw, argb);

    try
    {
        ApplyXor(locked1, array, locked2);
    }
    finally
    {
        img1.UnlockBits(locked1);
        img2.UnlockBits(locked2);
    }
}

And here the other required methods:

private unsafe void ApplyXor(BitmapData img1, BitmapData img2, byte[] array)
{
    byte* prev0 = (byte*)img1.Scan0.ToPointer();
    byte* cur0 = (byte*)img2.Scan0.ToPointer();

    int height = img1.Height;
    int width = img1.Width;
    int halfwidth = width / 2;

    fixed (byte* target = array)
    {
        ulong* dst = (ulong*)target;

        for (int y = 0; y < height;   y)
        {
            ulong* prevRow = (ulong*)(prev0   img1.Stride * y);
            ulong* curRow = (ulong*)(cur0   img2.Stride * y);

            for (int x = 0; x < halfwidth;   x)
            {
                if (curRow[x] != prevRow[x])
                {
                    int a = 0;
                }

                *(dst  ) = curRow[x] ^ prevRow[x];
            }
        }
    }
}

private unsafe void ApplyXor(BitmapData img1, byte[] array, BitmapData img2)
{
    byte* prev0 = (byte*)img1.Scan0.ToPointer();
    byte* cur0 = (byte*)img2.Scan0.ToPointer();

    int height = img1.Height;
    int width = img1.Width;
    int halfwidth = width / 2;

    fixed (byte* target = array)
    {
        ulong* dst = (ulong*)target;

        for (int y = 0; y < height;   y)
        {
            ulong* prevRow = (ulong*)(prev0   img1.Stride * y);
            ulong* curRow = (ulong*)(cur0   img2.Stride * y);

            for (int x = 0; x < halfwidth;   x)
            {
                curRow[x] = *(dst  ) ^ prevRow[x];
            }
        }
    }
}

NOTE: You must configure your project to allow unsafe.

With previous methods, you can do:

  • Save a img1 bitmap
  • Get img2 bitmap, do XOR and get the array (array2, for example)
  • With img3, get the XOR with img2 (array3, for example). Now, img2 isn't needed
  • With img4, get the XOR with img3 (array4). Now, img3 isn't needed
  • ...

You have img1 and array2, array3, array4... and you can recreate all images:

  • Make XOR between img1 and array2 to get img2
  • Make XOR between img2 and array3 to get img3
  • ...

If you need send video over TCP, you can send the images sending one image and the XOR arrays (the differences). Or better yet, compress the XOR arrays using K4os.Compression.LZ4.

  • Related