0

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

Darlyn
  • 4,715
  • 12
  • 40
  • 90
  • Possible duplicate : https://stackoverflow.com/questions/33311153/python-extracting-and-saving-video-frames – innicoder Mar 28 '18 at 16:47
  • 1
    Possible duplicate of [Python - Extracting and Saving Video Frames](https://stackoverflow.com/questions/33311153/python-extracting-and-saving-video-frames) – innicoder Mar 28 '18 at 16:47
  • i tried the solutions and it didnt work – Darlyn Mar 28 '18 at 16:47
  • Oh, I do apologize then you need an alternative way. How come that it saves empty files, does it read it properly that's the question? Does it support the filetype and is the encoding of the file same. – innicoder Mar 28 '18 at 16:49
  • i tried mp4 or mpeg. I am not sure how i can test if it read the video properly – Darlyn Mar 28 '18 at 17:27
  • Did you download the same video from the answer in the post @ElvirMuslic shared? You need to make sure the video is in the same folder as the python script. – fireant Mar 30 '18 at 00:25

2 Answers2

0

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

while(cap.isOpened()):

from

while(True):
puhuk
  • 464
  • 5
  • 15
-1

It seems your code has no problem, please try to run this command:

pip install opencv-python
Sahli Simo
  • 85
  • 2
  • 7