提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
实现离线文件推流成rtsp
1.安装ffmpeg
省略 前面写过
安装ffmpeg
2.进行搭建 rtsp 服务器
地址在自己的百度云 /脚本/rtsp服务脚本
1
解压 rtsp-simple-server_v0.17.17_linux_amd64.tar.gz
2 启动 rtsp 服务
执行 ./rtsp-simple-server
3.执行ffmpeg命令
# 将 本地front1.mp4 文件 推流成 本地服务 rtsp
ffmpeg -re -stream_loop -1 -i 20211102104550/front1.mp4 -c copy -f rtsp rtsp://localhost:8554/mystream2
4.启动脚本加载rtsp流
import cv2
import time
from queue import Queue
from threading import Thread
class VideoManager:
def __init__(self, videoPath, cam_type="none", rotate_type=False):
self.cap = cv2.VideoCapture(videoPath)
self.videoPath = videoPath
self.seq_length = 16
self.skip_num = 1
self.frame_buffer = Queue()
self.frame_idx = 0
self.cam_type = cam_type
self.rotate_type = rotate_type
def __iter__(self):
return self
def reset(self):
self.cap.release()
self.cap = cv2.VideoCapture(self.videoPath)
while not self.frame_buffer.empty():
self.frame_buffer.get()
return self.cap.isOpened()
def release(self):
self.cap.release()
def __next__(self):
was_read = True
frames = []
idx = 0
while was_read and len(frames) < self.seq_length:
was_read, frame = self.cap.read()
idx += 1
# 用于抽帧
if (idx - 1) % self.skip_num != 0:
continue
if self.rotate_type and self.cam_type == "top":
frame = frame[::-1, ::-1, :].copy()
# time.sleep(2e-2)
frames.append(frame)
if was_read:
self.frame_buffer.put(frame)
if self.frame_buffer.qsize() > self.seq_length:
self.frame_buffer.get()
if len(frames) > 0 and len(frames) < self.seq_length:
frames.clear()
while not (self.frame_buffer.empty()):
frames.append(self.frame_buffer.get())
was_read = True
if len(frames) == 0:
was_read = False
self.frame_idx += 1
return was_read, frames, self.frame_idx
def start(self):
return self
def videoReadMp4(cap_manager, videoFrameQueue): # 用MP4视频
ret = True
while ret:
# if videoFrameQueue.full():
# time.sleep(1e-3)
# continue
ret, frame, frame_id = next(cap_manager)
if not frame:
continue
frame = cv2.resize(frame[0], (960,540))
cv2.imshow('', frame)
cv2.waitKey(1)
# if ret:
# videoFrameQueue.put(frame)
else:
cap_manager.release()
# logger.info('视频检测完成')
print('检测完成')
# self.timeout_queue.put(self.flag)
# self.detectEnd()
if __name__ == '__main__':
# rtsp = r"C:\Users\caibaojun\Desktop\lldvideo\merge\new.mp4"
# rtsp = r"C:\Users\caibaojun\Desktop\lldvideo\merge\new3.avi"
rtsp = "rtsp://192.168.5.18:8554/mystream2"
videoFrameQueue = Queue()
cap_manager = VideoManager(rtsp, cam_type="front", rotate_type=False)
t = Thread(target=videoReadMp4, args=(cap_manager, videoFrameQueue))
t.start()