0

我正在尝试在视频上添加文本,但无论我做什么,我都会收到无效的合成错误 (-11841)。我遵循了教程,通读了线程,没有什么是错误的。我正在组合 3 个视频和一个音轨,效果很好。我正在尝试在第一个视频剪辑 (videoStartAsset) 的持续时间内添加文本。

这是我正在使用的代码:

   var mainInstruction = AVMutableVideoCompositionInstruction()
    mainInstruction.timeRange = CMTimeRange(start: kCMTimeZero, duration: videoStartTimeRange.duration)

    var videoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoCompositionTrack)
    var videoAssetTrack = videoStartAsset.tracksWithMediaType(AVMediaTypeVideo)[0] as AVAssetTrack

    videoLayerInstruction.setOpacity(0.0, atTime: videoStartTimeRange.duration)

    mainInstruction.layerInstructions = [videoLayerInstruction]

    var mainCompositionInst = AVMutableVideoComposition()
    var naturalSize = videoAssetTrack.naturalSize
    var renderWidth = naturalSize.width
    var renderHeight = naturalSize.height
    mainCompositionInst.renderScale = Float(UIScreen.mainScreen().scale)
    mainCompositionInst.renderSize = CGSize(width: 640.0, height: 480.0)
    mainCompositionInst.instructions = [mainInstruction]
    mainCompositionInst.frameDuration = CMTimeMake(1, 30)

    var nameTextLayer = CATextLayer()
    nameTextLayer.font = "FONT"
    nameTextLayer.fontSize = 100
    nameTextLayer.frame = CGRect(x: 0.0, y: 0.0, width: naturalSize.width, height: naturalSize.height)
    if let nameText = nameInputText?.label?.text {
        nameTextLayer.string = "TEXT"
    } else {
        nameTextLayer.string = "DEFAULT TEXT"
    }
    nameTextLayer.alignmentMode = kCAAlignmentCenter
    nameTextLayer.foregroundColor = UIColor.whiteColor().CGColor

    var overlayLayer = CALayer()
    overlayLayer.addSublayer(nameTextLayer)
    overlayLayer.frame = CGRect(x: 0.0, y: 0.0, width: naturalSize.width, height: naturalSize.height)
    overlayLayer.masksToBounds = true

    var parentLayer = CALayer()
    var videoLayer = CALayer()

    parentLayer.frame = CGRect(x: 0.0, y: 0.0, width: naturalSize.width, height: naturalSize.height)
    videoLayer.frame = CGRect(x: 0.0, y: 0.0, width: naturalSize.width, height: naturalSize.height)

    parentLayer.addSublayer(videoLayer)
    parentLayer.addSublayer(overlayLayer)

    mainCompositionInst.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)

谢谢。

4

2 回答 2

0
AVErrorInvalidVideoComposition = -11841,

You attempted to perform a video composition operation that is not supported.

Available in iOS 5.0 and later.

您可以在此处找到有关 AV Foundation 错误的更多信息。

于 2015-03-20T07:51:30.887 回答
0

修复了我自己的问题;事实证明,由于以下行而引发了错误:

mainInstruction.timeRange = CMTimeRange(start: kCMTimeZero, duration: videoStartTimeRange.duration)

这是因为我将 timeRange 设置为第一个视频剪辑的时间范围(应该添加文本),而不是整个视频的 timeRange(我将 3 个视频合并为一个,所以我需要设置timeRange 是它们所有持续时间的总和)。

感谢 Apple 提供非常详细的错误消息。/讽刺

于 2015-03-24T16:24:45.243 回答