Skip to content

Make video frames in 420YpCbCr8BiPlanarFullRange from view representation of UIKit or SwiftUI.

License

Notifications You must be signed in to change notification settings

naru-jpn/FrameMint

Repository files navigation

FrameMint 🌱

Library to make pixel buffer for video frame formatted 420YpCbCr8BiPlanarFullRange from UIKit/SwiftUI view or UIImage.

This library dose NOT written for real-time application.

Installation

  • Swift Package Manager

Usage

1. Create Canvas

Canvas is a class to manage pixel buffer as a source of video frame.

let canvasFactory = FrameMint.CanvasFactory()
let canvas = try canvasFactory.makeCanvas(width: width, height: height, extent: extent)

2. Add/Draw contents on canvas

a. Draw SwiftUI contents

Example to add and draw SwiftUI contents on canvas.

// Add SwiftUI contents on canvas.
canvas.addSwiftUIContents {
  ZStack(alignment: .center) {
    Color.white
    Text("Hello, world!")
      .font(.system(size: 40, weight: .medium))
  }
}
// Draw current canvas  contents on pixel buffer managed by canvas.
canvas.drawContents()

b. Draw UIKit contents

Example to add and draw UIKit contents on canvas.

// Create drawn view (Here is a simple UILabel).
let label = UILabel(frame: canvas.content.bounds)
label.backgroundColor = .white
label.text = "Hello, world!"
label.font = .systemFont(ofSize: 40, weight: .medium)
label.textAlignment = .center
// Add drawn view on content view managed by canvas.
canvas.content.addSubview(label)
// Draw current canvas contents on pixel buffer managed by canvas.
canvas.drawContents()

c. Draw Resources (UIImage)

Example to draw UIImage resource on canvas.

// Prepare image to draw.
let image: UIImage = ...
// Draw image on pixel buffer managed by canvas in same rect of pixel buffer.
canvas.drawImage(image)

3. Make video frame pixel buffer from canvas

a. Create Pixel Buffer

Example to make 420YpCbCr8BiPlanarFullRange format pixel buffer from canvas.

let pixelBuffer: CVPixelBuffer = canvas.make420YpCbCr8BiPlanarFullRangePixelBuffer()

b. Create Sample Buffer

Example to make 420YpCbCr8BiPlanarFullRange format sample buffer from canvas.

// `orientation` is a value about frame orientation related with RPVideoSampleOrientationKey
let sampleBuffer: CMSampleBuffer = canvas.make420YpCbCr8BiPlanarFullRangeSampleBuffer(orientation: orientation)

Clear canvas

Remove all SWiftUI/UIKit contents on canvas.

canvas.clearContents()

Sample Application

Sample application framemint_app to check detail of usage of this library contained in ./App Directory.

Home An example of preview for video frame

About

Make video frames in 420YpCbCr8BiPlanarFullRange from view representation of UIKit or SwiftUI.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages