oxtimeline/oxgst/video.py

163 lines
5 KiB
Python
Raw Normal View History

2009-01-18 08:39:14 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2009-01-18 08:39:14 +00:00
# GPL 2008
import gobject
gobject.threads_init()
import pygst
pygst.require("0.10")
import gst
import Image
import time
from singledecodebin import SingleDecodeBin
from imagesink import ImageSink
class Video(gst.Pipeline):
#def log(self, msg):
# print msg
2009-01-18 08:39:14 +00:00
2010-08-20 10:11:17 +00:00
def __init__(self, uri, framerate='25/1'):
2009-01-18 08:39:14 +00:00
gst.Pipeline.__init__(self)
self.framerate = framerate
2009-01-18 08:39:14 +00:00
self.duration = -1
# queue of timestamps
self._queue = []
2009-01-18 08:39:14 +00:00
# queue callbacks
self.callback = {}
# extracted frames
2009-01-18 23:43:30 +00:00
self._frames = {}
2009-01-18 08:39:14 +00:00
# true only if we are prerolled
self._ready = False
2009-01-18 08:39:14 +00:00
uri = 'file://' + uri
self.log("uri : %s" % uri)
self.uri = uri
self.sbin = SingleDecodeBin(caps=gst.Caps("video/x-raw-rgb;video/x-raw-yuv"),
uri=self.uri)
self.csp = gst.element_factory_make("ffmpegcolorspace")
self.scale = gst.element_factory_make("videoscale")
2010-08-20 10:11:17 +00:00
self.rate = gst.element_factory_make("videorate")
self.queue = gst.element_factory_make("queue")
2009-01-18 08:39:14 +00:00
self.sink = ImageSink()
self.sink.connect('frame', self._frameCb)
self.add(self.sbin, self.csp, self.queue, self.scale, self.rate, self.sink)
2009-01-18 08:39:14 +00:00
self.sbin.connect('pad-added', self._sbinPadAddedCb)
2009-01-18 08:39:14 +00:00
self.set_state(gst.STATE_PAUSED)
self.get_state()
self.width = self.sink.width
self.height = self.sink.height
self.framerate = self.sink.framerate
self.getDuration()
self.frames = int((float(self.duration) / gst.SECOND) * float(self.framerate))
2009-01-18 08:39:14 +00:00
def _sbinPadAddedCb(self, unused_sbin, pad):
self.log("pad : %s" % pad)
2010-08-20 10:11:17 +00:00
pad.link(self.queue.get_pad("sink"))
caps = pad.get_caps()
width = caps[0]["width"]
height = caps[0]["height"]
if width % 4:
width += 4 - width % 4
if height % 4:
height += 4 - height % 4
self.queue.link(self.scale)
self.scale.link(self.rate)
self.rate.link(self.csp, gst.Caps("video/x-raw-rgb;video/x-raw-yuv,framerate=%s,width=%s,height=%s" % (self.framerate, width, height)))
self.csp.link(self.sink)
2009-01-18 08:39:14 +00:00
def _frameCb(self, unused_thsink, frame, timestamp):
self.log("image:%s, timestamp:%s" % (frame, gst.TIME_ARGS(timestamp)))
if not self._ready:
# we know we're prerolled when we get the initial thumbnail
self._ready = True
elif timestamp in self.callback and self.callback[timestamp]:
self.callback[timestamp](frame, timestamp)
del self.callback[timestamp]
if timestamp in self._queue:
self._queue.remove(timestamp)
2009-01-18 08:39:14 +00:00
if self._queue:
2009-01-18 08:39:14 +00:00
# still some more thumbnails to process
gobject.idle_add(self._getFrame, self._queue.pop(0))
2009-01-18 08:39:14 +00:00
def getDuration(self):
if self.duration < 0:
pads = self.sink.sink_pads()
q = gst.query_new_duration(gst.FORMAT_TIME)
for pad in pads:
if pad.get_peer() and pad.get_peer().query(q):
format, self.duration = q.parse_duration()
return self.duration
2009-01-18 08:39:14 +00:00
def getFrame(self, timestamp, callback):
"""
Queue a frame request for the given timestamp,
callback is called once frame is extracted.
returns False if timestamp > duration
"""
self.log("timestamp %s" % gst.TIME_ARGS(timestamp))
if self.duration < 0:
self.getDuration()
2009-01-18 08:39:14 +00:00
if timestamp > self.duration:
self.log("timestamp %s > duration %s" % (timestamp, self.duration))
return False
2009-01-18 08:39:14 +00:00
self.callback[timestamp] = callback
if self._queue or not self._ready:
2009-01-18 08:39:14 +00:00
self.log('ready')
self._queue.append(timestamp)
2009-01-18 08:39:14 +00:00
else:
self._queue.append(timestamp)
2009-01-18 08:39:14 +00:00
self._getFrame(timestamp)
return True
def _getFrame(self, timestamp):
if not self._ready:
return
self.log("timestamp : %s" % gst.TIME_ARGS(timestamp))
self.seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
gst.SEEK_TYPE_SET, timestamp,
gst.SEEK_TYPE_NONE, -1)
return False
def frame(self, timestamp):
2009-06-21 19:42:10 +00:00
self.mainloop = gobject.MainLoop()
2009-01-18 23:43:30 +00:00
self._frames[timestamp] = None
2009-01-18 08:39:14 +00:00
def callback(frame, timestamp):
2009-01-18 23:43:30 +00:00
self._frames[timestamp] = frame
2009-06-21 19:42:10 +00:00
self.mainloop.quit()
2009-06-24 18:21:08 +00:00
self._quit = False
2009-06-21 20:25:38 +00:00
def quit():
if self._quit:
self.mainloop.quit()
return False
else:
self._quit = True
return True
gobject.timeout_add(1000, quit)
2009-01-18 08:39:14 +00:00
if self.getFrame(timestamp, callback):
2009-06-21 19:42:10 +00:00
self.mainloop.run()
2009-01-18 23:43:30 +00:00
frame = self._frames[timestamp]
del self._frames[timestamp]
2009-01-18 08:39:14 +00:00
return frame