Author: Hakan Ardo <[email protected]>
Branch: extradoc
Changeset: r3640:3e4ca802db5a
Date: 2011-06-11 12:08 +0200
http://bitbucket.org/pypy/extradoc/changeset/3e4ca802db5a/
Log: reading/viewing video using mplayer
diff --git a/talk/iwtc11/benchmarks/image/io.py
b/talk/iwtc11/benchmarks/image/io.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/image/io.py
@@ -0,0 +1,39 @@
+import os, re, array
+
+def mplayer(Image, fn='tv://'):
+ f = os.popen('mplayer -really-quiet -noframedrop ' +
+ '-vo yuv4mpeg:file=/dev/stdout 2>/dev/null </dev/null ' + fn)
+ hdr = f.readline()
+ m = re.search('W(\d+) H(\d+)', hdr)
+ w, h = int(m.group(1)), int(m.group(2))
+ while True:
+ hdr = f.readline()
+ if hdr != 'FRAME\n':
+ break
+ yield Image(w, h, typecode='B', fromfile=f)
+ f.read(w*h/2) # Color data
+
+class MplayerViewer(object):
+ def __init__(self):
+ self.width = self.height = None
+ def view(self, img):
+ assert img.typecode == 'B'
+ if not self.width:
+ self.mplayer = os.popen('mplayer -really-quiet -noframedrop - ' +
+ '2> /dev/null ', 'w')
+ self.mplayer.write('YUV4MPEG2 W%d H%d F25:1 Ip A1:1\n' %
+ (img.width, img.height))
+ self.width = img.width
+ self.height = img.height
+ self.color_data = array.array('B', [127]) * (img.width *
img.height / 2)
+ assert self.width == img.width
+ assert self.height == img.height
+ self.mplayer.write('FRAME\n')
+ img.tofile(self.mplayer)
+ self.color_data.tofile(self.mplayer)
+
+default_viewer = MplayerViewer()
+
+def view(img):
+ default_viewer.view(img)
+
diff --git a/talk/iwtc11/benchmarks/image/noborder.py
b/talk/iwtc11/benchmarks/image/noborder.py
--- a/talk/iwtc11/benchmarks/image/noborder.py
+++ b/talk/iwtc11/benchmarks/image/noborder.py
@@ -3,10 +3,15 @@
class NoBorderImage(object):
"An image class for people who dont care about border effects"
- def __init__(self, w, h):
+ def __init__(self, w, h, typecode='d', fromfile=None):
self.width = w
self.height = h
- self.data = array('d', [0]) * (w*h)
+ if fromfile is not None:
+ self.data = array(typecode)
+ self.data.fromfile(fromfile, w*h)
+ else:
+ self.data = array(typecode, [0]) * (w*h)
+ self.typecode = typecode
def _idx(self, p):
if isinstance(p, Pixel):
@@ -42,6 +47,9 @@
def clone(self):
return self.__class__(self.width, self.height)
+ def tofile(self, f):
+ self.data.tofile(f)
+
class NoBorderImagePadded(NoBorderImage):
def __init__(self, w, h):
self.width = w
diff --git a/talk/iwtc11/benchmarks/image/test.avi
b/talk/iwtc11/benchmarks/image/test.avi
new file mode 100644
index
0000000000000000000000000000000000000000..e72f9f1b0e99f77baa54aa3f9ef4399b0b82ec45
GIT binary patch
[cut]
diff --git a/talk/iwtc11/benchmarks/image/view.py
b/talk/iwtc11/benchmarks/image/view.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/image/view.py
@@ -0,0 +1,6 @@
+from noborder import NoBorderImage
+from io import mplayer, view
+
+for img in mplayer(NoBorderImage, 'test.avi'):
+ view(img)
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit