为录制的视频添加水印并保存

Add watermark to recorded video and save

所以我尝试使用以下代码为以前录制的视频添加水印,但是当我查看视频时,没有水印。谁能帮忙?我尝试在 post 处关注:iPhone Watermark on recorded Video.

    public func addWatermarkToVideo(url: NSURL, completion:(url: NSURL?) -> Void) {
    let videoAsset = AVURLAsset(URL: url)
    let mixComposition = AVMutableComposition()
    let compositionVideoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
    let clipVideoTrack: AVAssetTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0]
    do {
        try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: clipVideoTrack, atTime: kCMTimeZero)
    } catch {
        print(error)
    }

    compositionVideoTrack.preferredTransform = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0].preferredTransform

    //Add watermark
    guard let myImage = UIImage(named: "Logo") else {
        completion(url: nil)
        return
    }
    let aLayer = CALayer()
    aLayer.contents = myImage.CGImage
    aLayer.frame = CGRectMake(5, 25, 100, 57)
    aLayer.opacity = 0.65

    let videoSize = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0].naturalSize
    let parentLayer = CALayer()
    let videoLayer = CALayer()
    parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height)
    videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height)
    parentLayer.addSublayer(videoLayer)
    parentLayer.addSublayer(aLayer)

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

    let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration)
    let videoTrack = mixComposition.tracksWithMediaType(AVMediaTypeVideo)[0]
    let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
    instruction.layerInstructions = [layerInstruction]
    videoComp.instructions = [instruction]

    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDirectory: AnyObject = paths[0]
    let dataPath = documentsDirectory.stringByAppendingPathComponent("VideoCache")

    if (!NSFileManager.defaultManager().fileExistsAtPath(dataPath)) {
        do {
            try NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil)
        } catch {
            print("Couldn't create path")
        }
    }
    let tempURL = NSURL(fileURLWithPath: dataPath)
    let completeMovieUrl = tempURL.URLByAppendingPathComponent("tether-\(NSDate()).mov")

    if let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) {
        exporter.outputURL = completeMovieUrl
        exporter.outputFileType = AVFileTypeMPEG4
        exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in
            switch exporter.status {
            case  .Failed:
                print("failed \(exporter.error)")
                completion(url: nil)
                break
            case .Cancelled:
                print("cancelled \(exporter.error)")
                completion(url: nil)
                break
            default:
                print("complete")
                completion(url: exporter.outputURL)
            }
        })
    }
}

您忘记将您的 videoComposition 添加到 AVAssetExportSession:

exporter.outputFileType = AVFileTypeMPEG4 // You had this
exporter.videoComposition = videoComp     // but had forgotten this
exporter.exportAsynchronouslyWithCompletionHandler({ // ...