Not able to play video recorded using OpenCV 4.4.0 on Raspberry Pi4

1.5k views Asked by At

I have OpenCV 4.4 on my Raspberry Pi Model 4B. I can play the video directly from camera. When I am recording the video and looking to play the file then I am not able to play the video. I am using VLC player. Here is the code I am using to record the video

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
if (cap.isOpened()==False) :
        print("error in opening  video stream")
        exit()

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('video.mp4',fourcc,20.0,(160,120),True)
while(cap.isOpened()):
        ret,frame = cap.read()
        if ret == True:
                frame = cv2.flip(frame,0)
                out.write(frame)
                cv2.imshow('frame',frame)
                # Press 'q' to quit
                if cv2.waitKey(25) & 0xFF == ord('q'):
                        break
                else:
                   break
cap.release()
out.release()
cv2.destroyAllWindows()

On running the above script, I am getting following warning

[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

When I tried to convert the recorded video using ffmpeg, I am getting error. Code I am using for conversion

ffmpeg -i video.mp4 -vcodec libx264 demo.mp4

Error details

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.20.100
  Duration: 00:00:00.00, bitrate: N/A
Output #0, mp4, to 'demo.mp4':
Output file #0 does not contain any stream

It seems the file is created but it has missing video codec. Looking for help.

Thank you

1

There are 1 answers

0
Abhimanyu On

Thank you everyone !

I am able to solve it. It seems there was an issue with the frame and height that I was providing to VideoWriter with respect to default VideoCapture resolution. Below-mentioned is the modified script

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

if (cap.isOpened()==False) :
        print("error in opening  video stream")
        exit()

fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
out = cv2.VideoWriter('video.avi',fourcc,10,(640,480)) 

while(cap.isOpened()):
        ret,frame = cap.read()
        if ret == True:
                frame = cv2.flip(frame,0)
                out.write(frame)
                cv2.imshow('frame',frame)
                # Press 'q' to quit
                if cv2.waitKey(25) & 0xFF == ord('q'):
                        break
        else:
          break
cap.release()
out.release()
cv2.destroyAllWindows()