import gobject
gobject.threads_init()
import gst
import sys
import os

def make_source (filename, start):
    def pad_added_cb(element, pad):
        sinkpad = scale.get_static_pad("sink")
        try:
            if sinkpad:
                pad.link(sinkpad)
        except gst.LinkError:
            print "linking failed!"

    b = gst.Bin()
    src = gst.element_factory_make("uridecodebin")
    src.connect("pad-added", pad_added_cb)
    src.props.uri = "file://" + os.path.abspath(filename)

    scale = gst.element_factory_make("videoscale")
    iconv = gst.element_factory_make("ffmpegcolorspace")
    oconv = gst.element_factory_make("ffmpegcolorspace")
    freeze = gst.element_factory_make("imagefreeze")

    b.add(src, scale, iconv, freeze, oconv)
    scale.link(iconv)
    iconv.link(freeze)
    freeze.link(oconv)

    b.add_pad(gst.GhostPad("src", oconv.get_static_pad("src")))

    s = gst.element_factory_make ("gnlsource")
    s.add(b)
    s.props.duration = gst.SECOND * 5
    s.props.start = start

    return s

p = gst.Pipeline()


sink = gst.element_factory_make("autovideosink")

def comp_pad_added(element, pad):
    sinkpad = sink.get_compatible_pad(pad)
    pad.link(sinkpad)

comp = gst.element_factory_make ("gnlcomposition")

start = 0
for filename in sys.argv[1:]:
    comp.add(make_source(filename, start))
    start += 5 * gst.SECOND

comp.connect("pad-added", comp_pad_added)

p.add(comp)
p.add(sink)

p.set_state(gst.STATE_PLAYING)
ml = gobject.MainLoop()
ml.run()
