import gst
import os
import gc

fname = os.tempnam()

pipeline = gst.element_factory_make('pipeline', 'snapshot')
src = gst.element_factory_make('videotestsrc', 'src')
encoder = gst.element_factory_make('pngenc', 'encoder')
encoder.set_property('compression-level', 0)
writer = gst.element_factory_make('filesink', 'writer')
writer.set_property('location', fname)

pipeline.add(src)
pipeline.add(encoder)
pipeline.add(writer)

src.link(encoder)
encoder.link(writer)

bus = pipeline.get_bus()

while True:
    gc.collect()
    pipeline.set_state(gst.STATE_PLAYING)
    bus.poll(gst.MESSAGE_EOS | gst.MESSAGE_ERROR, -1)
    pipeline.set_state(gst.STATE_NULL)
    
    os.remove(fname)
