在Python中设置gstreamer管道

2024-05-18 12:24:17 发布

您现在位置: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_启动

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)

Tags: devimagetrueparsedevicevideourilaunch
1条回答
网友
1楼 · 发布于 2024-05-18 12:24:17

Furas把它钉在了their comment上。所有问题都是由主管道元素中的" "引起的。去掉它们可以修复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秒钟,然后在其他人需要时优雅地结束

相关问题 更多 >

    热门问题