0

I use AVAudioRecorder to record sound, the out put file format is .caf. And then I add a background sound (which is also in .caf format) onto the audio that I recorded, with AVMutableComposition. It works perfectly when I try to play it.

Now I want to export it and save it with the following code. However, the filesize of the exported file increase a lot. (for instance, the audio that I record is 16045byte, the background sound is 50400byte, and the exported file is 246472byte which is larger than the sum of the two file by times.)

AVAssetExportSession *export = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
export.audioMix = audioMix;
export.outputFileType = AVFileTypeAppleM4A;
NSString *exportURL = [NSString stringWithFormat:@"%@/export.m4a", DOCUMENTS_FOLDER];
NSURL *outputURL = [NSURL fileURLWithPath:exportURL];
export.outputURL = outputURL;

[export exportAsynchronouslyWithCompletionHandler:^{
    int exportStatus = export.status;

    switch (exportStatus) {
       // check the status and do the handling
    }
}];

}

So, can some one teach me to how I can make the output audio file smaller? Thanks.

Newbie
  • 2,775
  • 6
  • 33
  • 40

1 Answers1

0

Read more about fileTypes here:

https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVFoundation_Constants/Reference/reference.html#//apple_ref/doc/uid/TP40009539:

NSString *const AVFileTypeAIFC;
NSString *const AVFileTypeAIFF;
NSString *const AVFileTypeAMR;
NSString *const AVFileTypeCoreAudioFormat;
NSString *const AVFileTypeAppleM4V;
NSString *const AVFileTypeMPEG4;
NSString *const AVFileTypeAppleM4A;
NSString *const AVFileTypeQuickTimeMovie;
NSString *const AVFileTypeWAVE

AIFC might be interesting for you since it's compressed.

You will have to update also presetName:AVAssetExportPresetAppleM4A to presetName:AVAssetExportPresetPassthrough

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
  • Hi @Grzegorz, thanks for the reference. I have tried to change the export.outputFileType to AVFileTypeAIFC, and the export file export.m4a to export.aifc. However, it doesn't work. Here is the error message: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid output file type'. Do I miss anything? – Newbie Sep 12 '13 at 13:49
  • Can you paste an output of supportedFileTypes ? https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAssetExportSession_Class/Reference/Reference.html#//apple_ref/occ/instp/AVAssetExportSession/supportedFileTypes – Grzegorz Krukowski Sep 12 '13 at 13:52
  • Ah you also have AVAssetExportPresetAppleM4A when you initialize the class - I updated my answer. – Grzegorz Krukowski Sep 12 '13 at 13:54
  • Here is the output of supportedFileTypes: 2013-09-12 21:56:20.648 VoiceRecorder[4831:c07] Supported Output: ( "com.apple.m4a-audio" )...Seems that it only support AVFileTypeAppleM4V. – Newbie Sep 12 '13 at 13:58
  • oh, so what preset name should I use? – Newbie Sep 12 '13 at 13:59
  • I used exportPresetsCompatibleWithAsset: to find out the compatible presets for my composition. Here is the result: AVAssetExportPresetMediumQuality, AVAssetExportPresetAppleM4A, AVAssetExportPresetHighestQuality, AVAssetExportPresetLowQuality, AVAssetExportPreset640x480. So, which one should I use? – Newbie Sep 12 '13 at 14:02
  • oh, it runs with no error prompt. However, the filesize of the output file is 0 in AVFileTypeAIFC. When I change it to AVFileTypeAppleM4A, it has 200kB. hm..why is that?? – Newbie Sep 12 '13 at 14:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37266/discussion-between-grzegorz-krukowski-and-newbie) – Grzegorz Krukowski Sep 12 '13 at 14:24