合并视频& AVMutableComposition中的图像使用AVMutableCompositionTrack,而不是AVVideoCompositionCoreAnimationTool? [英] Merge videos & images in AVMutableComposition using AVMutableCompositionTrack, not AVVideoCompositionCoreAnimationTool?

查看:147
本文介绍了合并视频& AVMutableComposition中的图像使用AVMutableCompositionTrack,而不是AVVideoCompositionCoreAnimationTool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The code below exports a video using AVMutableComposition. But in the exported video, if you want an image to display for 3 seconds after the source video finishes, is there a way to do that with AVMutableCompositionTrack or do you need to add an image layer and animate its appearance after the video ends?

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

Eventually, the goal is to merge an arbitrary number of images and videos into one master video.

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

Unfortunately, during testing it seems like AVVideoCompositionCoreAnimationTool severely slows down the export process (from < 1 second to 10-20 seconds), so the goal is to avoid AVVideoCompositionCoreAnimationTool if possible.

    // 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!)
    })


推荐答案

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

After consulting others on SO and performing more web research, it seems like this is not possible. Merging an image with a video into a master video that is playable out of an app seems to require AVVideoCompositionCoreAnimationTool.

这篇关于合并视频&amp; AVMutableComposition中的图像使用AVMutableCompositionTrack,而不是AVVideoCompositionCoreAnimationTool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆