gpt4 book ai didi

ios - 以编程方式将 .m4a 转换为 .mp3 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:08:14 25 4
gpt4 key购买 nike

我这里有m4a格式的音频,我需要转换成mp3格式。

为此,我将代码用作

- (void) toMp3{
NSString *m4aFilePath = pathToSave;
NSString *mp3FileName = [NSString stringWithFormat:@"Audio%d",[audioArr count]];
mp3FileName = [mp3FileName stringByAppendingString:@".mp3"];
pathToSave = [NSString stringWithFormat:@"%@/%@", DOCUMENTS_FOLDER, mp3FileName];
NSLog(@"pathToSave :%@",pathToSave);
@try {
int read, write;

FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([pathToSave cStringUsingEncoding:1], "wb"); //output

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

lame_t lame = lame_init();
lame_set_in_samplerate(lame, 44100);
lame_set_VBR(lame, vbr_default);
lame_init_params(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);

} while (read != 0);

lame_close(lame);
fclose(mp3);
fclose(pcm);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
}
@finally {
[self performSelectorOnMainThread:@selector(convertMp3Finish)
withObject:nil
waitUntilDone:YES];
}
}

我如何将 m4a 格式转换为 mp3 格式。请帮助我

最佳答案

试试这个,

@try
{
int read, write;
FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source
fseek(pcm, 4*1024, SEEK_CUR); //skip file header

mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output
const int PCM_SIZE = 8192*3;
const int MP3_SIZE = 8192*3;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];

lame_t lame = lame_init();
lame_set_in_samplerate(lame, 11025*2);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);

int nTotalRead=0;

do {
read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);

nTotalRead+=read*4;

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);
// write = lame_encode_buffer(lame, pcm_buffer,pcm_buffer, read, mp3_buffer, MP3_SIZE);

fwrite(mp3_buffer, write, 1, mp3);
} while (read != 0);

lame_close(lame);
fclose(mp3);
fclose(pcm);
}
@catch (NSException *exception)
{
NSLog(@"%@",[exception description]);
}

关于ios - 以编程方式将 .m4a 转换为 .mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19292323/

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