8

I'm trying to play a video file using python opencv this is my code , but it is not showing the vidfeo file when I run the code

import numpy as np
import cv2

cap = capture =cv2.VideoCapture('C2.mp4')
while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    cv2.waitKey(1)

cap.release()
cv2.destroyAllWindows()

I tried the answer in : link but not working again

1
  • 2
    why cap = capture = .... ? Do you enter in the while loop? Commented Nov 24, 2015 at 18:00

7 Answers 7

4

I think u just have to increase the number inside cv2.waitKey() function to may be 25 or 30. You should get the desired result.

Also, there is no need to write cap = capture = cv2.......

Simply, writing,

cap = cv2.videoCapture('path of the video')

should work as well . Hope it works.

Sign up to request clarification or add additional context in comments.

1 Comment

This answers the question! Compare question code with the OpenCV tutorial: opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/…
2
import numpy as np
import cv2

cap = cv2.VideoCapture('C2.mp4')
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('frame', gray)
        # & 0xFF is required for a 64-bit system
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows()

1 Comment

Welcome to SO! Can you add a bit of explanation as to how this fixes the OP's problem?
1

This code worked for me. It shows both the original and the grayscale video output. Press 'q' to exit. I also didnt see the need for cap = capture... in your code.

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',frame)
    cv2.imshow('grayF',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
         break

cap.release()
cv2.destroyAllWindows()

Comments

0

Try using this

cv.CaptureFromFile()

and also check out this link http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

1 Comment

Yeah but it has detail
0

First of all, there is no need of capture as you are not using capture in your code. The reason why your video file is not showing because you haven't saved it in the same directory, where you have saved the code.

You can either give the path where your file is saved as shown below

cap = cv2.VideoCapture('(path to the video file)/cv2.mp4')

Again you need to change the argument inside waitKey otherwise the program will not close the window that shows the video correctly.

Try out the following, it will definitely work. Put an if statement with waitKey() function and increase the argument which indicates the number of milliseconds it will wait for a key function to 25 or whatever number you may like so that when you press ESC key, the window will be destroyed:

if cv2.waitKey(25) & 0xFF == 27:
  break

Comments

0

The problem in my code was in the part (While). It should be while (True) instead the one in your code

Comments

-3

ren opencv_ffmpeg.dll to opencv_ffmpeg2413.dll on your project dir if opencv-2.4.13.exe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.