rtspclientsink 无法自动切换到 TCP
作者: xunleiw创建于 2023年8月27日更新于 2026年9月30日
标签bugrtsp
start the server on a Linux AMD64 machine (following https://GitHub.com/bluenviron/mediamtx#Docker-image) with
Docker run --rm -it \ -e MTX_PROTOCOLS=tcp \ -p 8554:8554 \ -p 1935:1935 \ -p 8888:8888 \ -p 8889:8889 \ -p 8890:8890/udp \ bluenviron/mediamtxOn the same machine, publish with the sample code (following https://GitHub.com/bluenviron/mediamtx#opencv) with
from datetime import datetime
from time import sleep, time
import cv2
import numpy as np
fps = 15
width = 800
height = 600
colors = [
(0, 0, 255),
(255, 0, 0),
(0, 255, 0),
]
out = cv2.VideoWriter('appsrc ! videoconvert' + \
' ! video/x-raw,format=I420' + \
' ! x264enc speed-preset=ultrafast bitrate=600 key-int-max=' + str(fps * 2) + \
' ! video/x-h264,profile=baseline' + \
' ! rtspclientsink location=rtsp://localhost:8554/mystream',
cv2.CAP_GSTREAMER, 0, fps, (width, height), True)
if not out.isOpened():
raise Exception("can't open video writer")
curcolor = 0
start = time()
while True:
frame = np.zeros((height, width, 3), np.uint8)
# create a rectangle
color = colors[curcolor]
curcolor += 1
curcolor %= len(colors)
for y in range(0, int(frame.shape[0] / 2)):
for x in range(0, int(frame.shape[1] / 2)):
frame[y][x] = color
out.write(frame)
print("%s frame written to the server" % datetime.now())
now = time()
diff = (1 / fps) - now - start
if diff > 0:
sleep(diff)
start = now- encounter error
$ python3 mediamtx_example.py
2023-08-27 20:35:11.848800 frame written to the server
2023-08-27 20:35:11.911849 frame written to the server
2023-08-27 20:35:11.973843 frame written to the server
2023-08-27 20:35:12.036632 frame written to the server
2023-08-27 20:35:12.099610 frame written to the server
2023-08-27 20:35:12.164734 frame written to the server
2023-08-27 20:35:12.226825 frame written to the server
2023-08-27 20:35:12.288597 frame written to
…内容来源: bluenviron/mediamtx