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

def makeSrc(location, duration, speed):
    src = gst.element_factory_make("gnlfilesource")
    src.props.location = location
    src.props.media_start = 0
    src.props.duration = duration
    src.props.media_duration = long(speed * duration)
    return src

def comp_pad_added(comp, pad):
    tpad = csp.get_pad("sink")
    pad.link(tpad)

if len(sys.argv) < 4:
    print "usage: %s <file> <duration> <playback factor>"

location = os.path.abspath(sys.argv[1])
inpoint = long(gst.SECOND * float(sys.argv[2]))
speed = float(sys.argv[3])

assert speed > 0

comp = gst.element_factory_make("gnlcomposition")
comp.add(makeSrc(location, inpoint, speed))

comp.connect("pad-added", comp_pad_added)
csp = gst.element_factory_make("audioconvert")
sink = gst.element_factory_make("autoaudiosink")

p = gst.Pipeline()
p.add(comp, csp, sink)
gst.element_link_many(csp, sink)
p.set_state(gst.STATE_PLAYING)
ml = gobject.MainLoop()
ml.run()
