跛脚mp3改变比特率cpp

时间:2018-01-18 10:15:39

标签: c++ audio ffmpeg lame

我需要更改mp3比特率,我在Windows和Linux下使用lame for cpp项目。我想我需要将mp3转换为wav,然后将wav转换为具有所需比特率的mp3。

对于wav到mp3,我已经有了代码(使用跛脚)它的工作原理 对于mp3到wav,应用程序在hip_decode上崩溃存在问题 可能是由于文件开头的ID3标签造成的 如何跳过ID3标签或这是一个不同的问题?

这是我使用

的代码
bool Helper::convert2Wav(QString fromFile, QString toFile) {
    int read, write;
    int ctn = 0;

    FILE *mp3_in =fopen(fromFile.toLocal8Bit().data(),"rb");
    //this should be WAV
    FILE *pcm_out =fopen(toFile.toLocal8Bit().data(),"wb");

    const int PCM_SIZE = 4096;
    const int MP3_SIZE = 4096;

    short int pcm_buffer[PCM_SIZE*2];
    short int pcm_buffer1[PCM_SIZE];
    short int pcm_buffer2[PCM_SIZE];
    unsigned char mp3_buffer[MP3_SIZE];


    memset(pcm_buffer, 0, sizeof(pcm_buffer));
    memset(pcm_buffer1, 0, sizeof(pcm_buffer1));
    memset(pcm_buffer2, 0, sizeof(pcm_buffer2));
    memset(mp3_buffer, 0, sizeof(mp3_buffer));

    lame_t lame = lame_init();
    hip_t l = hip_decode_init();
    lame_set_in_samplerate(lame, 44100);
    //lame_set_VBR(lame, vbr_default);
    //lame_set_mode(lame, MONO);
    lame_init_params(lame);

    qDebug()<<"start decoding";

    do {
        ctn += 4096;
        qDebug()<<"a message:"<<ctn;
        read = fread(mp3_buffer, sizeof(unsigned char), MP3_SIZE, mp3_in);
        hip_decode(l, mp3_buffer, MP3_SIZE, pcm_buffer1, pcm_buffer2);

        //read = hip_decode(l, mp3_buffer, MP3_SIZE, pcm_buffer1,pcm_buffer2);

        //memcpy(pcm_buffer, pcm_buffer1, sizeof(pcm_buffer1));
        //memcpy(&pcm_buffer[PCM_SIZE], pcm_buffer2, sizeof(pcm_buffer2));
        //write = lame_encode_buffer_interleaved(lame, pcm_buffer, read,mp3_buffer, MP3_SIZE);
        //fwrite(pcm_buffer, 1, sizeof(pcm_buffer), pcm_out);

    } while (read != 0);

    hip_decode_exit(l);
    lame_close(lame);
    fclose(pcm_out);
    fclose(mp3_in);

    return true;
}

输出崩溃:

start decoding
a message: 4096
a message: 8192
a message: 12288
1   ??                                 0x7ffff7b8f0b5     
2   hip_decode1_headers                0x7ffff7b8f34b     
3   hip_decode1_headers                0x7ffff7b8f385     
4   hip_decode_headers                 0x7ffff7b8f42d     
5   hip_decode                         0x7ffff7b8f47c     
6   Helper::convert2Wav helper.cpp 498 0x5555555b0fe7     
7   ??                                 0x4490551063006a0  

        MP3_SIZE    258346945   int
        PCM_SIZE    234884892   int
        ctn 189860957   int
        l   1050198802094820962 hip_t
        lame    1165886731226582744 lame_t
        mp3_buffer  "\005\0029lM^J\035�xo�R����(����Rj\172175r�V�����ԡCb٥���0a�ע�r�4�A��7Xj�Q0u�\033Os��L5�S$D2\002\016� UxCp\014s��08\004+7��@"�r"... (4096)   unsigned char[4096]
        mp3_in  0xe0b0e1e0e060d4a   FILE*
        pcm_buffer  @0x7fffffff89a0 short[8192]
        pcm_buffer1 @0x7fffffff69a0 short[4096]
        pcm_buffer2 @0x7fffffff49a0 short[4096]
        pcm_out 0xc460bb00bb40c82   FILE*
        read    229772669   int
        write   <optimized out> 

似乎这不是ID3问题,我使用此代码https://searchcode.com/file/31230698/ID3.cpp来读取ID3,它显示没有ID3标签 但仍然在同一点崩溃:

Has ID3v2 Tag: No
  Tag size: 0
start decoding skip tag_size: 0
a message: 4096
a message: 8192
a message: 12288

1 个答案:

答案 0 :(得分:0)

回答我的蹩脚问题:

bool Helper::changeMp3Bitrate(QString fromFile, QString toFile, int sample_rate, int channels) {
    qDebug()<<"Helper::changeMp3Bitrate channels:"<<channels<<" sample_rate:"<<sample_rate;
    int read;
    int ctn = 0;
    int tag_size=0;

    ID3 tag(fromFile.toLocal8Bit().data());

    if(tag.hasTag()) {
        qDebug()<<"Helper::changeMp3Bitrate mp3 has tags";
        tag.parse();
    }
    else
        qDebug()<<"Helper::changeMp3Bitrate mp3 no tags";

    qDebug()<<"Helper::changeMp3Bitrate tagSize:"<< tag.size();

    if(tag.hasTag()) {
        tag_size=tag.size();
        qDebug()<<"Helper::changeMp3Bitrate tagSize:"<< tag.size();
    }

    FILE *mp3_in =fopen(fromFile.toLocal8Bit().data(),"rb");

    FILE *mp3_out =fopen(toFile.toLocal8Bit().data(),"wb");

    const int PCM_SIZE = 4096;
    const int MP3_SIZE = 4096;

    short int pcm_buffer[PCM_SIZE*100*2];
    short int pcm_buffer1[PCM_SIZE*100];
    short int pcm_buffer2[PCM_SIZE*100];
    unsigned char mp3_buffer[MP3_SIZE];
    unsigned char mp3_out_buffer[PCM_SIZE*100];


    memset(pcm_buffer, 0, sizeof(pcm_buffer));
    memset(pcm_buffer1, 0, sizeof(pcm_buffer1));
    memset(pcm_buffer2, 0, sizeof(pcm_buffer2));
    memset(mp3_buffer, 0, sizeof(mp3_buffer));
    memset(mp3_out_buffer, 0, sizeof(mp3_out_buffer));

    lame_t lame = lame_init();
    hip_t l = hip_decode_init();
    lame_set_in_samplerate(lame, sample_rate);
    lame_set_mode(lame, STEREO);
    lame_set_brate(lame,Zm().defaultBitrate);
    lame_set_num_channels(lame, channels);
    lame_init_params(lame);

    if(tag_size) {
        qDebug()<<"Helper::changeMp3Bitrate skip tag_size bytes:"<<tag_size;
        fread(NULL,sizeof(unsigned char), tag_size, mp3_in);
    }
    qDebug()<<"Helper::changeMp3Bitrate decoding";

    do {
        read = fread(mp3_buffer, sizeof(unsigned char), MP3_SIZE, mp3_in);
        int numdec=hip_decode(l, mp3_buffer, MP3_SIZE, pcm_buffer1, pcm_buffer2);
        ctn += read;
        qDebug()<<"Helper::changeMp3Bitrate mp3 read:"<<ctn;
        if(numdec>0) {
            qDebug()<<"Helper::changeMp3Bitrate mp3 numdec:"<<numdec;
            memcpy(pcm_buffer, pcm_buffer1, numdec);
            memcpy(&pcm_buffer[numdec], pcm_buffer2, numdec);

            int write = lame_encode_buffer(lame, pcm_buffer1,pcm_buffer2,numdec, mp3_out_buffer, sizeof(mp3_out_buffer));

            qDebug()<<"Helper::changeMp3Bitrate mp3 write to mp3_out:"<<write;

            fwrite(mp3_out_buffer, write, 1, mp3_out);
            memset(pcm_buffer, 0, sizeof(pcm_buffer));
            memset(pcm_buffer1, 0, sizeof(pcm_buffer1));
            memset(pcm_buffer2, 0, sizeof(pcm_buffer2));
            memset(mp3_buffer, 0, sizeof(mp3_buffer));
            memset(mp3_out_buffer, 0, sizeof(mp3_out_buffer));
        }

    } while (read != 0);

    hip_decode_exit(l);
    lame_close(lame);
    fclose(mp3_in);
    fclose(mp3_out);

    qDebug()<<"Helper::changeMp3Bitrate finished";
    return true;
}