Record directshow audio device to file

2019-09-02 00:10发布

问题:

I've stumbled through some code to enumerate my microphone devices (with some help), and am able to grab the "friendly name" and "clsid" information from each device.

I've done some tinkering with GraphEd.exe to try and figure out how I can take audio from directshow and write it to a file (I'm not currently concerned about the format, wav should be fine), and can't seem to find the right combination.

One of the articles I've read linked to this Windows SDK sample, but when I examined the code, I ended up getting pretty confused at how to use that code, ie. setting the output file, or specifying which audio capture device to use.

I also came across a codeguru article that has a nicely featured audio recorder, but it does not have an interface for selecting the audio device, and I can't seem to find where it statically picks which recording device to use.

I think I'd be most interested in figuring out how to use the Windows SDK sample, but any explanation on either of the two approaches would be fantastic.

Edit: I should mention my knowledge and ability as a win32 COM programmer is very low on the scale, so if this is easy, just explain it to me like I'm five, please.

回答1:

Recording audio into file with DirectShow needs you to build the right filter graph, as you should have figured out already. The parts include:

  1. The device itself, which you instantiate via moniker (not CLSID!), it is typically PCM format
  2. Multiplexer component that converts streams into container format
  3. File Writer Filter that takes file-compatible stream and writes into a file

The tricky moment is #2 since there is not standard component available. Windows SDK samples however contains the missing part - WavDest Filter Sample. Building it and making it ready for use, you can build a graph that records from device into .WAV file.

Your graph will look like this, and it's built easily programmatically as well:

I noticed that I have a variation of WavDest installed with Google Earth - for the case you have troubles building it yourself and you will be looking for prebuilt binary.



回答2:

You can instruct ffmpeg to record from a directshow device, and output to a file.