import gi
from gi.repository import GObject, Gst
gi.require_version('Gst', '1.0')
Gst.init(None)
pipeline = Gst.parse_launch('v4l2src do-timestamp=true device=/dev/video0 ! video/x-raw,width=848,height=480,framerate=20/1 ! videorate ! capsfilter caps="video/x-raw,framerate=1/2" name=timelapse_framerate ! queue ! jpegenc ! queue ! multifilesink post-messages=true location=test-%05d.jpg')
def on_message(bus, message):
try:
msgStruct = message.get_structure()
msgName = msgStruct.get_name()
if (msgName == "GstMultiFileSink"):
print("Multifilesink: recorded a timelapse image!!")
except Exception as e:
print("There was an error with processing the Gstreamer message")
print(e)
def check_pipeline_status(ret):
global bus, pipeline
if ret == Gst.StateChangeReturn.FAILURE:
print("Gstreamer: error starting pipeline")
bus = pipeline.get_bus()
msg = bus.pop_filtered(Gst.MessageType.ERROR
| Gst.MessageType.EOS)
if msg:
t = msg.type
if t == Gst.MessageType.ERROR:
err, dbg = msg.parse_error()
print("Gstreamer: Error element -" +
msg.src.get_name() + " - " + err.message)
if dbg:
print("Gstreamer: debugging info - " + dbg)
elif t == Gst.MessageType.EOS:
print("Gstreamer: End-Of-Stream reached")
else:
print("Gstreamer: Unexpected message received ")
bus = pipeline.get_bus()
bus.enable_sync_message_emission()
bus.add_signal_watch()
bus.connect("message", on_message)
ret = pipeline.set_state(Gst.State.PLAYING)
check_pipeline_status(ret)
loop = GObject.MainLoop()