在Python中设置gstreamer管道



我有一个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()

这会开始录制,等待10秒,然后在其他人需要时优雅地结束

最新更新