用opencv生成视频流,然后用rtsp进行拉流显示

python 复制代码
import cv2
import numpy as np
import time
from datetime import datetime
import gi
import socket
import sys
gi.require_version('Gst', '1.0')
try:
    gi.require_version('GstRtspServer', '1.0')
    from gi.repository import Gst, GLib, GstRtspServer
except ImportError as e:
    print(f"错误:无法导入 GstRtspServer,请确保已安装 gir1.2-gst-rtsp-server-1.0 包\n详细信息: {e}")
    sys.exit(1)
import threading

class RTSPServer:
    def __init__(self, port=8554, stream_name="test"):
        try:
            Gst.init(None)
        except Exception as e:
            print(f"GStreamer初始化失败: {e}")
            raise
        self.port = port
        self.stream_name = stream_name
        self.pipeline = None
        self.running = False
        self.host_ip = self.get_host_ip()
        self.appsrc = None  # 添加appsrc属性初始化

    def get_host_ip(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(('8.8.8.8', 80))
            ip = s.getsockname()[0]
            s.close()
            return ip
        except Exception:
            return '127.0.0.1'

    def start_server(self):
        try:
            # 创建和设置 pipeline
            pipeline_str = (
                'appsrc name=source is-live=true format=GST_FORMAT_TIME ! '
                'videoconvert ! video/x-raw,format=I420 ! '  # 添加格式转换
                'x264enc tune=zerolatency bitrate=500 ! '
                'h264parse ! rtph264pay name=pay0 pt=96 ! '
                'udpsink host=127.0.0.1 port=5000 sync=false'
            )
            
            self.pipeline = Gst.parse_launch(pipeline_str)
            self.appsrc = self.pipeline.get_by_name('source')
            
            # 设置 appsrc 属性
            self.appsrc.set_property('caps',
                Gst.Caps.from_string(
                    'video/x-raw,format=BGR,width=300,height=100,framerate=30/1'
                )
            )
            
            # 启动 pipeline
            self.pipeline.set_state(Gst.State.PLAYING)
            
            print(f"RTSP流地址: rtsp://{self.host_ip}:{self.port}/{self.stream_name}")
            
            # 创建RTSP服务器管道
            launch_str = (
                f'( udpsrc port=5000 ! application/x-rtp,media=video,clock-rate=90000,encoding-name=H264,payload=96 ! '
                f'rtph264depay ! h264parse ! rtph264pay name=pay0 pt=96 )'
            )
            
            # 初始化RTSP服务器
            self.server = GstRtspServer.RTSPServer()
            self.server.set_service(str(self.port))
            
            # 创建RTSP媒体工厂
            factory = GstRtspServer.RTSPMediaFactory()
            factory.set_launch(launch_str)
            factory.set_shared(True)
            
            # 将媒体工厂添加到服务器
            mount_points = self.server.get_mount_points()
            mount_points.add_factory(f"/{self.stream_name}", factory)
            
            # 启动服务器
            self.server.attach(None)
            
            self.running = True
        except Exception as e:
            print(f"启动RTSP服务器失败: {e}")
            self.stop_server()
            raise

    def push_frame(self, frame):
        if not self.running or not self.appsrc:
            return
        
        try:
            # 确保时间戳正确
            buffer = Gst.Buffer.new_wrapped(frame.tobytes())
            buffer.pts = Gst.CLOCK_TIME_NONE
            buffer.duration = Gst.CLOCK_TIME_NONE
            
            ret = self.appsrc.emit('push-buffer', buffer)
            if ret != Gst.FlowReturn.OK:
                print(f"推送帧失败: {ret}")
        except Exception as e:
            print(f"推送帧时发生错误: {e}")

    def stop_server(self):
        self.running = False
        if self.pipeline:
            self.pipeline.set_state(Gst.State.NULL)

def show_time():
    try:
        rtsp_server = RTSPServer()
        rtsp_server.start_server()
        
        img = np.zeros((100, 300, 3), dtype=np.uint8)
        
        while True:
            try:
                img_copy = img.copy()
                current_time = datetime.now().strftime('%H:%M:%S')
                cv2.putText(img_copy, current_time, (20, 60), 
                            cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
                
                cv2.imshow('Real-time Clock', img_copy)
                rtsp_server.push_frame(img_copy)
                
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            except Exception as e:
                print(f"处理帧时发生错误: {e}")
                break
    
    except Exception as e:
        print(f"程序运行出错: {e}")
    finally:
        if 'rtsp_server' in locals():
            rtsp_server.stop_server()
        cv2.destroyAllWindows()

if __name__ == '__main__':
    show_time()

功能都正常,就是延时有点严重。

相关推荐
hongmai6668887 分钟前
FH8856V310芯片详解:6M高清+0.5TOPS算力,赋能智能安防新方案
人工智能·单片机·嵌入式硬件·物联网·智能家居
一颗小树x8 分钟前
NVIDIA Jetson Thor 运行 LLM / VLM:模型全整理与 vLLM 实践
人工智能·llm·jetson·vllm
每日综合9 分钟前
蓝白风暴席卷BW2026!雷克沙展台首日燃情纪实
人工智能
To_OC10 分钟前
手搓 LangChain 工具调用:原来 Agent 的核心逻辑,就是个 while 循环
人工智能·langchain·llm
Drgfd20 分钟前
机器人从工厂走进生活:通用机器人开启大众商用时代
人工智能
硕迪科技SOLIDWORKS24 分钟前
SOLIDWORKS与ERP对接不再是研发孤岛:硕迪科技数据同步方案让BOM流转提速百分之三百
人工智能·科技·solidworks与erp
RestCloud25 分钟前
借助ETL工具,实现AI智能体+数据的落地
数据仓库·人工智能·sql·etl·etlcloud·数据集成平台·java脚本
AIGS00134 分钟前
企业AI落地的关键认知:向量空间JBoltAI的本体语义平台
java·人工智能·人工智能ai大模型应用
大龄牛码1 小时前
GPT-5.6 模型家族:分层矩阵与多智能体时代的全面开启
人工智能·gpt
记忆停留w1 小时前
从单体到微服务:Redis 协同 MySQL、Milvus、MinIO 搭建企业级RAG/AI Agent脚手架架构
大数据·人工智能·redis·微服务·ai·架构·milvus