Home  >  Article  >  Backend Development  >  How to play video in python

How to play video in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-22 10:21:1514500browse

How to play video in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn