将视频与AVMutableComposition合并导致无音频

时间:2014-06-17 15:15:40

标签: ios objective-c video

我有一个包含视频NSURL列表的NSArray,我想将它们合并在一起进行一次长编译。问题是,当我使用下面的代码时,视频合并但没有音频。

- (IBAction)buildVideo {

// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                               preferredTrackID:kCMPersistentTrackID_Invalid];

int i = 0;

for (id object in movieArray) {

    AVAsset *asset = [AVAsset assetWithURL:object];

    if(i == 0){

        [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                       ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];


    }else {

        [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                       ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:[mixComposition duration] error:nil];

    }

    i = i + 1;

}

// 4 - Get path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                         [NSString stringWithFormat:@"mergeVideo-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];
// 5 - Create exporter
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                  presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self exportDidFinish:exporter];
    });
}];

}

1 个答案:

答案 0 :(得分:8)

您没有收到音频的原因是您没有添加音轨。您需要创建一个类型为AVMutableCompositionTrack的其他AVMediaTypeAudio

AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                           preferredTrackID:kCMPersistentTrackID_Invalid];

AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                           preferredTrackID:kCMPersistentTrackID_Invalid];

将源音频和视频轨道的时间范围插入两个合成轨道:

CMTime insertTime = kCMTimeZero;

for (id object in movieArray) {

    AVAsset *asset = [AVAsset assetWithURL:object];

    CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

    [videoTrack insertTimeRange:timeRange
                        ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                         atTime:insertTime
                          error:nil];

    [audioTrack insertTimeRange:timeRange
                         ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                          atTime:insertTime
                           error:nil];

    insertTime = CMTimeAdd(insertTime,asset.duration);
}