Export a big file than the original one made using AVAssetExportSession

270 views Asked by At

I am working on an iPhone app which recorded multiple files and then i merged the files into one file and at the end i upload that file on server. I experienced that if i make a single file of 7 mint recording its size is 0.85 MB but if i make two files of 3.5 minutes and then merged them into a single file its size is 9.75 MB which is very big in size and take too long time to upload. Kindly suggest me someway to handle the issue. Thanks in advance `

AVMutableComposition* composition = [[AVMutableComposition alloc] init];
    AVMutableCompositionTrack* audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                     preferredTrackID:kCMPersistentTrackID_Invalid];
    //  kCMPersistentTrackID_Invalid, kCMMediaType_Audio,
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath_ = [searchPaths objectAtIndex: 0];
    // RecorderFilePaths contain the path of recorded files
    if ([recordedFilePaths count]>0)
    {
        NSError* error = nil;
        AVURLAsset* masterAsset= nil;
        CMTime time = kCMTimeZero;

        NSURL *tempUrl =[recordedFilePaths objectAtIndex:1];
        time = CMTimeAdd(time, [masterAsset duration]);
        AVURLAsset *Asset = [AVURLAsset URLAssetWithURL:tempUrl options:nil];
        CMTimeRange timeRange  = CMTimeRangeMake(kCMTimeZero, Asset.duration);
        NSArray *ar = [Asset tracksWithMediaType:AVMediaTypeAudio];
        [audioTrack insertTimeRange:timeRange ofTrack:[ar objectAtIndex:0] atTime:kCMTimeZero error:&error];
        //kAudioFormatMPEG4AAC,   AVMediaTypeAudio
        tempUrl = [recordedFilePaths objectAtIndex:0];
        masterAsset = [AVURLAsset URLAssetWithURL:tempUrl options:nil];
        timeRange = CMTimeRangeMake(kCMTimeZero, masterAsset.duration);
        NSArray *tracktypes = [masterAsset tracksWithMediaType:AVMediaTypeAudio];
        [audioTrack insertTimeRange:timeRange ofTrack:[tracktypes objectAtIndex:0] atTime:kCMTimeZero error:&error];

        if (error)
        {
            NSLog(@"%@",error);
            return;
        }

        AVAssetExportSession* exportSession = [AVAssetExportSession
                                               exportSessionWithAsset:composition
                                               presetName:AVAssetExportPresetAppleM4A];
        //  AVAssetExportPresetPassthrough,,AVAssetExportPresetLowQuality
        if (nil == exportSession)
        {            
            return;
        }

        NSString *urlSt = [documentPath_ stringByAppendingPathComponent:[self dateString]];
        NSURL* combined = [[NSURL alloc] initFileURLWithPath:urlSt];

        exportSession.outputURL = combined;
        exportSession.outputFileType = AVFileTypeAppleM4A;
        //  AVFileTypeAIFF , AVFileTypeAIFC, AVFileTypeAIFC,
        [exportSession exportAsynchronouslyWithCompletionHandler:^(void){

            dispatch_async(dispatch_get_main_queue(), ^{
                switch (exportSession.status)
                {
                    case AVAssetExportSessionStatusFailed:
                        NSLog(@"Failed");
                        break;
                    case AVAssetExportSessionStatusCompleted:
                        NSLog(@"Completed");
                        break;
                    case AVAssetExportSessionStatusWaiting:
                        break;
                    default:
                        break;
                }

            });
        }];

        [recordedFilePaths addObject:combined];
}

`

0

There are 0 answers