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 自動升級系統 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM