5

Objective: I want to extract an audio from video in mp3 format. Video can be in any iOS supported format

I have tried the following technique to achieve the above objective and i am using lame library:

Step 1: Create an AVURLAsset object by passing source file URL to it.

Step 2: Create an AVAssetExportSession object with source asset set its outputfileType to m4a.

Step 3: Extracts its audio and after that i try to convert it into mp3 format.

Here is the code that i have used:

NSURL *videoFileUrl = [NSURL fileURLWithPath:originalVideoPath];
AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:videoFileUrl options:nil];

exportSession=[AVAssetExportSession exportSessionWithAsset:anAsset presetName:AVAssetExportPresetPassthrough];

[exportSession determineCompatibleFileTypesWithCompletionHandler:^(NSArray *compatibleFileTypes) {
    NSLog(@"compatiblefiletypes: %@",compatibleFileTypes);
}];

NSURL *furl = [NSURL fileURLWithPath:tmpVideoPath];
exportSession.outputURL = furl;
exportSession.outputFileType=AVFileTypeAppleM4A;

CMTime duration = anAsset.duration;
CMTimeRange range = CMTimeRangeMake(kCMTimeZero, duration);
exportSession.timeRange = range;

[exportSession exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
    switch (exportSession.status)
    {
        case AVAssetExportSessionStatusCompleted:
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                    [SVProgressHUD showProgress:0 status:@"Converting..." maskType:SVProgressHUDMaskTypeGradient];
                    [self performSelector:@selector(convertToMp3) withObject:nil afterDelay:0.3f];       
            });
            break;
        }
        case AVAssetExportSessionStatusFailed:
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MyUtility showAlertViewWithTitle:kAlertTitle msg:exportSession.error.localizedDescription];
            });
            break;
        }
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"Export canceled");
            break;
        default:

            break;
    }
}];

__weak AVAssetExportSession *weakSession = exportSession;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    while (weakSession.status == AVAssetExportSessionStatusWaiting
           || weakSession.status == AVAssetExportSessionStatusExporting) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            [SVProgressHUD showProgress:exportSession.progress status:@"Extracting..." maskType:SVProgressHUDMaskTypeGradient];
        });
    }
});
- (void)convertToMp3
{
NSURL *extractedAudioFileURL = [[NSURL alloc] initWithString:tmpVideoPath];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:extractedAudioFileURL error:&error];

float noOfChannels = [[audioPlayer.settings objectForKey:AVNumberOfChannelsKey] floatValue];
float sampleRate = [[audioPlayer.settings objectForKey:AVSampleRateKey] floatValue];
float bitRate = 16;//[[audioPlayer.settings objectForKey:AVLinearPCMBitDepthKey] floatValue];

@try {
    int read, write;

    FILE *pcm = fopen([tmpVideoPath cStringUsingEncoding:1], "rb");  //source
    fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
    FILE *mp3 = fopen([tmpMp3FilePath 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, sampleRate);
    lame_set_VBR(lame, vbr_default);
    lame_init_params(lame);

    long long fileSize = [[[[NSFileManager defaultManager] attributesOfItemAtPath:tmpVideoPath error:nil] objectForKey:NSFileSize] longLongValue];
    long duration = (fileSize * 8.0f) / (sampleRate * noOfChannels);

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

    int percent     = 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 = (int) (100. * frameNum / totalframes + 0.5);
        else
            percent = 100;

        [SVProgressHUD showProgress:percent status:@"Converting..." maskType:SVProgressHUDMaskTypeGradient];

        NSLog(@"progress: %d",percent);

    } while (read != 0);

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

The resultant audio that i get has nothing but a noise and its duration is also wrong. I googled and found that the "libmp3lame" only understand the Linear PCM audio where as the m4a is compressed audio format.

Now how can i convert the audio into mp3 format from m4a or any other way to directly extract audio from video in mp3 format.

Thanks.

Muhammad Zeeshan
  • 2,441
  • 22
  • 33

1 Answers1

0

Instead of exporting to AVFileTypeAppleM4A try exporting to AVFileTypeAIFF. This will give you the Linear PCM that the lame encoder needs. The interim file will be larger than the m4a, obviously, but that's probably the only difference you will notice.

nsdebug
  • 364
  • 2
  • 8
  • Thanks for your answer but unfortunately my app crashed if i set the outputfiletype to AVFileTypeAIFF and the reason is this format is not compatible with .mov video – Muhammad Zeeshan Mar 16 '14 at 15:43
  • Hmmm...I've never had a situation where an export session couldn't export to a particular format that it supported. If it can read the data, it should be able to export the data. Not sure what's happening there then, sorry. – nsdebug Mar 16 '14 at 17:03
  • @MuhammadZeeshan I'm facing the same issue as you have faced. Have you found any solution to this? – Parul Garg Nov 06 '15 at 16:34
  • Sorry i didn't found any solution of this problem. – Muhammad Zeeshan Nov 09 '15 at 09:48