Use DirectX. Capture. dll and dshownet. DLL to capture video programs

Source: Internet
Author: User

 
Http://www.codeproject.com/cs/media/directxcapture.asp from:
I will help the following friends who are not very good at English

Sample download:
Http://files.cnblogs.com/mgod/DirectXCapture_src.zip
Http://files.cnblogs.com/mgod/DirectXCapture_demo.zip

 

Description:

Now I will introduce the class library I use to you. DirectX. Capture class library(By Brian low.) It is a class library I am looking for on codeproject. It is fully written by C #. It is based on the directshow.net class library for capturing audio and video, and can be saved as an AVI file, you can easily download the source files of these two class libraries from the codeproject.
Main features of this Class Library:

    • You can list and select all audio and video hardware devices.
    • You can set audio and video attributes, such as frame rate, size, and sampling frequency.
    • Supports audio/video compression and encoding
    • Supports video preview.
    • Support for TV Interfaces
    • Supports sound mixing.
    • Displays hardware driver attributes.
    • Includes standard msdn help documentation

When using this class library, you must introduceDirectX. Capture. dll andDshownet. dllTo your project.

Example 1:
UsingDirectX. Capture

// Capture the default Audio/Video device

Capture capture =NewCapture (filters. videoinputdevices [0],
Filters. audioinputdevices [0]);

// Start capturing
Capture. Start ();

// Stop capturing
Capture. Stop ();

Example 2:

Capture capture = New Capture (filters. videoinputdevices [ 0 ],
Filters. audioinputdevices [ 1 ]);

// Which compression encoding method can be set here
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 ();

Example 3: preview

// Start Preview
Capture. previewwindow = mypanelcontrol;

// Stop Preview
Capture. previewwindow =Null;


This class library starts from the driver of the device every time.ProgramTo update the latest supported properties, you do not have to worry about whether the device supports specific properties. You only need to select from the supported properties.


Of course, this class library also has some problems. For example, the video card support is not very good, but if you don't need it, just operate on common video and audio devices, this class library is still very useful,

--------------------- The original text is as follows ---------------------------- 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 encoded DED
Using the code

TheCaptureClass is the core of this library. Here is a simple example:

  //   remember to add a reference to DirectX. capture. DLL   using  DirectX. capture  //   capture using the first video  ///   and audio devices available  capture =  New  capture (filters. videoinputdevices [ 0 ], filters. audioinputdevices [ 0 ]);  //   Start capturing  capture. start ();  //   stop capturing  capture. stop (); 

Remember to add a reference in your projectDirectX. Capture. dll. This dll requiresDshownet. dll, So make sure they are both in the same directory. Once you add the reference, Visual Studio. NET shocould 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, passNullAs the second parameter to the constructor.

The class is initialized to a valid temporary file in the windowsTempFolder. To capture to a different file, setCapture. filenameProperty before you begin capturing.

A second example

This next example shows how to change video and audio settings. properties suchCapture. framerateAndCapture. audiosamplesizeAllow you to programmatically adjust the capture. UseCapture. videocapsAndCapture. audiocapsTo determine valid values for these properties.

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 1 GB of disk space per minute. whenever possible, setCapture. videocompressorAndCapture. audiocompressorProperties 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 shoshould 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 thedshownet project. ThisCaptureLibrary uses dshownet for the InterOP layer with only a few extensions. This isDshownet. dllMentioned earlier.

Sitting on top of all of this isCaptureClass 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. 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:

 
//Start PreviewCapture. previewwindow = mypanelcontrol;//Stop PreviewCapture. 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 untilPreviewwindowProperty 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

overview 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.

  //   audiosamplesize is retrieved from DirectShow each iteration   for  ( int  C =  0 ; c  32 ; C ++) { If  (C = cap Ture. 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 have CT most video capture cards and sound cards will work. you may have trouble with TV tuner cards and DV devices (FireWire camcorders) though they shocould 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 shoshould 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:

    • Frequency overrides for the tuner class by fdaupias
    • Radio tuning for the tuner class by dauboro

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.

License

This article, along with any associated source code and files, is licensed under a public domain dedication

About the author

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.