AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

Recording speed is very poor !

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Recording speed is very poor !

Postby gomidas95 » Sun Dec 10, 2017 1:17 pm

I am a newcomer I just tried to record some videos and even I set bitrate higher and fps 60 I get very poor results what should I do to get video faster ? That changes frames very slowly and looks like frozen!


Code: Select all
using AForge.Video;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Accord.Video.FFMPEG;
using System.IO;
using System.Threading;

namespace ArevScreenRecord
{
    public partial class Form1 : Form
    {
        // get entire desktop area size
        Rectangle screenArea = Rectangle.Empty;
        ScreenCaptureStream stream;
        private VideoFileWriter FileWriter = new VideoFileWriter();
        private string path = Path.GetDirectoryName(Application.ExecutablePath) + @"\Videos\";
        Bitmap bitmap;
        public Form1()
        {
            InitializeComponent();

            foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens)
            {
                screenArea = Rectangle.Union(screenArea, screen.Bounds);
            }
            // create screen capture video source
            stream = new ScreenCaptureStream(screenArea);

            // set NewFrame event handler
            stream.NewFrame += new NewFrameEventHandler(video_NewFrame);

            // start the video source
            stream.Start();

        }

        private Thread workerThread = null;
        private bool stopProcess = false;
        private bool isRecord = false;

        private void recordScreen()
        {
            if (!stopProcess)
            {
                while (isRecord)
                {
                    FileWriter.WriteVideoFrame(bitmap);
                }
                FileWriter.Close();
            }
            else
            {
                workerThread.Abort();
            }
        }

        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // get new frame
            bitmap = eventArgs.Frame;
            // process the frame


        }

        private void RecordScreenBtn_Click(object sender, EventArgs e)
        {
            isRecord = true;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            FileWriter.Open(path + "recorded at " + DateTime.Now.ToString("HH-mm-ss") + ".mp4", screenArea.Width, screenArea.Height, 60, VideoCodec.MPEG4, 1111800000);

            //FileWriter.WriteVideoFrame(bitmap);
            stopProcess = false;
            workerThread = new Thread(new ThreadStart(recordScreen));
            workerThread.Start();

        }

        private void StopScreenRecordBtn_Click(object sender, EventArgs e)
        {
            // ...
            // signal to stop
            stopProcess = true;
            isRecord = false;

            if (workerThread != null) workerThread.Abort();
            bitmap = null;
            // ...

        }
        private void ReleaseScreenCapture()
        {
            try
            {
                if (stream.IsRunning)
                {
                    stream.SignalToStop();
                    stream.Stop();
                    if (FileWriter.IsOpen)
                    {
                        FileWriter.Close();
                        bitmap = null;
                    }
                }
                else
                    return;
            }
            catch
            {
                if (stream != null) { stream.Stop(); }
                FileWriter.Close();
                bitmap = null;
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ReleaseScreenCapture();
        }
    }
}
gomidas95
 
Posts: 1
Joined: Sun Dec 10, 2017 1:15 pm



Return to AForge.NET Framework

cron