I thought I’d be all cool making a Kotlin sequence from the frames of a video. But the last image seems to clobber everything. This is very odd - maybe with how Sequences are evaluated in a lazy fashion, fighting with how internal memory is handled by Frames? Clearly I’m missing something, I thought that the immediate convert to BufferedImage would side-step any issues.
This example snippet will write the 500th frame out to both first.png and last.png - which is bad.
fun main() {
val converter: FrameConverter<BufferedImage> = Java2DFrameConverter()
val grabber = FFmpegFrameGrabber("https://github.com/mediaelement/mediaelement-files/blob/master/big_buck_bunny.mp4?raw=true")
grabber.start()
println("Opened video: ${grabber.imageWidth} x ${grabber.imageHeight} ${grabber.pixelFormat}")
val frames = sequence<BufferedImage> {
while (true) {
yield(converter.convert(grabber.grabImage() ?: break))
}
grabber.stop()
}.constrainOnce()
val first500 = frames.take(500).toList()
ImageIO.write(first500.first(), "png", File("first.png"))
ImageIO.write(first500.last(), "png", File("last.png"))
}