Skip to content

Instantly share code, notes, and snippets.

@Circuitsoft
Last active December 20, 2015 00:29
Show Gist options
  • Save Circuitsoft/6042644 to your computer and use it in GitHub Desktop.
Save Circuitsoft/6042644 to your computer and use it in GitHub Desktop.
Trying to view a video one frame at a time. It works now that I make sure a reference to the buffer string always exists, via containing it in the deque.
#!/usr/bin/env python
import sys, os
import pygst
pygst.require("0.10")
import gst
import collections
import glib
import inspect
glib.threads_init()
avifile = gst.parse_launch("filesrc location=cap.avi ! avidemux ! appsink name=sink max-buffers=1")
vidout = gst.parse_launch("appsrc emit-signals=true name=src caps=video/x-raw-yuv,format=(fourcc)I420,framerate=(fraction)30/1,width=(int)1024,height=(int)768 ! xvimagesink")
avifile.set_state(gst.STATE_PLAYING)
framesink = avifile.get_by_name("sink")
vidsrc = vidout.get_by_name("src")
buf = framesink.emit("pull-buffer").make_metadata_writable()
avifile.set_state(gst.STATE_PAUSED)
ncmd = 'n'
pics = collections.deque([buf.data], 1)
def need_data(appsrc, tag):
appsrc.emit("push-buffer", gst.Buffer(pics[0]))
vidsrc.connect("need-data", need_data)
vidout.set_state(gst.STATE_PLAYING)
while True:
#buf.timestamp = 0
cmd = raw_input()
if cmd:
ncmd = cmd
if 'n' == ncmd:
avifile.set_state(gst.STATE_PLAYING)
pics.append(framesink.emit("pull-buffer").data)
avifile.set_state(gst.STATE_PAUSED)
if 'b' == ncmd:
pos = avifile.query_position(gst.FORMAT_TIME, None)[0]
print "pos: %s" % `pos`
pos -= (100000000)
print "pos: %s" % `pos`
avifile.set_state(gst.STATE_PLAYING)
avifile.seek_simple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, pos)
pics.append(framesink.emit("pull-buffer").data)
avifile.set_state(gst.STATE_PAUSED)
if 'w' == ncmd:
outfile = gst.parse_launch("appsrc name=src caps=video/x-raw-yuv,format=(fourcc)I420,framerate=(fraction)1/4,width=(int)1024,height=(int)768 ! avimux ! filesink location=outimg.avi")
outfile.set_state(gst.STATE_PLAYING)
outfile.get_by_name("src").emit("push-buffer", gst.Buffer(pics[0]))
outfile.get_by_name("src").emit("end-of-stream")
outfile.set_state(gst.STATE_PAUSED)
outfile.get_state()
outfile.set_state(gst.STATE_NULL)
outfile.get_state()
del outfile
if 'q' == ncmd:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment