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/

posted @   月薪几千的牛马  阅读(74)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示