gpt4 book ai didi

c# 如何从 nvlc 捕获音频并引发 Accord.Audio.NewFrameEventArgs

转载 作者:行者123 更新时间:2023-12-04 23:02:38 26 4
gpt4 key购买 nike

我正在用 c# 编写应用程序来记录来自 IP 摄像机的视频流。

我正在使用 Accord.Video.FFMPEG.VideoFileWriter和 nVLC C# 包装器。
我有一个使用 nVLC 从流中捕获音频的类,它应该实现 IAudioSource接口(interface),所以我用了CustomAudioRendere捕获声音数据,然后引发包含信号对象的事件 NewFrame。
问题是在将信号保存到视频文件时,当来自 RTSP 的记录时声音很可怕(离散)流,但从本地麦克风(从笔记本电脑)录制时质量很好。
这是引发事件的代码:

public void Start()
{
_mFactory = new MediaPlayerFactory();
_mPlayer = _mFactory.CreatePlayer<IAudioPlayer>();
_mMedia = _mFactory.CreateMedia<IMedia>(Source);
_mPlayer.Open(_mMedia);

var fc = new Func<SoundFormat, SoundFormat>(SoundFormatCallback);
_mPlayer.CustomAudioRenderer.SetFormatCallback(fc);
var ac = new AudioCallbacks { SoundCallback = SoundCallback };
_mPlayer.CustomAudioRenderer.SetCallbacks(ac);

_mPlayer.Play();
}

private void SoundCallback(Sound newSound)
{

var data = new byte[newSound.SamplesSize];
Marshal.Copy(newSound.SamplesData, data, 0, (int)newSound.SamplesSize);

NewFrame(this, new Accord.Audio.NewFrameEventArgs(new Signal(data,Channels, data.Length, SampleRate, Format)));
}

private SoundFormat SoundFormatCallback(SoundFormat arg)
{


Channels = arg.Channels;
SampleRate = arg.Rate;
BitPerSample = arg.BitsPerSample;

return arg;

}

这是捕获事件的代码:
private void source_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Signal sig = eventArgs.Signal;

duration += eventArgs.Signal.Duration;
if (videoFileWrite == null)
{


videoFileWrite = new VideoFileWriter();
videoFileWrite.AudioBitRate = sig.NumberOfSamples*sig.NumberOfChannels*sig.SampleSize;
videoFileWrite.SampleRate = sig.SampleRate;
videoFileWrite.FrameSize = sig.NumberOfSamples/sig.NumberOfFrames;


videoFileWrite.Open("d:\\output.mp4");
}
if (isStartRecord)
{
DoneWriting = false;

using (MemoryStream ms = new MemoryStream())
{
encoder = new WaveEncoder(ms);
encoder.Encode(eventArgs.Signal);
ms.Seek(0, SeekOrigin.Begin);
decoder = new WaveDecoder(ms);
Signal s = decoder.Decode();
videoFileWrite.WriteAudioFrame(s);

encoder.Close();
decoder.Close();

}
DoneWriting = true;
}
}

最佳答案

我通过从 SoundCallback void 中的 Sound 对象“newSound”中仅获取一个 channel 解决了这个问题,然后从该字节数组中创建一个信号并引发事件“NewFrame”。
该解决方案背后的主要思想是,当音频流包含多个 channel 时,SoundCallback void 中 Sound 对象中的 SampleData 属性包含所有 channel 的字节数组,格式如下:
让我们假设一个 channel 的两个字节样本的声音数据是 A1A2B1B2C1C2 ......等,所以 SampleData 将是两个 channel :
A1A2A1A2B1B2B1B2C1C2C1C2 ......等。

希望能帮到你。

关于c# 如何从 nvlc 捕获音频并引发 Accord.Audio.NewFrameEventArgs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51810651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com