根据一定的时间间隔获取视频帧

  连接直播地址,并设置每个几秒读取视频帧,并将图片保存

import cv2
from datetime import datetime,timedelta


video_full_path = "rtmp://192.168.18.23:1935/myapp/test1"
cap = cv2.VideoCapture(video_full_path)
print(cap.isOpened())
frame_count = 1

now = datetime.now()
print(now)
strnow = now.strftime('%Y-%m-%d %H:%M:%S')
period = timedelta(seconds=3)
next_time = now + period
strnext_time = next_time.strftime('%Y-%m-%d %H:%M:%S')
period1 = timedelta(seconds=10)
next_time1 = now + period1
strnext_time1 = next_time1.strftime('%Y-%m-%d %H:%M:%S')

while (datetime.now()-now < timedelta(seconds=10)):
    ral, frame = cap.read()
    iter_now = datetime.now()
    iter_now_time = iter_now.strftime('%Y-%m-%d %H:%M:%S')
    if str(iter_now_time) == str(strnext_time):
        iter_time = iter_now + period
        strnext_time = iter_time.strftime('%Y-%m-%d %H:%M:%S')
        print(1111)
        cv2.imwrite("static/" + "video" + "_%d.jpg" % frame_count, frame)
        frame_count = frame_count + 1
    continue
print(datetime.now())

你可能感兴趣的:(根据一定的时间间隔获取视频帧)