简体   繁体   English

导出多个轨道:AVMutableComposition和AVAssetExportSession

[英]Exporting Multiple Tracks: AVMutableComposition and AVAssetExportSession

I have 4 videos files which I have edited and added to a mutable composition. 我有4个视频文件,我已对其进行编辑并将其添加到可变的作品中。 I am trying to use the export session to export the files, however, when I am exporting, only the first track in the list of tracks below gets exported 我正在尝试使用导出会话来导出文件,但是,当我导出时,仅导出了以下轨道列表中的第一条轨道

<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)

Only the first track with trackID = 1 gets exported. 仅具有trackID = 1的第一条轨道被导出。 Here is the export session source: 这是导出会话源:

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

Does anyone have any ideas how I can get all 4 tracks to export to a single .mov file using export session? 有谁知道如何使用导出会话将所有4条轨道导出到单个.mov文件?

You can only export to one NSURL with AVAssetExportSession . 您只能使用AVAssetExportSession导出到一个NSURL If you want to export 4 separate files, you will have to export 4 times. 如果要导出4个单独的文件,则必须导出4次。

Thanks to jlw for his help. 感谢jlw的帮助。 What I found the problem was that I was adding multiple video tracks to mutable composition. 我发现问题是我在可变的构图中添加了多个视频轨道。 What instead I should have done is made a single video track and applied all edits from the other asset tracks onto the single video tracks. 相反,我应该做的是制作单个视频轨道,并将来自其他资产轨道的所有编辑应用于单个视频轨道。 Seems AVAssetExportSession will only export a single track as noted by jlw. 似乎AVAssetExportSession将仅导出jlw指出的单个轨道。

Summary: 摘要:

  1. Create mutable composition 创建可变的成分
  2. Create mutable composition track 创建可变的合成音轨
  3. Apply asset tracks to the composition (insertTimeRange) 将资产轨道应用于合成(insertTimeRange)
  4. Export the mutable composition 导出可变成分

I think there is no need to create new AVMutableComposition . 我认为没有必要创建新的AVMutableComposition Instead just assign your AVVideoComposition? 而是只分配您的AVVideoComposition? to the videoComposition property of your exporter. 导出器的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 {
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM