you can use this SDAVAssetExportSession with the following code to export many files to AAC:
-(void)mergeAudioFiles { NSFileManager * fm = [[NSFileManager alloc] init]; NSError * error; NSArray * filesNames = **NSArray of File Names; NSString * filePath = @"Dest File Name"; NSString * pathToSave =[NSString stringWithFormat:@"%@%@",filePath,@".m4a"]; CMTime startTime = kCMTimeZero; AVMutableComposition *composition = [AVMutableComposition composition]; AVMutableCompositionTrack *compositionAudioTrack =[AVMutableCompositionTrack alloc]; compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; float audioEndTime=0; for (NSString *fileName in filesNames) { NSURL *audioUrl = [NSURL fileURLWithPath:fileName]; AVURLAsset *audioasset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil]; CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, audioasset.duration); AVAssetTrack *audioAssetTrack= [[audioasset tracksWithMediaType:AVMediaTypeAudio] lastObject]; [compositionAudioTrack insertTimeRange:timeRange ofTrack:audioAssetTrack atTime:startTime error:&error]; startTime = CMTimeAdd(startTime, timeRange.duration); CMTime assetTime2 = [audioasset duration]; Float64 duration2 = CMTimeGetSeconds(assetTime2); audioEndTime+=duration2; } NSURL *exportUrl = [NSURL fileURLWithPath:pathToSave]; float audioStartTime=0; CMTime startTime1 = CMTimeMake((int)(floor(audioStartTime * 100)), 100); CMTime stopTime = CMTimeMake((int)(ceil(audioEndTime * 100)), 100); CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime1, stopTime); SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:composition]; encoder.outputFileType = AVFileTypeAppleM4A; encoder.outputURL = exportUrl; encoder.audioSettings = @ { AVFormatIDKey: @(kAudioFormatMPEG4AAC), AVNumberOfChannelsKey: @2, AVSampleRateKey: @44100, AVEncoderBitRateKey: @128000, }; encoder.timeRange = exportTimeRange; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); NSLog(@"Starting Audio Marge"); [encoder exportAsynchronouslyWithCompletionHandler:^ { if (encoder.status == AVAssetExportSessionStatusCompleted) { NSLog(@"Audio Marge succeeded"); NSError * err = NULL; BOOL result = [fm moveItemAtPath:pathToSave toPath:filePath error:&err]; if(!result) { NSLog(@"Error: %@", err); } NSLog(@"Audio Copied"); } else if (encoder.status == AVAssetExportSessionStatusCancelled) { NSLog(@"Audio export cancelled"); } else { NSLog(@"Audio export failed with error: %@ (%ld)", encoder.error.localizedDescription, encoder.error.code); } dispatch_semaphore_signal(semaphore); }]; NSLog(@"Audio Wait to Finish"); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
Liron sher
source share