Click here to Skip to main content
16,022,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I wanted to build a screen recorder app which should record the screen and at the same time it should record the audio of system. So I tried to use AI tools to do this, they suggested me using NAudio and SharpAvi libraries to do so, but I couldn't achieve anything in order to build this kind of app that I've mentioned above using the combination of these libraries. Since I don't have any practical knowledge to use these libraries.

What I have tried:

C#
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using NAudio.Wave;
using SharpAvi;
using SharpAvi.Codecs;
using SharpAvi.Output;

namespace ScreenAudioRecorder
{
    public partial class Form1 : Form
    {
        private Stopwatch stopwatch;
        private string outputFileName;
        private WasapiLoopbackCapture audioCapture;
        private AviWriter aviWriter;
        private IAviVideoStream videoStream;
        private IAviAudioStream audioStream;
        private System.Windows.Forms.Timer screenCaptureTimer; // Specify the correct Timer
        private int frameRate = 10; // Frame rate for video

        public Form1()
        {
            InitializeComponent();
            stopwatch = new Stopwatch();
            screenCaptureTimer = new System.Windows.Forms.Timer(); // Specify the correct Timer
            screenCaptureTimer.Interval = 1000 / frameRate;
            screenCaptureTimer.Tick += CaptureScreenFrame;
        }

        private void OnFormLoad(object sender, EventArgs e)
        {
            stopwatch = new Stopwatch();
        }

        private void BtnRecord_Clicked(object sender, EventArgs e)
        {
            var dialog = new SaveFileDialog
            {
                Filter = "AVI files | *.avi"
            };

            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            outputFileName = dialog.FileName;
            btn_record.Enabled = false;
            btn_stop.Enabled = true;

            aviWriter = new AviWriter(outputFileName)
            {
                FramesPerSecond = frameRate,
                EmitIndex1 = true
            };

            var screenBounds = Screen.PrimaryScreen.Bounds;
            videoStream = aviWriter.AddVideoStream();
            videoStream.Width = screenBounds.Width;
            videoStream.Height = screenBounds.Height;
            videoStream.Codec = CodecIds.Uncompressed;
            videoStream.BitsPerPixel = BitsPerPixel.Bpp24;

            audioCapture = new WasapiLoopbackCapture();
            var audioFormat = audioCapture.WaveFormat;
            audioStream = aviWriter.AddAudioStream(audioFormat.SampleRate, audioFormat.Channels, (int)audioFormat.BitsPerSample);
            audioStream.Name = "System Audio";

            audioCapture.DataAvailable += (s, a) =>
            {
                audioStream.WriteBlock(a.Buffer, 0, a.BytesRecorded);
            };

            audioCapture.StartRecording();
            screenCaptureTimer.Start();
            stopwatch.Start();
        }

        private void CaptureScreenFrame(object sender, EventArgs e)
        {
            var screenBounds = Screen.PrimaryScreen.Bounds;
            using (var bitmap = new Bitmap(screenBounds.Width, screenBounds.Height))
            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(0, 0, 0, 0, screenBounds.Size);
                var bits = bitmap.GetFrame();
                videoStream.WriteFrame(true, bits, 0, bits.Length);
            }
        }

        private void BtnStop_Clicked(object sender, EventArgs e)
        {
            btn_record.Enabled = true;
            btn_stop.Enabled = false;
            screenCaptureTimer.Stop();
            audioCapture.StopRecording();
            stopwatch.Stop();

            aviWriter.Close();
            audioCapture.Dispose();

            stopwatch.Reset();
        }

        private void TimerTick(object sender, EventArgs e)
        {
            lbl_Timer.Text = string.Format("{0:hh\\:mm\\:ss}", stopwatch.Elapsed);
        }
    }

    public static class BitmapExtensions
    {
        public static byte[] GetFrame(this Bitmap bitmap)
        {
            var bits = new byte[bitmap.Width * bitmap.Height * 3];
            var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, bits, 0, bits.Length);
            bitmap.UnlockBits(bitmapData);
            return bits;
        }
    }
}
Posted
Updated 29-May-24 1:04am
v3
Comments
pdoxtader 28-May-24 16:33pm    
Every new programmer seems to want to make something like this, or remote control software. I guess it's just part of the process... until you realize it's been done to death to the point where you can get great software that does this, and it's free.

Check out OBS: https://obsproject.com/

Quote:
So I tried to use AI tools to do this
- Albeit that AI like ChatGPT etc . is a great tool to use, it is just that, a tool. You cannot believe or copy/paste as-is code and expect it to work straight from the bat, it will point you in the right direction but you need to know what the code is doing and when.

Quote:
suggested me using NAudio and SharpAvi libraries to do so
- Your first step should then be to read and study the documentation for these 2 libraries to understand where you need to change the code for it to work properly - This contains the documentation to naudio - NAudio[^] This contains the documentation to SharpAVI[^] Note that both is a few years old and there might be better and/or newer solutions...

pdoxtader suggested the correct route, why re-invent the wheel if it has been offered for free and as a plug-in.
 
Share this answer
 
Rather than relying on ChatGPT, you could have looked to see if anyone else had implemented something like this. I did a quick search and found this lengthy tutorial[^]. It looks rather good. You might want to try this one instead of ChatGPT.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900