导出多个轨道:AVMutableComposition和AVAssetExportSession

时间:2014-10-02 02:12:30

标签: ios avfoundation avassetexportsession avmutablecomposition avasset

我有4个视频文件,我已编辑并添加到可变组合中。我正在尝试使用导出会话导出文件,但是,当我导出时,只会导出下面曲目列表中的第一首曲目

<AVAssetExportSession: 0x60800001da50, asset = <AVMutableComposition: 0x6080002240a0 tracks = (
    "<AVMutableCompositionTrack: 0x600000224ca0 trackID = 1, mediaType = vide, editCount = 8>",
    "<AVMutableCompositionTrack: 0x600000226da0 trackID = 2, mediaType = vide, editCount = 10>",
    "<AVMutableCompositionTrack: 0x60000023e180 trackID = 3, mediaType = vide, editCount = 3>",
    "<AVMutableCompositionTrack: 0x60000023e500 trackID = 4, mediaType = vide, editCount = 7>"
)>, presetName = AVAssetExportPreset1280x720, outputFileType = (null)

仅导出trackID = 1的第一首曲目。这是导出会话源:

   // Create path to output file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"ProcessedVideo-%d.mov", arc4random() % 1000]];
    NSURL *url = [NSURL fileURLWithPath:myPathDocs];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:batchComposition presetName:AVAssetExportPreset1280x720];

    NSLog(@"%@", exporter);

    exporter.outputURL = url;
    exporter.outputFileType = AVFileTypeQuickTimeMovie;

    [exporter exportAsynchronouslyWithCompletionHandler:^(void) {
        switch (exporter.status) {
            case AVAssetExportSessionStatusCompleted:
                NSLog(@"Completed");
                break;
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Failed:%@",exporter.error);
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Canceled:%@",exporter.error);
                break;
            default:
                break;
        }
    }];

有没有人有任何想法如何使用导出会话将所有4首曲目导出到单个.mov文件?

3 个答案:

答案 0 :(得分:2)

您只能使用NSURL导出到AVAssetExportSession个{{1}}。如果要导出4个单独的文件,则必须导出4次。

答案 1 :(得分:1)

感谢jlw的帮助。我发现问题是我正在为可变组合添加多个视频轨道。我应该做的是制作单个视频轨道,并将来自其他资产轨道的所有编辑应用到单个视频轨道上。似乎AVAssetExportSession将只导出单个轨道,如jlw所述。

要点:

  1. 创建可变组合
  2. 创建可变构图轨迹
  3. 将资产跟踪应用于合成(insertTimeRange)
  4. 导出可变成分

答案 2 :(得分:1)

我认为没有必要创建新的AVMutableComposition。而只需将AVVideoComposition?分配给导出方的videoComposition属性。

guard let playerItem = getPlayerItem() as? AVPlayerItem else {
    return
}

let asset = playerItem.asset
let videoComposition: AVVideoComposition? = playerItem.videoComposition

let path = NSTemporaryDirectory().stringByAppendingFormat("/video.mov")
if NSFileManager.defaultManager().fileExistsAtPath(path) {
    do {
        try NSFileManager.defaultManager().removeItemAtPath(path)
    } catch {
        print("Temporary file removing error.")
    }
}
let outputURL = NSURL.fileURLWithPath(path)

guard let exporter = AVAssetExportSession(asset: asset,
                                          presetName: AVAssetExportPresetHighestQuality) else {
                                            return
}

exporter.outputURL = outputURL
exporter.outputFileType = AVFileTypeQuickTimeMovie
exporter.shouldOptimizeForNetworkUse = true
exporter.videoComposition = videoComposition // 

exporter.exportAsynchronouslyWithCompletionHandler {
    PHPhotoLibrary.sharedPhotoLibrary().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(outputURL)
    }) { (success: Bool, error: NSError?) -> Void in
        if success {
        } else {
        }
    }
}