li-heng 2023-08-16 19:31 采纳率: 80.8%
浏览 17
已结题

C++实现将PCM文件转换成MP3文件

找一个C++实现将PCM文件转换成MP3文件的代码,要求转换前后播放效果没有差异,能正常的播放,要能成功转换,使用第三方库或者自己编写代码实现都行,谢谢!

  • 写回答

6条回答 默认 最新

  • DarrenPig 2023-08-16 20:15
    关注
    
    #include <stdio.h>
    #include <lame/lame.h>
    
    bool convertPcmToMp3(const char* pcmFilePath, const char* mp3FilePath, int sampleRate, int channels, int bitRate) {
        FILE *pcmFile = fopen(pcmFilePath, "rb");
        if (!pcmFile) {
            return false;
        }
    
        FILE *mp3File = fopen(mp3FilePath, "wb");
        if (!mp3File) {
            fclose(pcmFile);
            return false;
        }
    
        const int PCM_SIZE = 8192;
        const int MP3_SIZE = 8192;
    
        short pcmBuffer[PCM_SIZE * channels];
        unsigned char mp3Buffer[MP3_SIZE];
    
        lame_t lame = lame_init();
        lame_set_num_channels(lame, channels);
        lame_set_in_samplerate(lame, sampleRate);
        lame_set_out_samplerate(lame, sampleRate);
        lame_set_brate(lame, bitRate);
        lame_set_mode(lame, channels == 1 ? MONO : STEREO);
        lame_set_quality(lame, 2);
        lame_init_params(lame);
    
        bool success = true;
        int readSize = 0;
        int writeSize = 0;
        do {
            readSize = fread(pcmBuffer, sizeof(short) * channels, PCM_SIZE, pcmFile);
            if (readSize != 0) {
                if (channels == 1) {
                    writeSize = lame_encode_buffer(lame, pcmBuffer, NULL, readSize, mp3Buffer, MP3_SIZE);
                } else {
                    writeSize = lame_encode_buffer_interleaved(lame, pcmBuffer, readSize, mp3Buffer, MP3_SIZE);
                }
                fwrite(mp3Buffer, sizeof(unsigned char), writeSize, mp3File);
            } else {
                // Flush the last bit of data.
                writeSize = lame_encode_flush(lame, mp3Buffer, MP3_SIZE);
                fwrite(mp3Buffer, sizeof(unsigned char), writeSize, mp3File);
            }
        } while (readSize != 0);
    
        lame_close(lame);
        fclose(pcmFile);
        fclose(mp3File);
    
        return success;
    }
    
    int main() {
        const char* pcmFilePath = "input.pcm";
        const char* mp3FilePath = "output.mp3";
        int sampleRate = 44100;   // 采样率(Hz)
        int channels = 2;         // 声道数(1:单声道,2:立体声)
        int bitRate = 128;        // 比特率(kbps)
    
        bool success = convertPcmToMp3(pcmFilePath, mp3FilePath, sampleRate, channels, bitRate);
        if (success) {
            printf("PCM to MP3 conversion completed.\n");
        } else {
            printf("Failed to convert PCM to MP3.\n");
        }
    
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月18日
  • 创建了问题 8月16日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?