从视频中提取音频为 wav

发布于 2024-08-18 10:00:01 字数 292 浏览 1 评论 0原文

我知道有一个与我类似的问题:从视频文件中提取 wav 文件

我是 C++ 新手,了解 COM 库 + 视频和音频需要 directX。我一直在寻找教程和示例代码,但收效甚微。

我的问题是如何对应用程序进行编码以获取视频文件(任何类型)并将提取的音频保存为应用程序中的 .wav,而不是使用其他应用程序(例如 graphedit 或 virtualdub)?

I know there is a question similar to mine: Extract wav file from video file

I am new to C++ and understand about COM library + directX is needed for video and audio. I been looking for tutorial and samples code but little success.

My question is how do I code the application to take video file (any type) and saved the extracted audio as .wav in my application rather than using other applications such as graphedit or virtualdub?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2024-08-25 10:00:01

我将附议仅使用 ffmpeg 构建来执行音频提取的动议。它可以通过一个简单的命令来完成,而不是很可能使用数百行代码(如果您要检查处理不同视频格式和编解码器时可能发生的所有可能问题)。

ffmpeg -i video.avi -vn soundfile.wav

你可以使用 libavformat 和 libavformat(ffmpeg 后面的库)来做同样的事情,但是除非你需要在输出到 wav 之前对原始音频进行一些处理,否则除了知识之外什么也得不到。

ffmpeg 很好,因为可执行文件包含您可能需要的所有音频和视频解码器,因此该解决方案具有高度可移植性。你没有安装编解码器或任何东西。输入视频文件可以是 ffmpeg 支持的任何格式或编解码器,您不必费心在代码中对它们进行不同的处理。

在 C++ 中,您可以通过在代码中构建命令行字符串并从代码中启动该过程来调用 ffmpeg(作为 C++ 新手,您可能需要研究如何执行此操作,但这非常简单)。

I'll second the motion to just use a build of ffmpeg to perform the audio extraction. It can be done in one easy command as opposed to most likely hundreds of lines of code (If your going to check for all of the possible problems that could happen when dealing with different video formats and codecs).

ffmpeg -i video.avi -vn soundfile.wav

You could use libavformat and libavformat(libraries behind ffmpeg) to do the same thing, but unless you need to do some processing on the raw audio before outputting to wav, there would be nothing to gain except for knowledge.

ffmpeg is nice because the executable contains all of the audio and video decoders you'll probably ever need so the solution is highly portable. You don't have it install codecs or anything. The input video file can be in any format or codec that ffmpeg supports and you don't have to bother with treating them differently in your code.

From C++ you can call ffmpeg by building the command line string in your code and kicking off the process from your code (being new the C++, you'll probably need to research how to do this, but it's pretty easy).

碍人泪离人颜 2024-08-25 10:00:01

你不能使用像 ffmpeg 这样的东西,或者它使用的库之一吗?或者也许是 mencoder,它也可以做同样的事情。据我所知,它们都有一个命令行界面,而且它们可能也有一些 API......

Can't you use something like ffmpeg, or one of the libraries it uses? Or maybe mencoder, which can do the same. Both of them have a command line interface as far as I know, and they might have some API as well...

心欲静而疯不止 2024-08-25 10:00:01

您可以使用 Directshow 过滤器构建一个图表,将音频另存为 .wav。

您需要使用的接口是: (注意:此解决方案将从 avi 文件中提取音频)

IGraphBuilder:这将用于构建图形。

IBaseFilter:这将是您初始化为图表一部分的过滤器

要初始化图表,您需要执行以下操作:

IGraphBuilder *pGraph = NULL;
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph)

CLSID_FilterGraph 在 uuids.h 中定义,uuids.h 是 PaltformSDK 的一部分。

图表初始化后,您将需要初始化 3 个将添加到图表中的过滤器。

  1. AVI 多路复用器:CLSID_AviDest
  2. 文件写入器:CLSID_FileWriter。
  3. 空渲染器:CLSID_NullRenderer

您可以通过以下方式初始化过滤器:

IBaseFilter *pF = NULL;
CoCreateInstance(clsid, 0, CLSCTX_INPROC_SERVER,  IID_IBaseFilter, (void**)&pF);
clsid = clsid of the filter

并使用以下方式在图表中添加过滤器:

pGraph->AddFilter(pF, name)
name = name of the filter. Can be 'AVI Mux' etc

初始化“文件写入器”过滤器后,您将需要设置要写入文件的路径。您可以这样做:

IFileSinkFilter* pFileSink=NULL;
 fileWriterFilter->QueryInterface(IID_IFileSinkFilter, (void**)&pFileSink);
pFileSink->SetFileName(filepath, NULL);


Here: fileWriter = file writer filter instance.

确保文件名的扩展名为 .wav

在图表中添加过滤器后,您将需要渲染视频文件,如下所示:

pGraph->RenderFile(sourcePath, NULL);

渲染后,您现在需要运行这个图。您可以通过从图表中查询几个接口来完成此操作:

IMediaControl 用于运行过滤器

IMediaEvent 用于从图表中获取事件。

查询界面:

pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
and pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

运行图形:

pControl->Run();

等待渲染完成:

pEvent->WaitForCompletion(INFINITE, &evCode);

完成后,您将发现一个包含 .wav 格式音频的文件。

我已经通过 graphedit 对此进行了测试并且它有效。我希望这会有所帮助。

You can use Directshow filters to construct a graph that will save the audio as .wav.

The interfaces that you need to use are: (Note: This solution will extract audio from avi files)

IGraphBuilder: This will be used to build graph.

IBaseFilter: This will be the filters that you initialize to make part of the graph

To initialize graph you do:

IGraphBuilder *pGraph = NULL;
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph)

CLSID_FilterGraph is defined in uuids.h which is part of PaltformSDK.

Once the graph is initialized, you will need to initialize 3 filters that will be added in the graph.

  1. AVI Multiplexer: CLSID_AviDest
  2. File Writer: CLSID_FileWriter.
  3. Null renderer: CLSID_NullRenderer

You can initialize filters by:

IBaseFilter *pF = NULL;
CoCreateInstance(clsid, 0, CLSCTX_INPROC_SERVER,  IID_IBaseFilter, (void**)&pF);
clsid = clsid of the filter

And add the filter in graph using:

pGraph->AddFilter(pF, name)
name = name of the filter. Can be 'AVI Mux' etc

Once you initialize 'File writer' filter you will need to set the path where you wish to write the file. You can do that:

IFileSinkFilter* pFileSink=NULL;
 fileWriterFilter->QueryInterface(IID_IFileSinkFilter, (void**)&pFileSink);
pFileSink->SetFileName(filepath, NULL);


Here: fileWriter = file writer filter instance.

Make sure that the extension of file name is .wav

Once you added the filters in graph, you will need to render the video file like:

pGraph->RenderFile(sourcePath, NULL);

Once rendered, you will now need to Run this graph. You can do this by querying couple of interfaces from the graph:

IMediaControl Used to run the filter

and IMediaEvent Used to get events from graph.

Query the interface:

pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
and pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

Run the graph:

pControl->Run();

And wait for the rendering for completion:

pEvent->WaitForCompletion(INFINITE, &evCode);

Once done, you will find a file having audio in .wav format.

I have tested this through graphedit and it works. I hope this will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文