python读取视频流提取视频帧的方法

方法一:通过imageio库和skimage库

1. 安装环境:

pip install imageio

pip install skimage

这时候会报错Please install the `scikit-image` package (instead of `skimage`)

所以按照提示操作即可:

pip install scikit-image

环境安装成功。

2.通过python安装ffmpge(用来解码视频流)

import imageio

imageio.plugins.ffmpeg.download()

这时候会提示:

Imageio: 'ffmpeg.linux64' was not found on your computer; downloading it now.Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg.linux64 (27.2 MB)Downloading: 8192/28549024 bytes

开始进行下载ffmpeg

注意:下载这个文件很容易断线,所有可以先拿下载工具下载后,再放到下面路径,然后系统就会认为文件已经下载完了。 
文件路径: /root/.imageio/ffmpeg/ffmpeg.linux6

如果不断线的话,文件就会存放在上述路径中。

下次执行就可以直接读取视频流了。

3. 使用imageio库提取视频帧

imageio官方文档地址:http://imageio.readthedocs.io/en/latest/examples.html

用法:

# coding:utf-8
import pylab
import imageio
#注释的代码执行一次就好,以后都会默认下载完成
#imageio.plugins.ffmpeg.download()
import skimage
import numpy as np

#视频的绝对路径
filename = 'video/01_01.wmv'
#可以选择解码工具
vid = imageio.get_reader(filename,  'ffmpeg')
for num,im in enumerate(vid):
    #image的类型是mageio.core.util.Image可用下面这一注释行转换为arrary
    print im.mean()
    image = skimage.img_as_float(im).astype(np.float64)
    fig = pylab.figure()
    fig.suptitle('image #{}'.format(num), fontsize=20)
    pylab.imshow(im)
pylab.show()

方式二:通过cv2库

用法:

import cv2
video_full_path="video/01_01.wmv"
cap  = cv2.VideoCapture(video_full_path)
print cap.isOpened()
frame_count = 1
success = True
while(success):
    success, frame = cap.read()
    print 'Read a new frame: ', success

    params = []
    #params.append(cv.CV_IMWRITE_PXM_BINARY)
    params.append(1)
    cv2.imwrite("video" + "_%d.jpg" % frame_count, frame, params)

    frame_count = frame_count + 1

cap.release()
存在问题:cap.isOpened()总是输出false,表示打开失败。没办法读取

问题一直没有解决,如果有哪位大神解决还往告知!!






你可能感兴趣的:(python)