OpenCV使用RTMP流
使用RTMP流之前使用nginx进行推流
sudo apt-get install nginx libnginx-mod-rtmp
配置文件中添加以下内容:
rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; } } }
Linux下安装v4l查看摄像头名称:
sudo apt-get install v4l-utils dog:~$ v4l2-ctl --list-devices C922 Pro Stream Webcam (usb-0000:02:00.0-3): /dev/video0 /dev/video1 /dev/media0
相应Python代码:
#encoding:utf-8
import cv2
import time
import threading
import subprocess
def start_rtmp_service():
cmd = "ffmpeg -f v4l2 -framerate 30 -video_size 1280x720 -i /dev/video0 -c:v libx264 -preset veryfast -maxrate 1000k -bufsize 2000k -pix_fmt yuv420p -g 50 -f flv rtmp://127.0.0.1/live"
p = subprocess.Popen(cmd,shell=True)
p.communicate()
def start_video_service():
url = "rtmp://127.0.0.1/live"
cap = cv2.VideoCapture(url)
while 1:
if cap.isOpened():
ret,frame = cap.read()
cv2.imshow("win",frame)
key = cv2.waitKey(1)
if (key & 0xFF == ord("q")):
break
cap.release()
cv2.destroyAllWindows()
t1 = threading.Thread(target=start_video_service)
t1.start()
time.sleep(2)
t2 = threading.Thread(target=start_rtmp_service)
t2.start()
参考文章:
https://obsproject.com/forum/resources/how-to-set-up-your-own-private-rtmp-server-using-nginx.50/