Python-提取和保存视频帧 [英] Python - Extracting and Saving Video Frames

查看:129
本文介绍了Python-提取和保存视频帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遵循了

So I've followed this tutorial but it doesn't seem to do anything. Simply nothing. It waits a few seconds and closes the program. What is wrong with this code?

import cv2
vidcap = cv2.VideoCapture('Compton.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
  success,image = vidcap.read()
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  if cv2.waitKey(10) == 27:                     # exit if Escape is hit
      break
  count += 1

此外,它在评论中说这将帧数限制为1000?为什么?

Also, in the comments it says that this limits the frames to 1000? Why?

我尝试先做success = True,但这没有帮助.它只创建了一个0字节的图像.

I tried doing success = True first but that didn't help. It only created one image that was 0 bytes.

推荐答案

这是有效的最终代码:

import cv2
print(cv2.__version__)
vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')
success,image = vidcap.read()
count = 0
while success:
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  success,image = vidcap.read()
  print ('Read a new frame: ', success)
  count += 1

因此,要使其正常工作,您将必须获得一些东西.首先,下载OpenCV2 .然后为Python安装此软件2.7.x.转到3rd party文件夹内的FFmpeg文件夹(类似于C:\OpenCV\3rdparty\ffmpeg,但我不确定).复制opencv_ffmpeg.dll(如果您的python版本是x64,则复制x64版本)并将其粘贴到您的Python文件夹中(可能是C:\Python27).如果您的OpenCV版本是3.0.0,则将其重命名为opencv_ffmpeg300.dll(您可以在此处),然后相应地更改为您的版本.顺便说一句,您必须在您的环境路径中有您的 python文件夹.

So for this to work, you'll have to get some stuff. First, download OpenCV2. Then install this for Python 2.7.x. Go to the FFmpeg folder inside the 3rd party folder (something like C:\OpenCV\3rdparty\ffmpeg, but I'm not sure). Copy opencv_ffmpeg.dll (or the x64 version if your python version is x64) and paste it into your Python folder (probably C:\Python27). Rename it opencv_ffmpeg300.dll if your OpenCV version is 3.0.0 (you can find it here), and change accordingly to your version. Btw, you must have your python folder in your environment path.

这篇关于Python-提取和保存视频帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆