简体   繁体   中英

extracting frames from video

I am trying to extract frames from video, i am using this code:

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

This returns

Read a new frame: False

Ihave followed various posts about this in SO. I installed ffmepg, av but nothing works.

I am running it on windows, installed opencv via Anaconda.

What is workaround for this? Thanks for help

I also tried

import cv2
import numpy as np
import os

# Playing video from file:
cap = cv2.VideoCapture('example.mp4')

try:
    if not os.path.exists('data'):
        os.makedirs('data')
except OSError:
    print ('Error: Creating directory of data')

currentFrame = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Saves image of the current frame in jpg file
    name = './data/frame' + str(currentFrame) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

However this saves empty images

How about change while loop in second code (15th line) to this

while(cap.isOpened()):

from

while(True):

看来你的代码没有问题,请尝试运行以下命令:

pip install opencv-python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM