# Are Sequences biting me with javacv frame reading?

**URL:** <https://discuss.kotlinlang.org/t/are-sequences-biting-me-with-javacv-frame-reading/10136>\
**Category:** Support\
**Created:** [November 1, 2018, 10:52pm UTC](https://discuss.kotlinlang.org/t/are-sequences-biting-me-with-javacv-frame-reading/10136 "2018-11-01T22:52:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [November 1, 2018, 10:52pm UTC](https://discuss.kotlinlang.org/t/are-sequences-biting-me-with-javacv-frame-reading/10136/1 "2018-11-01T22:52:25Z")

</div>

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.

```kotlin
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"))
}

```

[crossposted to javacv github.](https://github.com/bytedeco/javacv/issues/1079)
