IOS How to export audio from movie

/* export audio from movie as sound  *.m4a*/
- (void) exportSoundFromMovie:(NSString*) inputVideo outputAudio:(NSString*) outputAudio {
      NSURL*    video_inputFileUrl = [NSURL fileURLWithPath:inputVideo];
      NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputAudio];
      AVURLAsset* videoAsset = [[[AVURLAsset alloc]initWithURL:video_inputFileUrl
                                                                                                      options:nil] autorelease];
    [videoAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"]  completionHandler:^ {
        //HERE IS THE PROBLEM. THE ARRAY OF TRACKS IS EMPTY FOR SOME REASON.
        AVAssetTrack* audioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio]   objectAtIndex:0];
       
        AVMutableComposition* audioComposition = [AVMutableComposition composition];
        AVMutableCompositionTrack* audioCompositionTrack = [audioComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        [audioCompositionTrack insertTimeRange:[audioTrack timeRange] ofTrack:audioTrack atTime:CMTimeMake(0, 1) error:nil];
       
AVAssetExportSession *exprortSession = [AVAssetExportSession exportSessionWithAsset:audioComposition presetName:AVAssetExportPresetAppleM4A];
        exprortSession.outputURL = outputFileUrl;
        exprortSession.outputFileType = @"com.apple.m4a-audio";
       
        NSLog(@"exportAsynchronouslyWithCompletionHandler will start");
       
        [exprortSession exportAsynchronouslyWithCompletionHandler: ^(void) {
           
            if (exprortSession.status == AVAssetExportSessionStatusCompleted) {
                NSLog(@"Export success");
            }
            else {
                NSLog(@"Export failed");
            }
        }];
    }];
   
}

Comments

Popular posts from this blog

Java : Variables Declaring

SQL Self JOIN