How to play video in python? Here are two methods to introduce to you:
Method One
Using the imageio library
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()
Method Two
Using the cv2 library, the advantage of using this method is that it returns an array without conversion, but method one can specify a certain frame to be displayed, and this method reads from beginning to end.
import numpy as np import matplotlib.pyplot as plt import pylab import imageio import skimage.io import numpy as np import cv2 cap = cv2.VideoCapture('/path/to/your/video.mp4') while(cap.isOpened()): ret, frame = cap.read() cv2.imshow('image', frame) k = cv2.waitKey(20) #q键退出 if (k & 0xff == ord('q')): break cap.release() cv2.destroyAllWindows()
The above is the detailed content of How to play video in python. For more information, please follow other related articles on the PHP Chinese website!