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

查看:31
本文介绍了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.

推荐答案

这里下载这个video 所以我们有相同的视频文件用于测试.确保该 mp4 文件位于 Python 代码的同一目录中.然后还要确保从同一目录运行python解释器.

From here download this video so we have the same video file for the test. Make sure to have that mp4 file in the same directory of your python code. Then also make sure to run the python interpreter from the same directory.

然后修改代码,抛弃浪费时间的 waitKey 也没有窗口它无法捕获键盘事件.我们还打印 success 值以确保它成功读取帧.

Then modify the code, ditch waitKey that's wasting time also without a window it cannot capture the keyboard events. Also we print the success value to make sure it's reading the frames successfully.

import cv2
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

这是怎么回事?

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

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