一.实现代码
复制
import cv2
from PIL import Image
from io import BytesIO
def tryTime(maxTry, timeout=random.random()):
"""
重试
:param maxTry:重试次数
:param timeout:睡眠时间
:return:
"""
def wrap1(func):
@functools.wraps(func)
def __decorator(*args,**kwargs):
tryTime = 0
while tryTime < maxTry:
try:
return func(*args,**kwargs)
except Exception:
pass
tryTime += 1
time.sleep(timeout)
return __decorator
return wrap1
@tryTime(2)
def get_video_cover(url):
cap = cv2.VideoCapture(url)
rate = cap.get(5)
frame_number = cap.get(7)
if rate==0:
duration=0
else:
duration = int(frame_number / rate)
cap.set(1, 1)
rval, frame = cap.read()
data = cv2.imencode(".jpg", frame)[1].tobytes()
cap.release()
height = frame.shape[0]
width = frame.shape[1]
return data, duration, width, height
def get_image_size(data):
im = Image.open(BytesIO(data))
return im.size
出 处:https://www.cnblogs.com/pythonywy/p/13749735.html