python怎样播放视频?
python播放视频的方法:
1、使用OpenCV播放视频
import numpy as np import cv2 as cv cap = cv.VideoCapture('vtest.avi') while cap.isOpened(): ret, frame = cap.read() # if frame is read correctly ret is True if not ret: print("Can't receive frame (stream end?). Exiting ...") break gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) cv.imshow('frame', gray) if cv.waitKey(1) == ord('q'): break cap.release() cv.destroyAllWindows()
2、使用imageio库
import pylab import imageio #视频的绝对路径 filename = '/path/to/your/video.mp4' #可以选择解码工具 vid = imageio.get_reader(filename, 'ffmpeg') for im in enumerate(vid): #image的类型是mageio.core.util.Image可用下面这一注释行转换为arrary #image = skimage.img_as_float(im).astype(np.float32) fig = pylab.figure() fig.suptitle('image #{}'.format(num), fontsize=20) pylab.imshow(image) pylab.show()
更多Python知识请关注云海天Python教程栏目。
来源:PY学习网:原文地址:https://www.py.cn/article.html