使用c#Accord .Net

时间:2017-11-15 10:52:26

标签: c# audio video webcam accord.net

我试图在第一眼看到轻松的事情:使用Accord .net框架在AVI文件中录制视频和音频(来自网络摄像头)。似乎看起来更复杂了:))

到目前为止,我使用此帧处理程序很好地处理视频帧(我认为):

void videoDevice_NewFrame(object sender, Accord.Video.NewFrameEventArgs eventArgs)
        {

            System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone();
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();

            MemoryStream ms = new MemoryStream();
            imgforms.Save(ms, ImageFormat.Bmp);
            ms.Seek(0, SeekOrigin.Begin);

            bi.StreamSource = ms;
            bi.EndInit();

            //Using the freeze function to avoid cross thread operations 
            bi.Freeze();

            //Calling the UI thread using the Dispatcher to update the 'Image' WPF control         
            Dispatcher.BeginInvoke(new ThreadStart(delegate
            {
                pbox.Source = bi; /*pbox is the name of the 'Image' WPF control*/
            }));

            if (_recording)
            {
                long currentTick = DateTime.Now.Ticks;
                StartTick = StartTick ?? currentTick;
                var frameOffset = new TimeSpan(currentTick - StartTick.Value);

                double elapsedTimeInSeconds = stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;
                double timeBetweenFramesInSeconds = 1.0 / 25;
                if (elapsedTimeInSeconds >= timeBetweenFramesInSeconds)
                {
                    stopwatch.Restart();
                    try
                    {
                        _writer.WriteVideoFrame(eventArgs.Frame, frameOffset);
                    }catch(Exception ex)
                    {

                    }

                    //Here come WriteAudioFrame(Signal signal) ?
                }

            }

        }

和音频数据存储在音频新帧事件处理程序的内存流/ WaveEncoder中:

audio_stream = new MemoryStream();           
audio_encoder = new WaveEncoder(audio_stream);

void audioDevice_NewFrame(object sender, Accord.Audio.NewFrameEventArgs eventArgs)
        {

            audio_encoder.Encode(eventArgs.Signal);

        }

现在,我不明白如何将音频内存流(或WaveEncoder?)转换为@cesarsouza在Accord .Net的最后一个预发行版v3.8.2中编写的新方法所需的Signal对象:

_writer.WriteAudioFrame(Signal signal)

以及如何同步视频和音频。我怀疑我们需要根据采样率获得相同数量的音频和视频帧。

你知道如何做到这一点吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

您可以使用WaveDecoder

WaveDecoder decoder = new WaveDecoder();
Signal s = decoder.Decode();
_writer.WriteAudioFrame(s);