2

Here i have m4a format audio, I need to convert into mp3 format.

for this I used the code as

- (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];
    }
}

How can i convert from m4a to mp3 format.kindly help me

dineshprasanna
  • 1,284
  • 4
  • 20
  • 37

3 Answers3

2

Try this,

@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]);
}
SBM
  • 1,025
  • 9
  • 23
  • 3
    What changed (I am not the `diff` utility)? – trojanfoe Dec 19 '13 at 10:32
  • 1
    its not working. after conversion if i play, it's producing some noisy sound.Can you help me, further level what i need to do.? – iTag Sep 20 '15 at 18:48
  • hey SBM, sorry to bother you but i can see that you now how to do it .. i have m4a formatted into NSData and i want to convert it to mp3 and then change this mp3 to NSData .. can you help me with this ? THANKS – user3783005 Nov 17 '15 at 16:15
  • 1
    Hi @SBM. my issue is the same. i get noisy output that too of 1min, but my song length is 5mins – Sana Jan 07 '16 at 11:10
1

An iOS project to convert audio from any format to any format. For that Its easy to use ExtAudioConverter

amisha.beladiya
  • 363
  • 1
  • 12
  • I tried that and it does seem to work.... however when uploading the audio file to Tumblr, I got the following error: ```This is not an MP3 file```. So I checked the file with an MP3 validator program and it stated the following conversion issue: ```It seems that file is truncated or there is garbage at the end of the file, at 176305. VBR detected, but no VBR header is present. Seeking may not work properly. No supported tags in the file.``` - So it seems that the converted file is not 100% valid, when using this library. – Supertecnoboff Mar 05 '18 at 17:42