hello i’m testing opencv with video. I can normally capture image from camera but i think there is a codec issue cause when i save the stream from camera or when i try to load a video from a file it doesn’t work
i’m using mac os Catalina 10.15.7, python 3.8.5, ffmpeg 4.3.1
this is my code
import cv2
import numpy as np
vid = cv2.VideoCapture('trailer.mp4')
while(vid.isOpened()):
ret, frame = vid.read()
cv2.imshow('frame',frame)
k = cv2.waitKey(25)
if k == 27:
break
vid.release()
cv2.destroyAllWindows()
The error i get is OpenCV: Couldn’t read video stream from file
that precise string does not occur within OpenCV (edit: I was mistaken. it’s related to AVFoundation, MacOS). nor can it apply to your code. your code reads from a camera device, not a file. your code writes to a file, it doesn’t read.
please be precise and complete in the error messages you get.
import cv2
import numpy as np
cam = cv2.VideoCapture(0) #get the first webcam
####defining the codec and creating the VideoWriter Obj
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out=cv2.VideoWriter('./assets/output.avi',fourcc,20.0,(1280,960))
while(cam.isOpened()):
ret, frame = cam.read()
if ret==True:
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cam.release()
out.release()
cv2.destroyAllWindows()
this is the code (the one i posted by mistake) i can change XVID with MP42 AVC1, but what would be the file extension in the name? is there a way to check which is the right codec? i tried to check the list of codec with ffmpeg -encoders