C#實現錄制屏幕


C#實現錄制屏幕

  以前寫過兩篇錄制麥克風語音和攝像頭視頻的文章(實現語音視頻錄制在服務器端錄制語音視頻),最近有朋友問,如果要實現屏幕錄制這樣的功能,該怎么做了?實際上原理是差不多的,如果了解了我前面兩篇文章中介紹的內容,只要在它們的基礎上做一些修改就可以了。

一.實現原理

   實現方案仍然基於OMCS+MFile構建,原理與實現語音視頻錄制差不多,我這里只列出其中的主要差異:

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

(2)使用定時器(比如10fps,則每隔100ms一次)定時調用DynamicDesktopConnector的GetCurrentImage方法,把得到的圖像使用MFile寫入視頻文件。

(3)Demo演示的是不需要同時錄制麥克風的聲音,所以使用了MFile提供的SilenceVideoFileMaker組件(而非原來的VideoFileMaker組件),僅僅錄制視頻數據。

(4)通過MultimediaManager的DesktopEncodeQuality屬性,控制屏幕圖像的清晰度。

 

二.實現代碼

  該Demo的所有源碼如下所示,如果不想下載Demo,可以直接通過下面的代碼了解詳細的實現思路。

復制代碼
    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("生成視頻文件成功!");
        }
    }
復制代碼

 

三.Demo下載

       錄制屏幕Demo源碼 

  F5運行Demo,我錄制了一段時間的自己桌面為mp4視頻文件(我的屏幕分辨率為1920*1080),然后,使用QQ影音播放效果如下:

         

  本文的Demo還可以做更多的擴展,比如: 

(1)當前demo僅僅是錄制了無聲的屏幕圖像,如果希望在錄制屏幕的同時,錄制麥克風的聲音(比如,錄制課件時經常有這樣的需求),那么,可以引入MicrophoneConnector,並使用MFile的VideoFileMaker組件就可以了。 

(2)如果把OMCS服務端從demo中拆出來,並用兩個客戶端帳號登錄,就可以實現遠程屏幕錄制了。當然,這需要有足夠大的帶寬來支持。

 

 

 
 
分類:  C#專欄
標簽:  錄制屏幕屏幕錄制


免責聲明!

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



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