C#实现录音录像录屏源码

  以前写过两篇录音和录像的文章(实现语音视频录制在服务器端录制语音视频),最近有朋友问,如果要实现屏幕录制这样的功能,该怎么做了?实际上录屏的原理跟录音、录像是差不多的,如果了解了我前面两篇文章中介绍的内容,只要在它们的基础上做一些修改就可以了。

一.录屏原理

   录制屏幕的实现方案仍然基于OMCS+MFile构建,原理与实现语音视频录制差不多,我这里只列出其中的主要差异:

(1)使用DynamicDesktopConnector连接到屏幕桌面。

(2)使用定时器(比如10fps,则每隔100ms一次)定时调用DynamicDesktopConnector的GetCurrentImage方法,把得到的图像使用MFile写入视频文件。

(3)源码演示的是不需要同时录制麦克风的声音,所以使用了MFile提供的SilenceVideoFileMaker组件(而非原来的VideoFileMaker组件),仅仅录制视频数据。

(4)通过MultimediaManager的DesktopEncodeQuality属性,控制屏幕图像的清晰度。

 

二.录屏源码

  源码如下所示,如果不想下载源码,可以直接通过下面的代码了解详细的实现思路。

    public partial class Form1 : Form
    {
        private MultimediaServer server; //在本地内嵌OMCS服务器
        private IMultimediaManager multimediaManager;
        private SilenceVideoFileMaker maker = new SilenceVideoFileMaker(); //录制无声视频
        private DynamicDesktopConnector dynamicDesktopConnector = new DynamicDesktopConnector(); //远程桌面连接器
       
        public Form1()
        {
            InitializeComponent();
            int port = 9900;
            OMCSConfiguration config = new OMCSConfiguration(10,8, EncodingQuality.High,16000,640,480,"") ;
            this.server = new MultimediaServer(port, new DefaultUserVerifier(), config, false, null);

            this.multimediaManager = MultimediaManagerFactory.GetSingleton();
            this.multimediaManager.DesktopEncodeQuality = 1; //通过此参数控制清晰度 
            this.multimediaManager.Initialize("aa01", "", "127.0.0.1", port);

            this.dynamicDesktopConnector.ConnectEnded += new ESBasic.CbGeneric<ConnectResult>(dynamicDesktopConnector_ConnectEnded);
            this.dynamicDesktopConnector.BeginConnect("aa01"); //连接本地桌面          

            this.Cursor = Cursors.WaitCursor;            
        }       

        void dynamicDesktopConnector_ConnectEnded(ConnectResult obj)
        {
            System.Threading.Thread.Sleep(500);
            this.Ready();  
        }       

        private void Ready()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new CbGeneric(this.Ready));
            }
            else
            {
                this.Cursor = Cursors.Default;
                this.button1.Enabled = true;
                this.label1.Visible = false;
            }
        }

        private System.Threading.Timer timer;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");
                //初始化H264视频文件
                this.maker.Initialize("test.mp4", VideoCodecType.H264, this.dynamicDesktopConnector.DesktopSize.Width, this.dynamicDesktopConnector.DesktopSize.Height, 10);

                this.timer = new System.Threading.Timer(new System.Threading.TimerCallback(this.Callback), null ,0, 100);
                this.label1.Text = "正在录制......";
                this.label1.Visible = true;
                this.button1.Enabled = false;
                this.button2.Enabled = true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        } 

        //定时获取屏幕图像,并使用MFile写入视频文件
        private void Callback(object state)
        {            
            Bitmap bm = this.dynamicDesktopConnector.GetCurrentImage();
            this.maker.AddVideoFrame(bm);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.timer.Dispose();
            this.button1.Enabled = false;
            this.button2.Enabled = false;
            this.label1.Visible = false;

            this.maker.Close(true);
            MessageBox.Show("生成视频文件成功!");
        }
    }

 

三.源码开源下载

       2015.01.06 现在更好的方案是 MCapture + MFile,将声卡/麦克风/摄像头/屏幕的采集与录制集中在一个源码中,截图运行如下:    

             

 

2014.11.26  现在录制本地的语音、视频、屏幕的最好的方案是MCapture + MFile,而不是通过OMCS绕一大圈,相应的源码源码下载Oraycn.Record源码.rar 。 

       当然,如果是远程录制语音、视频、屏幕,最好的方案是OMCS + MFile

2015.6.18 整理全部相关开源源码如下:

(声卡/麦克风/摄像头/屏幕)采集&录制源码源码:WinForm版本   WPF版本。 

          声卡录制源码、 混音&录制源码、  同时录制(桌面+麦克风+声卡)源码、 麦克风摄像头录制(可预览) 

          录制画中画(桌面+摄像头+麦克风/声卡)。 

          远程录制或在服务器端录制语音视频屏幕

 

敬请了解:

ESFramework通信框架     OMCS网络语音视频框架     MFile语音视频录制组件    MCapture语音视频采集组件  StriveEngine轻量级通信引擎    OAUS 自动升级系统 

 

posted @ 2014-03-11 15:31  zhuweisky  阅读(39956)  评论(55编辑  收藏  举报