多路视频并行拉流代码思路:从多路地址(rstp_urls.txt)中进行并行拉流(video_save.py)。
- rstp_urls.txt主要是拉流的地址,参考内容如下:
http://10.10.47.181:10002/track
rtsp://172.0.8.25:9981/534/analysis
rtsp://guest:Guest123@172.1.0.84:554/h264/ch1/main/av_stream
http://10.10.47.181:10001/track
rtsp://172.0.8.30:9981/904/analysis
rtsp://guest:Guest123@172.1.0.83:554/h264/ch1/main/av_stream
http://10.10.47.182:10006/track
rtsp://172.0.8.22:9980/91/analysis
rtsp://guest:Guest123@172.1.33.13:554/h264/ch1/main/av_stream
- video_save.py应注意修改对应路径(rstp_list),参考代码如下:
'''
import sys
# sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
sys.path.insert(0,'/home/ls/miniconda3/envs/yolo/lib/python3.8/site-packages')
import cv2
import time
import traceback
def test(rtsp_url):
cap = cv2.VideoCapture(rtsp_url)
if cap.isOpened():
success, frame = cap.read()
print('okokok')
else:
print("failed")
def pull_rtsp(rtsp_url, run_time=20, save_file=''):
print("CAP_FFMPEG...")
# save_file += "_FFMPEG"
videoWrite = False
# cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
cap = cv2.VideoCapture(rtsp_url, cv2.CAP_ANY)
# cap = cv2.VideoCapture(rtsp_url, cv2.CAP_GSTREAMER)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# fps = int(cap.get(cv2.CAP_PROP_FPS))
fps = 25
print(f"video{
size}, FPS:{
fps}")
# frame_time = time.time()
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# videoWrite = cv2.VideoWriter(f"{save_file+str(frame_time)}.avi", fourcc, fps, size)
strf_style = "%Y%m%d_%H%M%S"
frame_time = time.strftime(strf_style, time.localtime(time.time()))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
print(save_file)
videoWrite = cv2.VideoWriter(f"{
save_file+'/'+str(frame_time)}.mp4", fourcc, fps, size)
start_time = time.time()
while (time.time() - start_time) < run_time:
t