DirectX.Capture Class Library

Brian Low,              29 Mar 2008                                      Public Domain

   4.94 (222 votes)

1

2

3

4

5

4.94/5 - 222 votes
5 removed
μ 4.86, σa 0.93 [?]
 
Rate this:
            Please Sign up or sign in to vote.            
A .NET class library for capturing video and audio to AVI files.

Introduction

This article presents a class library for capturing audio and video to AVI files in .NET. Some of the features of this library:

  • List and select hardware devices
  • Access to common audio and video settings (e.g. frame rate, size)
  • Support audio and video compression codecs
  • Support video preview
  • Support TV tuners
  • Support crossbars and audio mixers
  • Retrieve device capabilities
  • Show property pages exposed by drivers
  • MSDN-style documentation included

Using the Code

The Capture class is the core of this library. Here is a simple example:

Hide    Copy Code
// Remember to add a reference to DirectX.Capture.dll
using DirectX.Capture // Capture using the first video
// and audio devices available
Capture capture = new Capture( Filters.VideoInputDevices[0],
Filters.AudioInputDevices[0] ); // Start capturing
capture.Start(); // Stop capturing
capture.Stop();

Remember to add a reference in your project to DirectX.Capture.dll. This DLL requires DShowNET.dll, so make sure they are both in the same directory. Once you add the reference, Visual Studio .NET should take care of the copying for you.

This example will capture video and audio using the first video and audio devices installed on the system. To capture video only, pass a null as the second parameter to the constructor.

The class is initialized to a valid temporary file in the Windows temp folder. To capture to a different file, set the Capture.Filename property before you begin capturing.

A Second Example

This next example shows how to change video and audio settings. Properties such as Capture.FrameRate and Capture.AudioSampleSize allow you to programmatically adjust the capture. Use Capture.VideoCaps and Capture.AudioCaps to determine valid values for these properties.

Hide    Copy Code
Capture capture = new Capture( Filters.VideoInputDevices[0],
Filters.AudioInputDevices[1] ); capture.VideoCompressor = Filters.VideoCompressors[0];
capture.AudioCompressor = Filters.AudioCompressors[0]; capture.FrameRate = 29.997; // NTSC
capture.FrameSize = new Size( 640, 480 ); // 640x480
capture.AudioSamplingRate = 44100; // 44.1 kHz
capture.AudioSampleSize = 16; // 16-bit
capture.AudioChannels = 1; // Mono capture.Filename = "C:\MyVideo.avi"; capture.Start();
...
capture.Stop();

The example above also shows the use of video and audio compressors. In most cases, you will want to use compressors. Uncompressed video can easily consume over 1GB of disk space per minute. Whenever possible, set the Capture.VideoCompressor and Capture.AudioCompressor properties as early as possible. Changing them requires the internal filter graph to be rebuilt which often causes most of the other properties to be reset to default values.

Behind the Scenes

This project uses 100% DirectShow to capture video. Once a capture is started, DirectShow spawns another thread and handles retrieving/moving all the video and audio data itself. That means you should be able to capture at the same speed and quality as an application written in C.

DirectShow is implemented as a set of COM components and we use .NET Interop to access them. The pioneering work on this was done by NETMaster with the DShowNET project. This Capture library uses DShowNET for the interop layer with only a few extensions. This is the DShowNET.dll mentioned earlier.

Sitting on top of all of this is the Capture class library. The center of any DirectShow app is the filter graph and the filter graph manager. For a good overview, see The Filter Graph and Its Components from the MSDN.

The Least Work Possible

The library tries at all times to do the least amount of work possible. The problem is: DirectShow is very flexible, but has few firm standards for driver developers and I have limited hardware to test with. As a result, the class tries to avoid doing any work that may not be necessary, hopefully avoiding potential incompatibilities in the process.

One example is video preview. You can start and stop preview with:

Hide    Copy Code
// Start preview
capture.PreviewWindow = myPanelControl; // Stop preview
capture.PreviewWindow = null;

Hopefully this is simple to use. Internally, DirectShow does a lot of work: add required upstream filters for WDM devices, search for preview pins, use the Overlay Manager for video ports (hardware overlays), insert SmartTee filters when a separate preview pin is not available and more. Instead of rendering the preview stream as soon as the class is created, the class waits until the PreviewWindow property is set.

For developers who don't need preview, none of this work will ever be done. That means your application is more likely to work on a wider range of hardware. For developers that do need preview, this makes it easier to locate the cause of the problem and fix it or handle it gracefully.

Performance Tips

Many of the properties on the Capture class are retrieved directly from the underlying DirectShow COM components. If you need to refer to the property repeatedly in a block of code, take a copy of the value and use your copy.

Hide    Copy Code
// AudioSampleSize is retrieved from DirectShow each iteration
for ( int c = 0; c < 32; c++ )
{
if ( c == capture.AudioSampleSize )
MessageBox.Show( "Found!" );
} // A faster solution
int x = capture.AudioSampleSize;
for ( int c = 0; c < 32; c++ )
{
if ( c == x )
MessageBox.Show( "Found!" );
}

Why doesn't the class simply cache the value internally? We don't know when the filter (device driver) will change this value, so we have to retrieve the value every time. This means you will always get the real value of the property.

Credits

The DirectShow interop layer was developed by NETMaster in the DShowNET project. The MDSN-style documentation was generated from the source code using nDoc.

Troubleshooting

I have tested this with an Asus v7700 (NVidia GeForce2, reference drivers) and my onboard sound card. I can't guarantee any other hardware will work. However, I expect most video capture cards and sound cards will work. You may have trouble with TV Tuner cards and DV devices (Firewire camcorders) though they should be solvable.

Try the AMCap sample from the DirectX SDK (DX9\Samples\C++\DirectShow\Bin\AMCap.exe) or Virtual VCR, a free DirectShow capture application.

This class library uses COM Interop to access the full capabilities of DirectShow, so if there is another application that can successfully use a hardware device then it should be possible to modify this class library to use the device. Please post your experiences, good or bad, in the forum below.

User Enhancements

The following enhancements have been posted to the discussion board:

Thanks to fdaupias and dauboro for their submissions. I have not had time to post a tested, updated version with these enhancements. If anyone wants to make an updated download zip, mail it to me and I will added it to this page. Keep the enhancements coming.

DirectX.Capture Wiki

A Wiki for this project is available here. This Wiki can be edited by anyone, no registration is required. I hope this Wiki will allow interested users to more easily collaborate on this project. New versions, enhancements, tips and tricks can be posted on the Wiki.

&amp;amp;lt;a href="https://pubads.g.doubleclick.net/gampad/jump?iu=/6839/lqm.codeproject.site/Multimedia/DirectX/General&amp;amp;amp;sz=300x250&amp;amp;amp;c=67397"&amp;amp;gt;&amp;amp;lt;img src="https://pubads.g.doubleclick.net/gampad/jump?iu=/6839/lqm.codeproject.site/Multimedia/DirectX/General&amp;amp;amp;sz=300x250&amp;amp;amp;c=67397" width="300px" height="250px" target="_blank"/&amp;amp;gt;&amp;amp;lt;/a&amp;amp;gt;

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

DirectX.Capture Class Library的更多相关文章

  1. DirectX.Capture Namespace

    DirectX.Capture Class Library   DirectX.Capture Namespace The DirectX.Capture namespace contains cla ...

  2. DirectShowNet 使用摄像头录像+录音

    http://www.cnblogs.com/endv/p/6052511.html // ------------------------------------------------------ ...

  3. net 中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net) (转)

    作者:王先荣前言    随着Windows操作系统的不断演变,用于捕获视频的API接口也在进化,微软提供了VFW.DirectShow和MediaFoundation这 三代接口.其中VFW早已被Di ...

  4. AudioCapabilities成员

    参考:http://www.ajerp.com/Cs/DirectX/Capture/DirectX.Capture.html 使用有道翻译2.0 AudioCapabilities成员 公共实例字段 ...

  5. cadence 16.6 Pspice 仿真步骤

    从ADI官网下载后缀为 cir 的文件,AD8210 为例 进行仿真 1 打开 Cadence -> Release 16.6 -> PSpice Accessories -> Mo ...

  6. ubuntu 深度学习cuda环境搭建,docker-nvidia 2019-02

    ubuntu 深度学习cuda环境搭建 ubuntu系统版本 18.04 查看GPU型号(NVS 315 性能很差,比没有强) 首先最好有ssh服务,以下操作都是远程ssh执行 lspci | gre ...

  7. configure: error: The LBL Packet Capture Library, libpcap, was not found!

    configure: error:  The LBL Packet Capture Library, libpcap, was not found! yum install libpcap*

  8. DirectX.DirectSound声音播放资料

    参考:https://msdn.microsoft.com/en-us/library/windows/desktop/bb318665(v=vs.85).aspx Microsoft DirectS ...

  9. DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记

    今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...

随机推荐

  1. TCP字节流和UDP数据报区别

    两者的区别在于TCP接收的是一堆数据,而每次取多少由主机决定;而UDP发的是数据报,客户发送多少就接收多少. 拥有这些区别的原因是由于TCP和UDP的特性不同而决定的.TCP是面向连接的,也就是说,在 ...

  2. JavaScript使用DeviceOne开发实战(五)仿ZAKER应用

    关于index底下切换的的组件,可以用ViewShower实现,详见: do_ViewShower http://bbs.deviceone.net/forum.php?mod=viewthread& ...

  3. Netbeans+CodeIgniter搭建PHP开发环境

    为什么选这样的开发环境 常见的PHP集成开发环境有Eclipse+PDT,NetBeans,PHPEclipse,Zend Studio,PhpStorm等,我经过多方面的考虑(费用.易用性.功能.活 ...

  4. IOS Animation-CAShapeLayer、UIBezierPath与Animation的结合

    在阅读本文之前,对CAShapeLayer.UIBezierPath不熟悉的话,可以先阅读文章 贝塞尔曲线与Layer 如果对动画不熟悉的话,先阅读文章 动画基础.深入 Layer是绘图的画板,Bez ...

  5. IOS Block-Block块的使用与理解

    在IOS中,block块是新添加的语法,其他程序语言中也被称为闭包. 程序块的理念是像任何其他C语言类型一样对待特定的代码块.程序块可以分配给一个变量,以参数的形式传递给函数或方法,当然也可以执行(不 ...

  6. MVVM架构~knockoutjs系列之包括区域级联列表的增删改

    返回目录 这个例子我做了几次,之前总是有BUG,目前测试后,确定没有BUG才发上来的,主要功能是实现“我的银行”模块的增删改的功能,这个里面包括了级联列表的区域选择,这部分是难点,在开发过程中,我们应 ...

  7. 爱上MVC3~在控制器或Action上动态设定模板页(Layout)

    回到目录 很多境况下,我们需要设置自己模块的layout,即它的布局页面,在MVC2中叫它模板页面,你可以在return view方法时设置它,当然,这不是一种好方法,因为我不想每个action都去设 ...

  8. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  9. C#并行编程-PLINQ:声明式数据并行

    目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C#并行编程-线程同步原语 C#并行编程-PLINQ:声明式数据并行 背景 通过LINQ可 ...

  10. Eclipse中java向数据库中添加数据,更新数据,删除数据

    前面详细写过如何连接数据库的具体操作,下面介绍向数据库中添加数据. 注意事项:如果参考下面代码,需要 改包名,数据库名,数据库账号,密码,和数据表(数据表里面的信息) package com.ning ...