1.安装ffmpeg
下载链接:「ffmpeg」https://www.aliyundrive.com/s/UoRHg9VMA8F
下载后将文件夹里面的bin文件添加到环境变量系统变量的path中。
pip install ffmpeg
pip install ffmpy
调用命令行(windows+R输入cmd)输入“ffmpeg –version”
2.安装opencv
pip install opencv-python
3.代码
其中rtmpUrl中填入,申请到的服务器地址和串流密钥
参考链接
import subprocess as sp
import cv2 as cv
rtmpUrl = "服务器地址+串流密钥"
cap = cv.VideoCapture(0)
# Get video information
fps = int(cap.get(cv.CAP_PROP_FPS))
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
# ffmpeg command
command = ['ffmpeg',
'-y',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', "{}x{}".format(width, height),
'-r', str(fps),
'-i', '-',
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-f', 'flv',
rtmpUrl]
# 管道配置
p = sp.Popen(command, stdin=sp.PIPE)
# # read webcamera
while (cap.isOpened()):
ret, frame = cap.read()
if not ret:
print("Opening camera is failed")
break
# process frame
# your code
# process frame
# write to pipe
p.stdin.write(frame.tostring())