我想同时录制视频和音频,但我不知道该怎么做?

时间:2019-08-22 10:12:11

标签: c# aforge accord.net

我正在使用Accord.net来分别录制音频和视频,但是我想同时进行这两个过程,因为我会在使用套接字将其录制在另一个应用程序中的同时读取带有声音的视频。

我在某处阅读以使用videofilewriter,但我不知道如何将其与音频一起使用。 我在后台运行记录,因为它使表格冻结。

  public Form1()
    {
        InitializeComponent();
 // back ground running
        bg = new BackgroundWorker();
        bg.WorkerSupportsCancellation = true;
        bg.DoWork += Run;
        audiostream = new MemoryStream();
        audioencoder = new WaveEncoder(audiostream);            
    }    
 private void Form1_Load(object sender, EventArgs e)
    {
        VideoCaptureDevices = new 
        FilterInfoCollection(FilterCategory.VideoInputDevice);
        captureDevice = new VideoCaptureDeviceForm();
    }

 private void source_NewFrame(object sender, Accord.Audio.NewFrameEventArgs eventArgs)
    {
        audioencoder.Encode(eventArgs.Signal);
    }

    void FinalVideo_NewFrame(object sender, Accord.Video.NewFrameEventArgs eventArgs)
    {
        video = (Bitmap)eventArgs.Frame.Clone();
        pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone(); ;
    }

 private void record_Click(object sender, EventArgs e)
 {
        source = new AudioCaptureDevice()
        {
            // Listen on 44.1 Hz, Cam Quality 1280x720
            DesiredFrameSize = 4096,
            SampleRate = 44100,
        };

 saveFileDialog2.Filter = "Wav Files (*.wav)|*.wav";
 saveFileDialog2.InitialDirectory = @"C:\Users\dell\Desktop\Camrecord";
 saveFileDialog1.Filter = "Avi Files (*.avi)|*.avi";
 saveFileDialog1.InitialDirectory = @"C:\Users\dell\Desktop\Camrecord";

 int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
 int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
 audiofilestream = new FileStream(saveFileDialog2.FileName, 
FileMode.Create);
 FileWriter.Open(saveFileDialog1.FileName, w, h, 155, VideoCodec.MPEG4, 
3200000);
source.NewFrame += source_NewFrame;
source.Start();
bg.RunWorkerAsync();

}

 private void Run(object sender, DoWorkEventArgs e)
    {
        audioencoder = new WaveEncoder(audiofilestream);

        do
        {
            Runexecuted = true;               
            FileWriter.WriteVideoFrame(video);

       } while (!Stopclicked);

        FinalVideo = captureDevice.VideoDevice;
        FinalVideo.NewFrame += new 
        NewFrameEventHandler(FinalVideo_NewFrame);
        source.NewFrame += source_NewFrame;
        source.Start();
        FinalVideo.Start();
    }

如果您知道如何合并它们,我可以在适合我的解决方案之前使用它。谢谢大家。

0 个答案:

没有答案