C#實現語音視頻錄製 【基於MCapture + MFile】

來源:互聯網
上載者:User

標籤:語音錄製   視頻錄製   案頭錄製   mfile   mcapture   

在上一篇使用C#採集語音視頻、螢幕案頭【基於MCapture組件】的文章中,我們已經可以採集到語音、視頻、案頭資料了,那麼,接下來我們再結合MFile的錄製功能,便能把這些資料寫到檔案中,產生標準的mp4檔案。

        使用MCapture+MFile,我們可以實現以下類似的應用:

(1)錄製課件:錄製螢幕案頭+語音。

(2)錄製自己的MV:錄製網路攝影機視頻+語音。

(3)錄製教學視頻:錄製案頭+自己的視頻+語音。(其中將案頭與自己視頻疊加在一起)

那接下來這篇文章將詳細介紹應用(2)的實現,我們做一個簡單的Demo(文末有源碼下載)。另外兩種應用都可以在本文Demo的基礎上作一些修改即可。Demo 啟動並執行如下所示:      

      

         首先,當點擊啟動裝置按鈕時,我們建立一個網路攝影機採集器執行個體和一個麥克風採集器執行個體,並啟動它們開始採集: 

    this.cameraCapturer = CapturerFactory.CreateCameraCapturer(0, new Size(int.Parse(this.textBox_width.Text), int.Parse(this.textBox_height.Text)), this.fps);    this.cameraCapturer.ImageCaptured += new CbGeneric<Bitmap>(cameraCapturer_ImageCaptured);    this.cameraCapturer.CaptureError += new CbGeneric<Exception>(cameraCapturer_CaptureError);    this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0);    this.microphoneCapturer.AudioCaptured += new CbGeneric<byte[]>(microphoneCapturer_AudioCaptured);    this.microphoneCapturer.CaptureError += new CbGeneric<Exception>(microphoneCapturer_CaptureError);    //開始採集    this.cameraCapturer.Start();    this.microphoneCapturer.Start();

       接下來,點擊開始錄製按鈕時,我們初始化VideoFileMaker組件:

    this.videoFileMaker = new VideoFileMaker();    this.videoFileMaker.AutoDisposeVideoFrame = true;    this.videoFileMaker.Initialize("test.mp4", VideoCodecType.H264, int.Parse(this.textBox_width.Text), int.Parse(this.textBox_height.Text), this.fps, AudioCodecType.AAC, 16000, 1, true);
    this.isRecording = true;

      參數中設定,使用h.264對視頻進行編碼,使用aac對音頻進行編碼,並產生mp4格式的檔案。然後,我們可以通過OMCS擷取即時的音頻資料和視頻資料,並將它們寫到檔案中。

    void microphoneCapturer_AudioCaptured(byte[] audioData) //採集到的語音資料    {        if (this.isRecording)        {            this.videoFileMaker.AddAudioFrame(audioData);                   }    }    //採集到的視頻映像    void cameraCapturer_ImageCaptured(Bitmap img)    {        if (this.isRecording)        {            this.DisplayVideo((Bitmap)img.Clone());            this.videoFileMaker.AddVideoFrame(img);                   }        else        {            this.DisplayVideo(img);        }    }

     當想結束錄製時,則調用Close方法:

   this.videoFileMaker.Close(true);

 更多細節大家可以下載文末的源碼研究,最後,有幾點需要額外強調一下的:

(1)網路攝影機採集的幀頻最好與錄製所設定的幀頻完全一致。demo中是使用成員變數fps來表示的。

(2)videoFileMaker的AutoDisposeVideoFrame屬性要設定為ture,即儘快釋放不再使用的視訊框架,節省記憶體。

(3)DisplayVideo方法使用的是採集視訊框架的複製品,這是因為videoFileMaker不是同步寫入的,而是先將幀放入隊列,然後非同步寫入的。

(4)DisplayVideo方法在使用完作為背景的視訊框架後,也要儘快釋放。道理同上。


Demo源碼下載。

C#實現語音視頻錄製 【基於MCapture + MFile】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.