合并视频&使用AVMutableCompositionTrack的AVMutableComposition中的图像,而不是AVVideoCompositionCoreAnimationTool?

时间:2016-01-22 02:11:10

标签: ios swift video avfoundation avmutablecomposition

以下代码使用AVMutableComposition导出视频。但是在导出的视频中,如果您希望在源视频完成后显示图像3秒,是否可以使用AVMutableCompositionTrack执行此操作,或者是否需要添加图像图层并在其后显示其外观视频结束了吗?

最终,目标是将任意数量的图像和视频合并为一个主视频。

不幸的是,在测试期间,似乎AVVideoCompositionCoreAnimationTool严重减慢了导出过程(从<1秒到10-20秒),因此目标是尽可能避免AVVideoCompositionCoreAnimationTool

    // Create composition object
    let composition = AVMutableComposition()
    let compositionVideoTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
    let compositionAudioTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
    var insertTime = kCMTimeZero

    // Extract tracks from slice video
    let videoURL = NSURL(fileURLWithPath: videoPath)
    let videoAsset = AVURLAsset(URL: videoURL, options: nil)
    let sourceVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0]
    let sourceAudioTrack = videoAsset.tracksWithMediaType(AVMediaTypeAudio)[0]
    do {
        try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: sourceVideoTrack, atTime: kCMTimeZero)
        try compositionAudioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: sourceAudioTrack, atTime: kCMTimeZero)
    } catch {
        print("Error with insertTimeRange while exporting video: \(error)")
    }

    // Export composition to video
    let outputURL = getFilePath(getUniqueFilename(gMP4File))
    let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
    exporter!.outputURL = NSURL(fileURLWithPath: outputURL)
    exporter!.outputFileType = AVFileTypeMPEG4
    exporter!.exportAsynchronouslyWithCompletionHandler({
        self.exportDidFinish(exporter!)
    })

1 个答案:

答案 0 :(得分:0)

在咨询其他人的SO并进行更多的网络研究后,似乎这是不可能的。将带有视频的图像合并到可从应用程序播放的主视频中似乎需要AVVideoCompositionCoreAnimationTool。