gpt4 book ai didi

ios - Lame - 将比特率增加到 320

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:48 24 4
gpt4 key购买 nike

首先我想展示一下我通过 Lame 库将源 .wav 文件转换为 .mp3 的方法:

- (void)convertFromWav:(NSString *)sourceFilePath ToMp3:(NSString *)resultName {
NSString *mp3FileName = [resultName stringByAppendingString:@".mp3"];
NSString *mp3FilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:mp3FileName];

@try {
int read, write;
FILE *pcm = fopen([sourceFilePath UTF8String], "rb"); //source
if (pcm == NULL) {
perror("fopen");
return;
}
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output

const int sampleRate = 44100;
const int bitsPerSample = 16;
const int numberOfChannels = 2;

const int PCM_SIZE = 8192*2;
const int MP3_SIZE = 8192*2;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];

lame_t lame = lame_init();
lame_set_in_samplerate(lame, sampleRate);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);

lame_get_num_samples(lame);

long long fileSize = [[[[NSFileManager defaultManager] attributesOfItemAtPath:sourceFilePath error:nil] objectForKey:NSFileSize] longLongValue];
long duration = fileSize / (sampleRate * numberOfChannels * bitsPerSample / 8);//(fileSize * 8.0f) / (sampleRate * 2);

lame_set_num_samples(lame, (duration * sampleRate));
lame_get_num_samples(lame);

float percent = 0.0;
int totalframes = lame_get_totalframes(lame);

do {
read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
if (read == 0)
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
else
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);

fwrite(mp3_buffer, write, 1, mp3);

int frameNum = lame_get_frameNum(lame);
if (frameNum < totalframes)
percent = (100. * frameNum / totalframes + 0.5);
else
percent = 100;

if ([_delegate respondsToSelector:@selector(convertingProgressChangedWithPercent:)])
{
[_delegate convertingProgressChangedWithPercent:percent];
}

} while (read != 0);

lame_close(lame);
fclose(mp3);
fclose(pcm);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
}
@finally {
if ([_delegate respondsToSelector:@selector(convertingDidFinish:)])
{
[_delegate convertingDidFinish:mp3FilePath];
}
}
}

没关系,它正在工作。结果我得到了每秒 152000 位的 .mp3。但我想让它达到每秒 320000 位。我该如何改变它?我对这些东西的理论并不擅长,所以我不知道哪些值会改变什么。谢谢。

最佳答案

您想使用 lame_set_VBR (lame_t, vbr_off); 然后您可以使用 lame_set_brate 来设置所需的比特率。使用 vbr_off 为您提供 CBR 模式,正如文档中所确认的那样(参见 headers.h ):

*********************************************************************
VBR control
************************************************************************
/* Types of VBR. default = vbr_off = CBR */
int CDECL lame_set_VBR(lame_global_flags *, vbr_mode);
vbr_mode CDECL lame_get_VBR(const lame_global_flags *);

试试这个:

//# for constants of settings
const int sampleRate = 44100;
const int bitsPerSample = 16;
const int numberOfChannels = 2;

const int myBitRate = 320;

//# for Lame settings
lame_t lame = lame_init();
lame_set_in_samplerate (lame_t, sampleRate); //is 44100
lame_set_VBR (lame_t, vbr_off); //force CBR mode
lame_set_brate (lame_t, myBitRate); //is 320
lame_init_params (lame_t);

你也可以像这样设置 Lame :
lame_t lame = lame_init(); 变成这样:lame_t = lame_init();

只是说,如果您定义了一个 lame_t,我希望它在其余设置中需要该名称。你知道像 lame_init_params (lame_t);

关于ios - Lame - 将比特率增加到 320,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35151284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com