繁体   English   中英

在 Python 中设置 gstreamer 管道

[英]Setting up gstreamer pipeline in Python

我有一个Gstreamer管道,它在使用gst-launch时可以工作,但我无法让它在 python 脚本中工作。

gst-launch-1.0 v4l2src do-timestamp=true device=/dev/video0 ! image/jpeg,width=1920,height=1080,framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv flip-method=2 ! clockoverlay halignment=left valignment=bottom text="Dashcam" shaded-background=true font-desc="Sans, 12" ! omxh264enc ! 'video/x-h264, streamformat=(string)byte-stream' ! h264parse ! qtmux ! filesink location=video.mp4

我已经尝试在gst-launch之后对所有内容使用gst-parse ,尝试过 subprocess.popen,但无法弄清楚 gstreamer 元素工厂的头部或尾部。 有小费吗?

我尝试过的事情:

import subprocess

dashcam = 'gst-launch-1.0 v4l2src do-timestamp=true device=/dev/video0 ! image/jpeg,width=1920,height=1080,framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv flip-method=2 ! clockoverlay halignment=left valignment=bottom text="Dashcam" shaded-background=true font-desc="Sans, 12" ! omxh264enc ! "video/x-h264, streamformat=(string)byte-stream" ! h264parse ! qtmux ! filesink location=video.mp4 -e'
dashcamPipe = subprocess.Popen(dashcam.split(), stdout=subprocess.PIPE)
output, error = dashcamPipe.communicate()

给我这些错误

gst-launch-1.0:18562): GStreamer-CRITICAL **: 07:47:51.920: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed

(gst-launch-1.0:18562): GStreamer-CRITICAL **: 07:47:51.927: gst_element_link_pads_filtered: assertion 'GST_IS_BIN (parent)' failed

使用 Gst.parse_launch

import gi 
import sys
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject

Gst.init(sys.argv)

pipeline = Gst.parse_launch('v4l2src do-timestamp=true device=/dev/video0 ! image/jpeg,width=1920,height=1080,framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv flip-method=2 ! clockoverlay halignment=left valignment=bottom text="Dashcam" shaded-background=true font-desc="Sans, 12" ! omxh264enc ! "video/x-h264, streamformat=(string)byte-stream" ! h264parse ! ! qtmux ! filesink location=video.mp4

给我

(record.py:18791): GStreamer-CRITICAL **: 07:53:00.456: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed

(record.py:18791): GStreamer-CRITICAL **: 07:53:00.463: gst_element_link_pads_filtered: assertion 'GST_IS_BIN (parent)' failed
Traceback (most recent call last):
  File "record.py", line 8, in <module>
    pipeline = Gst.parse_launch('v4l2src do-timestamp=true device=/dev/video0 ! image/jpeg,width=1920,height=1080,framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv flip-method=2 ! clockoverlay halignment=left valignment=bottom text="Dashcam" shaded-background=true font-desc="Sans, 12" ! omxh264enc ! "video/x-h264, streamformat=(string)byte-stream" ! h264parse ! qtmux ! filesink location=video.mp4')
GLib.Error: gst_parse_error: syntax error (0)

Furas他们的评论中指出了这一点。 这是导致所有问题的主要管道元素中的" " 将它们取出来修复popen和解析。

新的(和更新的)代码:

import gi 
import sys
from time import sleep

gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject

Gst.init(sys.argv)

pipeline = Gst.parse_launch('v4l2src do-timestamp=true device=/dev/video0 ! image/jpeg,width=1920,height=1080,framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv flip-method=2 ! clockoverlay halignment=left valignment=bottom text="Dashcam" shaded-background=true font-desc="Sans, 12" ! omxh264enc ! "video/x-h264, streamformat=(string)byte-stream" ! h264parse ! ! qtmux ! filesink location=video.mp4')

def main():
  bus = pipeline.get_bus()      
  pipeline.set_state(Gst.State.PLAYING)
  print('Recording Dashcam')
  
  sleep(10) 
  print('Sending EOS')
  pipeline.send_event(Gst.Event.new_eos())
  print('Waiting for EOS')
  bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS)
  pipeline.set_state(Gst.State.NULL)

main()

这将开始录音,等待十秒钟,然后在其他人需要时优雅地结束录音。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM