[PyKDE] I need some advices

ken.ramsey at nvl.army.mil ken.ramsey at nvl.army.mil
Tue Sep 7 15:47:46 BST 2004


Richard,

I've done a video app using PyQt, and for the display widget I used the
QGLWidget which takes advantage of OpenGL. I tried other approaches using
QCanvas and the like, but for a nice frame rate with larger frames OpenGL
was really the way to go. I'm hardly the OpenGL guru, but it wasn't too bad
to get what I needed up and running. I eventually had a video running with
symbology displayed on it, various custom mouse cursors, etc.

I used a QScrollView widget and attached the QGLWidget to it as a child.

Here's some sample code (untested) to give you a basic outline of what I
did. It does assume you have some kind of ImageSource class that exposes
frame info & extracts frames:

from time
from qt import *
from qtgl import *
from OpenGL.GL import *
from OpenGL.GLU import *

class GLMovie(QScrollView):
	
	def __init__(self, *args):
		apply(QScrollView.__init__, (self,)+args)
		self.glw = VideoDisplayWidget(self)
		self.addChild(self.glw)
		self.connect(self.glw, PYSIGNAL('movieStopped'), 
			self.movieStopped)
		self.connect(self.glw, PYSIGNAL('frameDrawn'),
			self.frameDrawn)

	def frameDrawn(self):
		self.emit(PYSIGNAL('frameDrawn'), ())

	def movieStopped(self):
		self.emit(PYSIGNAL('movieStopped'), ())

	def setFrameRate(self, rate):
		self.glw.frameRate = rate

	def getFrameRate(self):
		return self.glw.rate

	def playMovie(self):
		self.emit(PYSIGNAL('moviePlaying'), ())
		self.glw.playMovie()

	def stopMovie(self):
		self.glw.stopMovie()

	def loadFrame(self, frameno):
		self.glw.loadFrame(frameno)

	def setImageSource(self, imageSource):
		self.glw.setImageSource(imageSource)

	def getImageSource(self):
		self.glw.imageSource

class VideoDisplayWidget(QGLWidget):

	def __init__(self, *args):
		apply(QGLWidget.__init__, (self,)+args)
		self.startTime = None
		self.startFrame = 0
		self.frameno = -1
		self.imageSource = None
		self.timer = QTimer(self)
		self.movieIsRunning = 0
		self.frameRate = 30
		self.frame = None
		self.doLooping = 0
		self.connect(self.timer, SIGNAL('timeout()'),
			self.putUpNextFrame)

	def playMovie(self):
		self.movieIsRunning = 1
		self.startFrame = self.frameno
		self.startTime = time.clock()
		self.timer.start(0, 0)

	def stopMovie(self):
		self.movieIsRunning = 0
		self.timer.stop()
		self.emit(PYSIGNAL('movieStopped'), ())
			
	def putUpNextFrame(self):
		now = time.clock()
		elapsed = now - self.startTime
		frameno = int(round(self.frameRate * elapsed)) +
self.startFrame
		if frameno >= self.imageSource.numFrames:
			if self.doLooping:
				frameno = 0
				self.startTime = now
			else:
				self.stopMovie()
		else:
			if frameno != self.frameno:
				self.loadFrame(frameno)

	def loadFrame(self, frameno):
		self.frame = self.imageSource.getFrame(frameno)
		if not self.frame:
			return
		self.frame = self.frame.tostring()
		self.frameno = frameno
		self.update()

	def initializeGL(self):
		glClearColor(0.0, 0.0, 0.0, 0.0) # RGBA

		# I was working with frames that had the
		# following byte alignments.
		glPixelStore(GL_UNPACK_SKIP_PIXELS, 0)
		glPixelStore(GL_UNPACK_SKIP_ROWS, 0)
		glPixelStore(GL_UNPACK_ALIGNMENT, 1)

	def paintGL(self):
		glClear(GL_COLOR_BUFFER_BIT)
		if self.frame:
			# I was working with 8-bit grayscale video.
			# For RGB or something, change GL_LUMINANCE
			# and GL_UNSIGNED_BYTE to the appropriate thing.
			glDrawPixel(self.imageSource.frameWidth,
				self.imageSource.frameHeight,
				GL_LUMINANCE, GL_UNSIGNED_BYTE,
				self.frame)
		self.emit(PYSIGNAL('frameDrawn'), (self.frameno,))

	def resizeGL(self, w, h):
		if h == 0:
			h = 1
		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		glViewport(0.0, 0.0, float(w), float(h))
		glOrtho2D(0, w, 0, h)
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()

	def setImageSource(self, imageSource):
		self.imageSource = imageSource
		if not imageSource: return
		glPixelStore(GL_UNPACK_ROW_LENGTH, imageSource.frameWidth)
		self.resize(imageSource.frameWidth, imageSource.frameHeight)


Ken Ramsey
EOIR Technologies, Inc.
US Army Night Vision Lab
10221 Burbeck Road, Ste. 307/223
Fort Belvoir, VA 22060-5806
(703) 704 - 3459
kramsey at nvl.army.mil

-----Original Message-----
From: Richard Van Den Boom [mailto:rvdboom at free.fr] 
Sent: Monday, September 06, 2004 2:07 PM
To: pykde at mats.imk.fraunhofer.de
Subject: [PyKDE] I need some advices

Hi,

I'm working on a small video editing app (nothing too complicated). I would 
like to create a widget displaying the different tracks and possible 
transitions between them, a bit like Adobe Premiere does.
What QT Widget should I use for something like this? I suspect QCanvas or 
something like that but I'm not sure. Maybe something in PyKDE would be 
easier to do so.
Also, is there an easy way to compile the whole python app, once done, in a 
single binary file easily?

Thanks for any information.

Best regards,

 Richard

_______________________________________________
PyKDE mailing list    PyKDE at mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde




More information about the PyQt mailing list