AVAssetExportSession不导出音轨

时间:2011-03-15 03:42:05

标签: iphone audio avfoundation

我有一个应用程序,允许附加多个视频资源并将一个或多个音轨添加到合成中。一切似乎都有效,我可以使用AVPlayer播放结果合成(虽然音频电平似乎很低)。将合成导出到文件后,音轨丢失。

我的代码主要基于WWDC10会话的AVEditDemo示例代码。我已经针对AVEditDemo代码仔细检查了我的代码,但无法找到可能存在的问题。我也检查了论坛,但没有太多与AVFoundation相关的帖子/解决方案。

欢迎任何帮助。欢呼声,

让 - 皮埃尔


使用额外音轨构建合成的方法

注意:

compositionArray:包含构建合成的资源 AssetView:包含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;
}

构建基础组合的方法

- (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);
}
}

添加其他音轨的方法

- (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];
}    

}

导出合成的方法

- (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;
        }
    };
}];

}

1 个答案:

答案 0 :(得分:6)

没有得到任何回复。我通过添加一行代码来完成工作:

 exportSession.shouldOptimizeForNetworkUse = YES;

[exportSession exportAsynchronouslyWithCompletionHandler:^

我不确定为什么这可以解决这个问题。因为这似乎完全与问题无关,但我能够导出十几个乐曲,最多可以输出5个额外的音轨。

我希望这可以帮助那些已经摸不着头脑的其他人。

干杯