蹩脚的mp3改变比特率cpp
Posted
技术标签:
【中文标题】蹩脚的mp3改变比特率cpp【英文标题】:lame mp3 change bitrate cpp 【发布时间】:2018-01-18 10:15:39 【问题描述】:我需要更改 mp3 比特率,我在 Windows 和 Linux 下将 lame 用于 cpp 项目。我想我需要将 mp3 转换为 wav,然后将 wav 转换为具有所需比特率的 mp3。
对于 wav 到 mp3,我已经有了代码(使用 lame),它可以工作。 对于 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
【问题讨论】:
这是一个非常蹩脚的问题。题外也。见How to Ask。 好的,让我们更具体 YSC - 我想你甚至没有读过我的问题 您是否阅读了lame
程序的源代码以了解它如何处理--mp3input
?我认为,您应该能够在频域内完成此操作,而无需进行 PCM 和返回(就像 JPEG 质量更改不需要进行光栅和返回一样)。
好吧,如果每个人都应该阅读每个程序的源代码以获得一些程序,那么***是干什么用的?尤其是la脚,网上没有那么多例子
【参考方案1】:
回答我的蹩脚问题:
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;
【讨论】:
以上是关于蹩脚的mp3改变比特率cpp的主要内容,如果未能解决你的问题,请参考以下文章