AVAssetExportSession does not export audio tracks

I have an application that allows you to add multiple video attributes and add one or more audio tracks to the composition. Everything seems to work, I can reproduce the resulting composition with AVPlayer(although the sound level seems low). After exporting the song to a file, there is no sound track.

My code is largely based on the sample AVEditDemo code from WWDC10 sessions. I double checked my code for AVEditDemo code and cannot find what might be the problem. I also checked the forums, but not many posts / solutions related to AVFoundation.

Any help is appreciated. Cheers

Jean pierre


A method of constructing a composition with additional audio tracks

Notes:

compositionArray: contains the assets to create the composition.
AssetView: an object containing AVURLAsset.

- (AVMutableComposition *)buildCompositionObjects
{
// no assets available, return nil
if ([compositionArray count] < 1)
{
    return nil;
}

// get the asset video size
AssetView * view =  [compositionArray objectAtIndex:0];
AVURLAsset * asset = view.asset;

CGSize videoSize = [asset naturalSize];

// create new composition
AVMutableComposition * cmp = [AVMutableComposition composition];

// set the size
cmp.naturalSize = videoSize;

// build composition
[self buildComposition:cmp];

// add any extra audio track
[self addAudioTrackToComposition:cmp];

// return the new composition
return cmp;
}

The way to build a basic composition

- (void) buildComposition:(AVMutableComposition *)cmp
{
// set the start time of contiguous tracks
CMTime nextClipStartTime = kCMTimeZero;

// clear the composition
[cmp removeTimeRange:CMTimeRangeMake(CMTimeMake(0, 600), cmp.duration)];

// add audio and video tracks
AVMutableCompositionTrack *compositionVideoTrack = [cmp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [cmp addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

// loop through all available assets
for (AssetView * view in compositionArray)
{
    AVURLAsset *asset = view.asset;

    CMTimeRange timeRangeInAsset;
    timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [asset duration]);

    AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    [compositionVideoTrack insertTimeRange:timeRangeInAsset ofTrack:clipVideoTrack atTime:nextClipStartTime error:nil];

    // make sure there is an audio track. Had to do this becaaause of this missing audio track issue. Crashes if check is not done  (out of bounds).
    if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] > 0)
    {
        AVAssetTrack *clipAudioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        [compositionAudioTrack insertTimeRange:timeRangeInAsset ofTrack:clipAudioTrack atTime:nextClipStartTime error:nil];
    }

    // adjust next asset start
    nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration);
}
}

Method for adding additional audio tracks

- (void)addAudioTrackToComposition:(AVMutableComposition *)cmp
{
// no audio track, return
if ([audioTracks count] < 1)
{
    return;
}

// base track ID for additional audio tracks 
long baseTrackID = 100;

for (AVURLAsset * audioAsset in audioTracks)
{
    // make sure the audio track fits in the composition
    CMTimeRange commentaryTimeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);

    if (CMTIME_COMPARE_INLINE(CMTimeRangeGetEnd(commentaryTimeRange), >, [cmp duration]))
    {
        commentaryTimeRange.duration = CMTimeSubtract([cmp duration], commentaryTimeRange.start);
    }

    // Add the audio track.
    AVMutableCompositionTrack *compositionCommentaryTrack = [cmp addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:baseTrackID++];

    [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, commentaryTimeRange.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:commentaryTimeRange.start error:nil];
}    

}

Composition export method

- (void) save
{

NSString * eventFolder = [NSString stringWithFormat:@"%@/%@-%@", 
               DOCUMENTS_FOLDER, 
               event.title,
               [StringUtils stringForDate:event.timeStamp]];


NSString * exportVideoPath = [NSString stringWithFormat:@"%@/Edits/%@.MOV", eventFolder, [StringUtils stringForDate:[NSDate date]]];

video.path = exportVideoPath;

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];

NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
    switch (exportSession.status) 
    {
        case AVAssetExportSessionStatusFailed:
        {
            NSLog (@"FAIL");
            [self performSelectorOnMainThread:@selector (doPostExportFailed:)
                                   withObject:nil
                                waitUntilDone:NO];
            break;
        }
        case AVAssetExportSessionStatusCompleted: 
        {
            NSLog (@"SUCCESS");
            [self performSelectorOnMainThread:@selector (doPostExportSuccess:)
                                   withObject:nil
                                waitUntilDone:NO];
            break;
        }
        case AVAssetExportSessionStatusCancelled: 
        {
            NSLog (@"CANCELED");
            [self performSelectorOnMainThread:@selector (doPostExportCancelled:)
                                   withObject:nil
                                waitUntilDone:NO];
            break;
        }
    };
}];

}
+5
source share
2 answers

I have not received a response. I have a job adding one line of code:

 exportSession.shouldOptimizeForNetworkUse = YES;

before

[exportSession exportAsynchronouslyWithCompletionHandler:^

I am not sure why this fixes the problem. since this does not seem to be related to the problem, but I was able to export a dozen songs with up to 5 additional audio tracks without any problems.

Hope this helps other people who scratch their heads for days on end.

Greetings

+6
source

I wrote this little library to support functions similar to yours.

GitHub: https://github.com/Tulakshana/Composer

, .

0

All Articles