使用opencv来做rtsp拉流和rtmp推流

电脑用wifi连接到内网摄像头上,用rtsp获取视频流,然后再用rtmp协议从lte模块上推流出去。

ubuntu 16.04 使用quectel ec20可以直接上网,ubuntu 18.04 使用huawei e3372。用的是mobile broadband 不是 pppd

要先断开 wifi (rtsp),然后插入 lte dongle,再然后运行 sudo route add -net 0.0.0.0 dongle_name。完成以后再连上wifi摄像头

import cv2
import subprocess
 
rtsp = "rtsp_address"
rtmp = "rtmp_address"

cap = cv2.VideoCapture(rtsp)
#size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
size = (1280,720)
sizeStr = str(size[0]) + 'x' + str(size[1])
 
command = ['ffmpeg',
    '-y', '-an',
    '-f', 'rawvideo',
    '-vcodec','rawvideo',
    '-pix_fmt', 'bgr24',
    '-s', sizeStr,
    '-r', '25',
    '-i', '-',
    '-c:v', 'libx264',
    '-pix_fmt', 'yuv420p',
    '-preset', 'ultrafast',
    '-f', 'flv',
    rtmp]

pipe = subprocess.Popen(command
    , shell=False
    , stdin=subprocess.PIPE
)

while 1:
    ret, frame = cap.read()
    
    cv2.imshow("frame", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break    
    pipe.stdin.write(frame.tostring())
 
cap.release()
pipe.terminate()

 

你可能感兴趣的:(使用opencv来做rtsp拉流和rtmp推流)