Question

I am trying to export the audio from a 3gpp video file and it is not working... Does anyone know what I may be doing wrong? Here is the code I am using:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"newFile.m4a"];
NSString *tempFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"oldFile.3gp"];

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^ {

    //HERE IS THE PROBLEM. THE ARRAY OF TRACKS IS EMPTY FOR SOME REASON.
    AVAssetTrack* audioTrack = [[asset 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];
    NSURL *toFileURL = [NSURL URLWithString:filePath];
    exprortSession.outputURL = toFileURL;
    exprortSession.outputFileType = @"com.apple.m4a-audio";

    NSLog(@"exportAsynchronouslyWithCompletionHandler will start");

    [exprortSession exportAsynchronouslyWithCompletionHandler: ^(void) {

        if (exprortSession.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"Export success");
        }
        else {
            NSLog(@"Export failed");
        }
    }];
}];
Was it helpful?

Solution

Try to load the asset with

[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:tempFilePath] options:nil];

instead of

[AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil];

OTHER TIPS

As your task will be manipulating the audio sample buffers directly you should use the second variant that AVFoundation will give you: paired AVAssetReader and AVAssetWriter setup. You'll find proper sample code as in AVReaderWriterOSX from Apple developer source. This should also work with iOS besides you have different I/O format settings available. The availability to decompress audio as PCM and write back to uncompressed .wav or Audio file should be given

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top