Advertisement
TermSpar

Screen Recorder

Dec 1st, 2018
7,778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.18 KB | None | 0 0
  1. using System.IO;
  2. using System.Drawing;
  3. using System.Diagnostics;
  4. using System.Drawing.Imaging;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7.  
  8. using Accord.Video.FFMPEG;
  9.  
  10. namespace BenScreenRecorder
  11. {
  12.     class ScreenRecorder
  13.     {
  14.         //Video variables:
  15.         private Rectangle bounds;
  16.         private string outputPath = "";
  17.         private string tempPath = "";
  18.         private int fileCount = 1;
  19.         private List<string> inputImageSequence = new List<string>();
  20.  
  21.         //File variables:
  22.         private string audioName = "mic.wav";
  23.         private string videoName = "video.mp4";
  24.         private string finalName = "FinalVideo.mp4";
  25.  
  26.         //Time variable:
  27.         Stopwatch watch = new Stopwatch();
  28.  
  29.         //Audio variables:
  30.         public static class NativeMethods
  31.         {
  32.             [DllImport("winmm.dll", EntryPoint = "mciSendStringA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
  33.             public static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
  34.         }
  35.  
  36.         //ScreenRecorder Object:
  37.         public ScreenRecorder(Rectangle b, string outPath)
  38.         {
  39.             //Create temporary folder for screenshots:
  40.             CreateTempFolder("tempScreenCaps");
  41.  
  42.             //Set variables:
  43.             bounds = b;
  44.             outputPath = outPath;
  45.         }
  46.  
  47.         //Create temporary folder:
  48.         private void CreateTempFolder(string name)
  49.         {
  50.             //Check if a C or D drive exists:
  51.             if (Directory.Exists("D://"))
  52.             {
  53.                 string pathName = $"D://{name}";
  54.                 Directory.CreateDirectory(pathName);
  55.                 tempPath = pathName;
  56.             }
  57.             else
  58.             {
  59.                 string pathName = $"C://Documents//{name}";
  60.                 Directory.CreateDirectory(pathName);
  61.                 tempPath = pathName;
  62.             }
  63.         }
  64.  
  65.         //Change final video name:
  66.         public void setVideoName(string name)
  67.         {
  68.             finalName = name;
  69.         }
  70.  
  71.         //Delete all files and directory:
  72.         private void DeletePath(string targetDir)
  73.         {
  74.             string[] files = Directory.GetFiles(targetDir);
  75.             string[] dirs = Directory.GetDirectories(targetDir);
  76.  
  77.             //Delete each file:
  78.             foreach (string file in files)
  79.             {
  80.                 File.SetAttributes(file, FileAttributes.Normal);
  81.                 File.Delete(file);
  82.             }
  83.  
  84.             //Delete the path:
  85.             foreach (string dir in dirs)
  86.             {
  87.                 DeletePath(dir);
  88.             }
  89.  
  90.             Directory.Delete(targetDir, false);
  91.         }
  92.  
  93.         //Delete all files except the one specified:
  94.         private void DeleteFilesExcept(string targetDir, string excDir)
  95.         {
  96.             string[] files = Directory.GetFiles(targetDir);
  97.  
  98.             //Delete each file except specified:
  99.             foreach (string file in files)
  100.             {
  101.                 if(file != excDir)
  102.                 {
  103.                     File.SetAttributes(file, FileAttributes.Normal);
  104.                     File.Delete(file);
  105.                 }
  106.             }
  107.         }
  108.  
  109.         //Clean up program on crash:
  110.         public void cleanUp()
  111.         {
  112.             if (Directory.Exists(tempPath))
  113.             {
  114.                 DeletePath(tempPath);
  115.             }
  116.         }
  117.  
  118.         //Return elapsed time:
  119.         public string getElapsed()
  120.         {
  121.             return string.Format("{0:D2}:{1:D2}:{2:D2}", watch.Elapsed.Hours, watch.Elapsed.Minutes, watch.Elapsed.Seconds);
  122.         }
  123.  
  124.         //Record video:
  125.         public void RecordVideo()
  126.         {
  127.             //Keep track of time:
  128.             watch.Start();
  129.  
  130.             using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
  131.             {
  132.                 using (Graphics g = Graphics.FromImage(bitmap))
  133.                 {
  134.                     //Add screen to bitmap:
  135.                     g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
  136.                 }
  137.                 //Save screenshot:
  138.                 string name = tempPath + "//screenshot-" + fileCount + ".png";
  139.                 bitmap.Save(name, ImageFormat.Png);
  140.                 inputImageSequence.Add(name);
  141.                 fileCount++;
  142.  
  143.                 //Dispose of bitmap:
  144.                 bitmap.Dispose();
  145.             }
  146.         }
  147.  
  148.         //Record audio:
  149.         public void RecordAudio()
  150.         {
  151.             NativeMethods.record("open new Type waveaudio Alias recsound", "", 0, 0);
  152.             NativeMethods.record("record recsound", "", 0, 0);
  153.         }
  154.  
  155.         //Save audio file:
  156.         private void SaveAudio()
  157.         {
  158.             string audioPath = "save recsound " + outputPath + "//" + audioName;
  159.             NativeMethods.record(audioPath, "", 0, 0);
  160.             NativeMethods.record("close recsound", "", 0, 0);
  161.         }
  162.  
  163.         //Save video file:
  164.         private void SaveVideo(int width, int height, int frameRate)
  165.         {
  166.             using (VideoFileWriter vFWriter = new VideoFileWriter())
  167.             {
  168.                 //Create new video file:
  169.                 vFWriter.Open(outputPath + "//" + videoName, width, height, frameRate, VideoCodec.MPEG4);
  170.  
  171.                 //Make each screenshot into a video frame:
  172.                 foreach (string imageLocation in inputImageSequence)
  173.                 {
  174.                     Bitmap imageFrame = System.Drawing.Image.FromFile(imageLocation) as Bitmap;
  175.                     vFWriter.WriteVideoFrame(imageFrame);
  176.                     imageFrame.Dispose();
  177.                 }
  178.  
  179.                 //Close:
  180.                 vFWriter.Close();
  181.             }
  182.         }
  183.  
  184.         //Combine video and audio files:
  185.         private void CombineVideoAndAudio(string video, string audio)
  186.         {
  187.             //FFMPEG command to combine video and audio:
  188.             string args = $"/c ffmpeg -i \"{video}\" -i \"{audio}\" -shortest {finalName}";
  189.             ProcessStartInfo startInfo = new ProcessStartInfo
  190.             {
  191.                 CreateNoWindow = false,
  192.                 FileName = "cmd.exe",
  193.                 WorkingDirectory = outputPath,
  194.                 Arguments = args
  195.             };
  196.  
  197.             //Execute command:
  198.             using (Process exeProcess = Process.Start(startInfo))
  199.             {
  200.                 exeProcess.WaitForExit();
  201.             }
  202.         }
  203.  
  204.         public void Stop()
  205.         {
  206.             //Stop watch:
  207.             watch.Stop();
  208.  
  209.             //Video variables:
  210.             int width = bounds.Width;
  211.             int height = bounds.Height;
  212.             int frameRate = 10;
  213.  
  214.             //Save audio:
  215.             SaveAudio();
  216.  
  217.             //Save video:
  218.             SaveVideo(width, height, frameRate);
  219.  
  220.             //Combine audio and video files:
  221.             CombineVideoAndAudio(videoName, audioName);
  222.  
  223.             //Delete the screenshots and temporary folder:
  224.             DeletePath(tempPath);
  225.  
  226.             //Delete separated video and audio files:
  227.             DeleteFilesExcept(outputPath, outputPath + "\\" + finalName);
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement