在Swift 3中为视频添加叠加层

时间:2017-08-06 19:39:00

标签: ios swift video avfoundation

我正在学习AVFoundation,我在尝试使用Swift 3中的叠加图像保存视频时遇到问题。使用AVMutableComposition我可以将图像添加到视频但是视频已缩放并且不限制视频拍摄的肖像尺寸。我尝试过:

  • 通过AVAssetTrack设置自然尺寸。
  • AVMutableVideoComposition renderFrame
  • 中将视频限制为纵向尺寸
  • 将新视频边界锁定为录制的视频宽度和高度。

以下代码与我需要帮助的问题不同。我正在尝试添加的图像覆盖了整个纵向视图,并且边缘周围都有边框。该应用程序也只允许肖像。

func processVideoWithWatermark(video: AVURLAsset, watermark: UIImage, completion: @escaping (Bool) -> Void) {

    let composition = AVMutableComposition()
    let asset = AVURLAsset(url: video.url, options: nil)

    let track =  asset.tracks(withMediaType: AVMediaTypeVideo)
    let videoTrack:AVAssetTrack = track[0] as AVAssetTrack
    let timerange = CMTimeRangeMake(kCMTimeZero, asset.duration)

    let compositionVideoTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())

    do {
        try compositionVideoTrack.insertTimeRange(timerange, of: videoTrack, at: kCMTimeZero)
        compositionVideoTrack.preferredTransform = videoTrack.preferredTransform
    } catch {
        print(error)
    }

//      let compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
//      
//      for audioTrack in asset.tracks(withMediaType: AVMediaTypeAudio) {
//          do {
//              try compositionAudioTrack.insertTimeRange(audioTrack.timeRange, of: audioTrack, at: kCMTimeZero)
//          } catch {
//              print(error)
//          }
//          
//      }
//      
    let size = videoTrack.naturalSize

    let watermark = watermark.cgImage
    let watermarklayer = CALayer()
    watermarklayer.contents = watermark
    watermarklayer.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
    watermarklayer.opacity = 1

    let videolayer = CALayer()
    videolayer.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)

    let parentlayer = CALayer()
    parentlayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    parentlayer.addSublayer(videolayer)
    parentlayer.addSublayer(watermarklayer)

    let layercomposition = AVMutableVideoComposition()
    layercomposition.frameDuration = CMTimeMake(1, 30)
    layercomposition.renderSize = CGSize(width: screenWidth, height: screenHeight)
    layercomposition.renderScale = 1.0
    layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, in: parentlayer)

    let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)

    let videotrack = composition.tracks(withMediaType: AVMediaTypeVideo)[0] as AVAssetTrack
    let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videotrack)

    layerinstruction.setTransform(videoTrack.preferredTransform, at: kCMTimeZero)

    instruction.layerInstructions = [layerinstruction]
    layercomposition.instructions = [instruction]

    let filePath = NSTemporaryDirectory() + self.fileName()
    let movieUrl = URL(fileURLWithPath: filePath)

    guard let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality) else {return}
    assetExport.videoComposition = layercomposition
    assetExport.outputFileType = AVFileTypeMPEG4
    assetExport.outputURL = movieUrl

    assetExport.exportAsynchronously(completionHandler: {

        switch assetExport.status {
        case .completed:
            print("success")
            print(video.url)
            self.saveVideoToUserLibrary(fileURL: movieUrl, completion: { (success, error) in
                if success {
                    completion(true)
                } else {
                    completion(false)

                }
            })

            break
        case .cancelled:
            print("cancelled")
            break
        case .exporting:
            print("exporting")
            break
        case .failed:
            print(video.url)
            print("failed: \(assetExport.error!)")
            break
        case .unknown:
            print("unknown")
            break
        case .waiting:
            print("waiting")
            break
        }
    })

}

1 个答案:

答案 0 :(得分:0)

如果视频图层应填充父图层,则您的videoLayer的frame不正确。您需要将大小设置为size而不是screenSize