捕获到了音视频后要保存到本地文件,这是很常见的应用场景,DxPackNet保存视频文件也比较简单
用 IAviStreamWriter avi文件写入流即可
1.初始化相关设备,设定好数据捕获的回调函数
DxDeviceCollection cameras; IDxCameraCapture camCapture; IDxMicrophCapture microphone; IAviStreamWriter aviWriter; private void Form1_Load(object sender, EventArgs e) { cameras = DxFactory.CreateDxCompent<DxDeviceCollection>(DxDeviceCollection.ENVIRONMENT_CAMERAS); camCapture = DxFactory.CreateDxCompent<IDxCameraCapture>(this, panel1); camCapture.FrameCb = camerasFrameCb; camCapture.Init(cameras[0]); microphone = DxFactory.CreateDxCompent<IDxMicrophCapture>(100); microphone.AudioCb = auCaptureBufferCB; }
2.创建avi写入流 打开设备
private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; //创建avi文件写入流 第一个参数为-写入文件路径 第二个参数为-是否支持音频写入,true为音/视频,false为只写视频 aviWriter = DxFactory.CreateDxCompent<IAviStreamWriter>("c:\avicaptest.avi", true); aviWriter.Width = 640;//设置视频宽度 aviWriter.Height = 480;//设置视频高度 aviWriter.CompressType = AviVideoCompressType.MicrosoftVideo1; //设置视频压缩格式 aviWriter.Open();//打开写入流 microphone.Open(); microphone.Start(); camCapture.Run(); }
3.在回调中写入音/视频 数据
private void auCaptureBufferCB(byte[] buffer, int length) { //写入音频数据 aviWriter.WriteWavBuffer(buffer, length); } private void camerasFrameCb(byte[] buffer, int size) { //写入视频数据 aviWriter.WriteBmpBuffer(buffer, Math.Max(size, 640 * 480 * 3)); }
private void button2_Click(object sender, EventArgs e) { button2.Enabled = false; microphone.Stop(); camCapture.Stop(); aviWriter.Dispose();//写入完了记得关闭流 aviWriter = null; }