VideoOverlayProcessor 1.0.0

VideoOverlayProcessor 1.0.0

[dader] 维护。



  • 作者
  • Dawid Płatek

介绍

VideoOverlayProcessor 是由 Dawid Płatekinspace.io 编写的。

VideoOverlayProcessor

VideoOverlayProcessor 是一个简洁易用的库,它负责在视频上添加图像和文字叠加。

功能

  • 稳定性能:
    VideoOverlayProcessor 在内部使用 AVFoundation 框架。整个逻辑基于 AVMutableComposition,并且视频仅处理一次以保持其最高效率。

  • 支持时间范围
    叠加的可见性可以通过定义精确的时间范围来指定。

  • 定位和尺寸
    可以使用定义的 frame 简单地配置叠加的位置和尺寸。

入门指南

创建一个处理器

负责视频处理的基类称为 VideoOverlayProcessor。要开始使用库,您只需创建该类的新对象,并传递 inputURLoutputURL 参数。该过程可以提供有关输入文件的一些额外信息,例如正确计算叠加的帧或设置时间范围(分别使用 videoSizevideoDuration 属性)。

let inputURL = ...
let outputURL = ...

let processor = VideoOverlayProcessor(inputURL: inputURL, outputURL: inputURL)

添加叠加层

有两个类负责将文本和图像叠加到视频上:TextOverlayImageOverlay。您应该使用它们来构建叠加结构层次。要定义定位和大小,您必须设置 frame 属性。除此之外,您还可以指定叠加可见的时间范围。为了实现这一点,您必须使用 delayduration 属性。当配置好叠加对象后,在开始处理之前,您需要做的最后一件事是将对象添加到处理器(在这种情况下,您应该使用处理器的 addOverlay 方法)。

let videoSize = processor.videoSize
let videoDuration = processor.videoDuration

let textOverlay = TextOverlay(text: "Welcome on github.com!", frame: CGRect(x: 0, y: 0, width: videoSize.width, height: videoSize.height/4), delay: 0.0, duration: videoDuration)
processor.addOverlay(textOverlay)

let image = UIImage(named: ...)
let imageOverlay = ImageOverlay(image: image, frame: CGRect(x: videoSize.width/2-image.size.width/2, y: videoSize.height/2-image.size.height/2, width: image.size.width, height: image.size.height), delay: 0.0, duration: videoDuration)
processor.addOverlay(imageOverlay)

开始处理

您可以通过调用 process 方法开始处理。

用例

VideoOverlayProcessor 可以简化将重叠添加到视频的整个过程。有一些常见的用例,可能会给您提供一个关于如何潜在地使用此库的思路。

给视频添加水印

guard let inputURL = Bundle.main.url(forResource: "sample", withExtension: "mp4") else { return }

let outputURL = FileManager.default.temporaryDirectory.appendingPathComponent("output-\(Int(Date().timeIntervalSince1970)).mp4")

let processor = VideoOverlayProcessor(inputURL: inputURL, outputURL: outputURL)

let videoSize = processor.videoSize
let videoDuration = processor.videoDuration

guard let image = UIImage(named: "overlay") else { return }
let margin: CGFloat = 100
let imageOverlay = ImageOverlay(image: image, frame: CGRect(x: videoSize.width-image.size.width-margin, y: videoSize.height-image.size.height/2-margin, width: image.size.width/2, height: image.size.height/2), delay: 0.0, duration: videoDuration)
processor.addOverlay(imageOverlay)

processor.process { [weak self] (exportSession) in
    guard let exportSession = exportSession else { return }
    
    if (exportSession.status == .completed) {
        DispatchQueue.main.async { [weak self] in
            self?.showPlayerViewController(for: outputURL)
        }
    }
}

视频添加字幕

guard let inputURL = Bundle.main.url(forResource: "sample", withExtension: "mp4") else { return }

let outputURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("output-\(Int(Date().timeIntervalSince1970)).mp4")

let processor = VideoOverlayProcessor(inputURL: inputURL, outputURL: outputURL)

let videoSize = processor.videoSize
let videoDuration = processor.videoDuration

let textOverlay = TextOverlay(text: "Hello ;) I hope you like this library", frame: CGRect(x: 0, y: 0, width: videoSize.width, height: videoSize.height/12), delay: 0.0, duration: videoDuration, backgroundColor: UIColor.black.withAlphaComponent(0.3), textColor: UIColor.white)
processor.addOverlay(textOverlay)

processor.process { [weak self] (exportSession) in
    guard let exportSession = exportSession else { return }
    
    if (exportSession.status == .completed) {
        DispatchQueue.main.async { [weak self] in
            self?.showPlayerViewController(for: outputURL)
        }
    }
}

安装

VideoOverlayProcessor 将与最新的公开版本 Swift 兼容。

CocoaPods

VideoOverlayProcessor 通过 CocoaPods 提供。要安装它,请将以下内容添加到您的 Podfile

pod 'VideoOverlayProcessor'

需求

  • iOS 8.0+
  • Xcode 8.0+

许可证

VideoOverlayProcessor 采用 MIT 许可证发布。有关详细信息,请参阅 LICENSE。