1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
import cv2 from PIL import Image from io import BytesIO import random import time import functools import matplotlib.pyplot as plt
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 rval, frame, duration, width, height
def get_image_size(data): im = Image.open(BytesIO(data)) return im.size
if __name__ == '__main__': url = r'https://uipv4.zywvvd.com:33030/HexoFiles/win11-mt/20210909140933.mp4' rval, frame, duration, width, height = get_video_cover(url) print(rval) plt.imshow(frame) plt.show()
pass
|