在视频中添加水印(iOS,Swift)

时间:2018-10-16 17:00:03

标签: ios video watermark

我正在开发带有自定义录像机的应用程序,应该在上面加水印。在录制部分,一切都进行得很好,但是当我尝试放置水印时,视频以不同的方向导出,有时效果很差。结果仅在横向安装设备上正确。

这是我用来在视频上加水印的功能的代码:

func addWatermarkToVideo(_ videoURL: URL, completion:@escaping (URL) -> Void) {
    let videoAsset = AVURLAsset.init(url: videoURL)
    let mixComposition = AVMutableComposition.init()
    let compositionVideoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)

    do {
        let clipVideoTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo).first
        try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: clipVideoTrack!, at: kCMTimeZero)
        compositionVideoTrack.preferredTransform = (videoAsset.tracks(withMediaType: AVMediaTypeVideo).first?.preferredTransform)!

        let layer = CALayer.init()
        layer.contents = UIImage.init(named: "VideoWatermark")?.cgImage
        layer.frame = CGRect.init(x: 10, y: 10, width: 300, height: 100)

        let videoTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo).first
        let videoSize = videoTrack!.naturalSize
        let parentLayer = CALayer.init()
        let videoLayer = CALayer.init()
        parentLayer.frame = CGRect.init(x: 0, y: 0, width: videoSize.width, height: videoSize.height)
        videoLayer.frame = CGRect.init(x: 0, y: 0, width: videoSize.width, height: videoSize.height)
        parentLayer.addSublayer(videoLayer)
        parentLayer.addSublayer(layer)

        let videoComp = AVMutableVideoComposition.init()
        videoComp.renderSize = videoSize
        videoComp.frameDuration = CMTimeMake(1, 30)
        videoComp.animationTool = AVVideoCompositionCoreAnimationTool.init(postProcessingAsVideoLayer: videoLayer, in: parentLayer)

        let instruction = AVMutableVideoCompositionInstruction.init()
        instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration)
        let mixVideoTrack = mixComposition.tracks(withMediaType: AVMediaTypeVideo).first
        let layerInstruction = AVMutableVideoCompositionLayerInstruction.init(assetTrack: mixVideoTrack!)
        layerInstruction.setTransform((clipVideoTrack?.preferredTransform)!, at: kCMTimeZero)

        instruction.layerInstructions = [layerInstruction]
        videoComp.instructions = [instruction]

        let assetExport = AVAssetExportSession.init(asset: mixComposition, presetName: AVAssetExportPreset1280x720)
        assetExport?.videoComposition = videoComp

        let videoName = "\(Date.init().timeIntervalSince1970).mp4"

        let exportURL = URL.init(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(videoName)

        assetExport?.outputFileType = AVFileTypeMPEG4
        assetExport?.outputURL = exportURL
        assetExport?.shouldOptimizeForNetworkUse = true

        assetExport?.exportAsynchronously(completionHandler: {
            NSLog("Export status: \(String(describing: assetExport?.status.rawValue))")
            completion(exportURL)
        })

    } catch let error as NSError {
        print("\(error), \(error.localizedDescription)")
    }
}

我尝试使用谷歌搜索问题,但最终没有结果对我有帮助,我不知道我在这里做错了什么。

任何人的帮助都会深表谢意,在此先感谢可以帮助我的人。

0 个答案:

没有答案