ImageSequencer is a Swift framework for iOS, macOS, tvOS that allows you to create videos from a selection of images. Developed by Sam Stone.
In use in production with Lapsey. Create beautiful time-lapses on iOS - find out more here.
The API can be found in the ImageSequencerController
interface.
Go to File > Swift Packages > Add Package Dependency and add the following URL:
https://github.com/samst-one/ImageSequencer
- First we need to import the
ImageSequencer
into our project, we do this by importing the framework
import ImageSequencer
- Next we need to create a
ImageSequencerController
object. TheImageSequencerController
acts as the API for the package. We use theImageSequencerFactory
to do this. We also pass in the settings for the video we want to create. To create theImageSequencerController
, we do:
let outputUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("\(UUID().uuidString).mp4")
let renderSettings = RenderSettings(bitrate: 10000000,
size: CGSize(width: 1920,
height: 1080),
fps: 24,
outputUrl: outputUrl)
let controller = try? ImageSequencerFactory.make(settings: settings)
The make
method has the ability to throw.
- With the
controller
, we can now access the API. We first must start of some internalImageSequencer
processes before rendering. To do this, call:
controller.start()
-
When you have the images you want to render to a video, we can call the
render
function below. A breakdown of the parameters are as follows.- Parameters:
images
: The collection of images you wish to render in URL format.didAddFrame
: A closure that returns a double representing the percentage of rendering completed.completion
: A closure thats called when all the images have been rendered out. It returns an optionalError
.
- Parameters:
So the code looks a bit like this:
controller?.render(images: images) { percent in
} completion: { error in
}
- Once the
completion
handler has been called without an error, you call thefinish()
method to produce the video. The video can be found at the output URL that was provided in the render settings.
controller?.finish { outputUrl in
// Video now available at output URL provided.
}
In conclusion, to render out an sequence of images, use full code is below:
let outputUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("\(UUID().uuidString).mp4")
let renderSettings = RenderSettings(bitrate: 10000000,
size: CGSize(width: 1920,
height: 1080),
fps: 24,
outputUrl: outputUrl)
let controller = try? ImageSequencerFactory.make(settings: settings)
controller?.start()
controller?.render(images: images) { percent in
// Update the user on progress here.
} completion: { error in
if error == nil {
controller?.finish { outputUrl in
// Video now available at output URL provided.
}
}
}
A sample app is included.