I’ve got a sequence (of images). I’d like to init some stuff (a video recorder) based on info from the sequence (the resolution of the images). Is there any way to say “if this is the first element of the sequence, do this extra stuff, otherwise, do normal things”?
Here is the catch: The video recorder is an AutoClosable resource. So I’d love to “peek” into the first element in the sequence, but I can’t.
fun Sequence<BufferedImage>.framesToVideoFile(
destinationFileName: String = "out.mp4",
imageWidth: Int = 1280, // *** I want these to default to the image res
imageHeight: Int = 720 // *** I want these to default to the image res
) {
var frameCount = 0
FFmpegFrameRecorder(destinationFileName, imageWidth, imageHeight, 0).apply {
frameRate = 30.0
videoBitrate = 0 // max
videoQuality = 0.0 // max
start()
}.use { ffr ->
LOG.info { "Starting recording to $destinationFileName (${ffr.imageWidth}, ${ffr.imageHeight})" }
for (bi in this) {
val frame = BufferedImage(ffr.imageWidth, ffr.imageHeight, BufferedImage.TYPE_INT_ARGB)
frame.createGraphics()!!.apply {
drawImage(bi, 0, 0, null)
dispose()
}
ffr.record(converter.get().convert(frame), avutil.AV_PIX_FMT_ARGB)
if (frameCount > 0 && frameCount and frameCount - 1 == 0) {
LOG.debug { "Recorded frame $frameCount" }
}
frameCount++
}
}
LOG.info { "All frames recorded: $frameCount" }
}