VideoOverlayProcessor was written by Dawid Płatek for inspace.io
VideoOverlayProcessor is a clean and easy-to-use library responsible for adding image and text overlays to video.
-
Solid performance:
VideoOverlayProcessoris usingAVFoundationframework under the hood. The whole logic is based onAVMutableCompositionand the input video is processed only once to keep it as efficient as possible. -
Time range support
Visibility of the overlays can be specified by defining exact time range. -
Positioning and sizing
The position and the size of overlay can be be easily configured by definedframe.
The base class responsible for video processing is called VideoOverlayProcessor. To start using the library you have to just create a new object of this class and pass inputURL and outputURL parameters. The process can give you some additional information about the input file i.e. to calculate the frame of overlay correctly or to set the time range (videoSize and videoDuration properties respectively).
let inputURL = ...
let outputURL = ...
let processor = VideoOverlayProcessor(inputURL: inputURL, outputURL: inputURL)There are two classes responsible for adding text and image overlays to video: TextOverlay and ImageOverlay You should use them to constructed hierarchy of your overlay structure. To defined positioning and sizing you have to set frame property. Beside that, you can specify the time range which indicates when the overlay is visible. To achive it you have to use delay and duration properties. When the overlay object is configured accordingly the last thing you have to do before start processing is adding the object to processor (you should use addOverlay method of processing in this case).
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)You can start processing by calling process method.
VideoOverlayProcessor can simplify the whole process of adding overlays to video. There are some common use cases which may give an idea how potentially you use this library.
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 will be compatible with the lastest public release of Swift.
VideoOverlayProcessor is available through CocoaPods. To install it, add the following to your Podfile:
pod 'VideoOverlayProcessor'
- iOS 8.0+
- Xcode 8.0+
VideoOverlayProcessor is released under the MIT license. See LICENSE for details.
