windows DirectShow pcm to mp3 代码实现
时间: 2024-04-02 17:35:53 浏览: 229
DirectShow过滤器-MP3解码过滤器
以下是使用DirectShow将PCM转换为MP3的C++代码示例:
```cpp
#include <dshow.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
// 初始化COM组件
CoInitialize(NULL);
// 创建FilterGraph对象
IGraphBuilder* pGraph = NULL;
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph);
// 创建WAV解码器
IBaseFilter* pWAVDecoder = NULL;
CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&pWAVDecoder);
// 创建MP3编码器
IBaseFilter* pMP3Encoder = NULL;
CoCreateInstance(CLSID_MPEG1Layer3Encoder, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&pMP3Encoder);
// 创建文件写入过滤器
IFileSinkFilter* pFileSink = NULL;
CoCreateInstance(CLSID_FileWriter, NULL, CLSCTX_INPROC_SERVER, IID_IFileSinkFilter, (void**)&pFileSink);
// 打开PCM文件
HANDLE hPCMFile = CreateFile("test.pcm", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hPCMFile == INVALID_HANDLE_VALUE)
{
printf("Failed to open PCM file.\n");
return -1;
}
// 创建PCM文件流
IStream* pPCMStream = NULL;
CreateStreamOnHGlobal(NULL, TRUE, &pPCMStream);
// 读取PCM数据
BYTE buffer[4096];
DWORD bytesRead;
while (ReadFile(hPCMFile, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0)
{
pPCMStream->Write(buffer, bytesRead, NULL);
}
// 关闭PCM文件
CloseHandle(hPCMFile);
// 将PCM流封装为WAV文件
WCHAR filename[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, "test.wav", -1, filename, MAX_PATH);
AM_MEDIA_TYPE mt;
mt.majortype = MEDIATYPE_Audio;
mt.subtype = MEDIASUBTYPE_PCM;
mt.formattype = FORMAT_WaveFormatEx;
mt.cbFormat = sizeof(WAVEFORMATEX);
mt.pbFormat = (BYTE*)CoTaskMemAlloc(mt.cbFormat);
ZeroMemory(mt.pbFormat, mt.cbFormat);
((WAVEFORMATEX*)mt.pbFormat)->wFormatTag = WAVE_FORMAT_PCM;
((WAVEFORMATEX*)mt.pbFormat)->nChannels = 2;
((WAVEFORMATEX*)mt.pbFormat)->nSamplesPerSec = 44100;
((WAVEFORMATEX*)mt.pbFormat)->nAvgBytesPerSec = 176400;
((WAVEFORMATEX*)mt.pbFormat)->nBlockAlign = 4;
((WAVEFORMATEX*)mt.pbFormat)->wBitsPerSample = 16;
IFileSinkFilter* pWAVSink = NULL;
pGraph->QueryInterface(IID_IFileSinkFilter, (void**)&pWAVSink);
pWAVSink->SetFileName(filename, &mt);
CoTaskMemFree(mt.pbFormat);
pGraph->AddFilter(pWAVDecoder, L"WAV Decoder");
pGraph->ConnectDirect(pWAVDecoder, L"Output", pWAVSink, NULL);
// 将WAV解码器输出连接到MP3编码器输入
IPin* pWAVOutput = NULL;
pWAVDecoder->FindPin(L"Output", &pWAVOutput);
IPin* pMP3Input = NULL;
pMP3Encoder->FindPin(L"In", &pMP3Input);
pGraph->ConnectDirect(pWAVOutput, pMP3Input, NULL);
// 设置MP3编码器参数
ICodecAPI* pCodecAPI = NULL;
pMP3Encoder->QueryInterface(IID_ICodecAPI, (void**)&pCodecAPI);
VARIANT val;
val.vt = VT_UI4;
val.ulVal = 192000; // 设置比特率为192Kbps
pCodecAPI->SetValue(&CODECAPI_AVEncMP3BitRate, &val);
// 将MP3编码器输出连接到文件写入过滤器
IPin* pMP3Output = NULL;
pMP3Encoder->FindPin(L"Out", &pMP3Output);
pFileSink->SetFileName(L"test.mp3", NULL);
pGraph->AddFilter(pMP3Encoder, L"MP3 Encoder");
pGraph->AddFilter((IBaseFilter*)pFileSink, L"File Writer");
pGraph->ConnectDirect(pMP3Output, (IPin*)pFileSink, NULL);
// 开始转换
IMediaControl* pControl = NULL;
pGraph->QueryInterface(IID_IMediaControl, (void**)&pControl);
pControl->Run();
// 等待完成
IMediaEvent* pEvent = NULL;
pGraph->QueryInterface(IID_IMediaEvent, (void**)&pEvent);
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// 释放资源
pControl->Release();
pEvent->Release();
pPCMStream->Release();
pWAVDecoder->Release();
pMP3Encoder->Release();
pFileSink->Release();
pGraph->Release();
CoUninitialize();
return 0;
}
```
这段代码中使用了DSoundRender作为WAV解码器,使用了MPEG1Layer3Encoder作为MP3编码器,使用了FileWriter作为文件写入过滤器。需要注意的是,不同的解码器和编码器可能有不同的接口和参数,需要根据实际情况进行调整。
阅读全文