2

这里我有 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 格式。请帮助我

4

3 回答 3

2

尝试这个,

@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]);
}
于 2013-12-19T10:14:03.853 回答
1

一个将音频从任何格式转换为任何格式的 iOS 项目。为此,它易于使用ExtAudioConverter

于 2016-12-09T05:26:03.097 回答
0

您可以尝试使用 CoreAudio

和 LAME 编解码器(出于许可原因,这不包含在 iOS 中)

通过 CoreAudio 转换音频类型的一些示例:http: //developer.apple.com/library/mac/#samplecode/ConvertFile/Introduction/Intro.html

和 CoreAudio 文档:http: //developer.apple.com/library/mac/#documentation/MusicAudio/Conceptual/CoreAudioOverview/Introduction/Introduction.html

和 CoreAudio 支持的文件类型:http: //developer.apple.com/library/mac/#documentation/MusicAudio/Conceptual/CoreAudioOverview/SupportedAudioConverterFormats/SupportedAudioConverterFormats.html#//apple_ref/doc/uid/TP40003577-CH7-SW1

于 2015-10-13T11:29:35.333 回答