如何在视频中添加水印,并每三秒钟移动一次水印?使用媒体库MediaWatermark

时间:2019-10-10 00:21:58

标签: ios swift mobile-application video-recording video-watermarking

我正在制作一个录制视频并向其中添加移动/动画水印的应用程序。如何在视频中添加它?我已经尝试通过在podFile中进行安装来尝试使用外部库,但其中有些行令人困惑,例如“ videoPlayer.self”

我尝试了一个名为“ MediaWatermark”的开放源代码库,但是其中有几行没有上下文,或者我不太了解它们。任何帮助将不胜感激。

@IBOutlet弱var RecordButton:UIButton!     var videoAndImageReview = UIImagePickerController()     var videoURL:URL?

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func RecordAction(_ sender: UIButton) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        print("Camera Available")

        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .camera
        imagePicker.mediaTypes = [kUTTypeMovie as String]
        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)
    } else {
        print("Camera UnAvaialable")
    }

}

func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [String : Any]) {
    dismiss(animated: true, completion: nil)

    guard
        let mediaType = info[UIImagePickerControllerMediaType] as? String,
        mediaType == (kUTTypeMovie as String),
        let url = info[UIImagePickerControllerMediaURL] as? URL,
        UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path)
        else {
            return
    }
   if let item = MediaItem(url: url) {
        let logoImage = UIImage(named: "logo")

        let firstElement = MediaElement(image: logoImage!)
        firstElement.frame = CGRect(x: 0, y: 0, width: logoImage!.size.width, height: logoImage!.size.height)

        let testStr = "Attributed Text"
        let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 35) ]
        let attrStr = NSAttributedString(string: testStr, attributes: attributes)

        let secondElement = MediaElement(text: attrStr)
        secondElement.frame = CGRect(x: 300, y: 300, width: logoImage!.size.width, height: logoImage!.size.height)

        item.add(elements: [firstElement, secondElement])

        let mediaProcessor = MediaProcessor()
        mediaProcessor.processElements(item: item) { [weak self] (result, error) in
            self?.videoPlayer.url = result.processedUrl
            self?.videoPlayer.playFromBeginning()

        }
    }

    // Handle a movie capture
    UISaveVideoAtPathToSavedPhotosAlbum(
        url.path,
        self,
        #selector(video(_:didFinishSavingWithError:contextInfo:)),
        nil)
}

@objc func video(_ videoPath: String, didFinishSavingWithError error: Error?, contextInfo info: AnyObject) {
    let title = (error == nil) ? "Success" : "Error"
    let message = (error == nil) ? "Video was saved" : "Video failed to save"

    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil))
    present(alert, animated: true, completion: nil)
}

@IBAction func openImgVideoPicker() {
    videoAndImageReview.sourceType = .savedPhotosAlbum
    videoAndImageReview.delegate = self
    videoAndImageReview.mediaTypes = ["public.movie"]
    present(videoAndImageReview, animated: true, completion: nil)
}

func videoAndImageReview(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    videoURL = info[UIImagePickerControllerMediaURL] as? URL
    print("videoURL:\(String(describing: videoURL))")
    self.dismiss(animated: true, completion: nil)
}

}

我希望保存的视频必须带有水印,并且一定时间后才能移动。

0 个答案:

没有答案