I'm trying to resize a video input using OpenCV and export it as .avi or .mp4 file. For some unknown reason, the result file outcomes corrupted and refuses to be played.
I have tried several different combinations of video formats and codec types, such as: avi-MJPG, avi-XVID, avi-h264, avi-x264, mp4-mp4v and mp4-avc3. None of them seems to be working correctly.
import cv2
cap = cv2.VideoCapture('input.mp4')
out = cv2.VideoWriter(
'output.avi',
cv2.VideoWriter_fourcc(*'XVID'),
cap.get(cv2.CAP_PROP_FPS),
(640, 480)
)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (640, 480))
out.write(frame)
# cv2.imshow("video", frame)
# cv2.waitKey(1)
cap.release()
out.release()
# cv2.destroyAllWindows()
UPD: Running this code on Windows 10 machine with Python 3.11.1, using pre-built opencv-python (4.7.0.68) and opencv-contrib-python (4.7.0.68).
As mentioned below this code works perfectly fine in another runtime environment, so probably this issue is related with OS.
Related
I'd like to save video camera output in motion JPEG (MJPG) format. The below code,
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print("Unable to read camera feed")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
frame_per_sec = int('10')
out = cv2.VideoWriter('output.mjpeg',cv2.VideoWriter_fourcc('M','J','P','G'), (frame_per_sec), (frame_width,frame_height))
while(True):
ret, frame = cap.read()
if ret == True:
# Write the frame into the file 'output.mjpeg'
out.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
# Press Q on keyboard to stop recording
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
While it will run, I am getting the following error(s),
[ WARN:0] OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
OpenCV: FFMPEG: tag 0x67706a6d/'mjpg' is not supported with codec id 7 and format 'mjpeg / raw MJPEG video'
What can I do to resolve these? I've tried changing the case, ('M','J','P','G' to 'm','j','p','g') with no success.
Appreciate any suggestions regarding resolving the above issue, as well as the GStreamer issue. Thanks in advance.
.mjpeg is not a valid suffix for any known container format.
I'm sure you didn't intend to write a raw MJPG stream without a container. That is very very rarely useful at all and requires expert knowledge.
You have two options:
use MJPG in a .avi container, because that's built into OpenCV and doesn't even require ffmpeg
use whatever ffmpeg understands, which would be a .mpg container, or .mov or .mkv or whatever else
After cap.release() the only Frame is getting closed, webcam light is still ON.
import cv2
cap = cv2.VideoCapture(0)
#cap = cv2.VideoCapture(-1) if i give '-1' instead of '0' then light is getting OFF
#but camera is not working because i don't have second camera to laptop.
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cap.isOpened() #returns False
cv2.destroyAllWindows()
By pressing 'q', the Frame is getting closed but webcam light is still ON.
How to OFF the webcam? (It is getting OFF after python shell is closed.)
If possible, tell me the path of cv2.VideoCapture() class source code.
Set OPENCV_VIDEOIO_PRIORITY_MSMF=0 in your environment variables. Seems like there is an instance leak in opencv library. If you're on windows maybe use setx in your cmd to set the value setx OPENCV_VIDEOIO_PRIORITY_MSMF 0.
Reference to the issue : here
And it looks like the issue has been fixed too. So try updating your opencv library or reinstalling altogether.
That should solve your problem.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'): break
else: break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
It records properly using python but when I convert it using pyinstaller the video produces an output.avi without content (0 byte). I also tried cx_freeze but the result is the same.
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
You can probably try changing this to
out = cv2.VideoWriter('output.mp4',fourcc, 20.0, (640,480))
I know basically nothing except to change to mp4 or wav in the programming. But once you get the file out of the program you could save the file and change it's encoding. You can do this on windows by renaming it. But I don't know about Macs. Or if you wanted to you could make it .exe if you wanted.
I want to save my webcam video using opencv.
I wrote this code.
import numpy as np
import cv2
cap=cv2.VideoCapture(0)
#Define the codec
#FourCC code is passed as cv2.VideoWriter_fourcc('M','J','P','G')
#or cv2.VideoWriter_fourcc(*'MJPG') for MJPG.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#Define VideWrite object
#cv2.VideoWrite('arg1',arg2,arg3,(width,heigh))
#arg1:output file name
#arg2:Specify Fourcc code
#arg3: frames per seconds
#FourCC is a 4-byte code used to specify video codec
out=cv2.VideoWriter('SaveAVideo.avi',fourcc,20.0, (640,480))
while(cap.isOpened()):
ret,frame = cap.read()
print('frame =',frame)
print('ret = ',ret)
if ret==True:
frame =cv2.flip(frame,0)
#Write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(0) & 0xFF== ord('q'):
break
else:
break
print('after while loop')
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
The problem I am facing is that it is recording only black screen, although I have checked my webcam.
As I said in my comment, the solution is to change:
cv2.waitKey(0)
to:
cv2.waitKey(another_value)
by example 1.
According to the docs the parameter that receives cv2.waitKey() indicates the delay that is given:
In your case it is necessary since saving the image in the video, also is always convenient since an image is given at 60Hz so Overcoming that frequency is unnecessary.
I'm new in Python (2.7) and I try to work on video processing (with module openCv "cv2"). Starting with tutorials, I try to use the script of this tutorial : paragraph "Saving a video".
Everything works fine excepting that the video I'm saving is empty. I can find output.avi in my directory but its memory size is 0kb an, of course when I run it, no video is displayed.
After a few changes here is my code :
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Does anyone know why it is not working properly ?
Thanks a lot.
Edwin
I never worked with openCV, but I bet the problem is in
cap = cv2.VideoCapture(0)
This is a C version of the VideoCapture method http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture
Maybe you can try to do the same. Something like
cap = cv2.VideoCapture(0)
if (not cap.isOpened()):
print "Error"
EDIT: just downloaded Python and OpenCV and discovered the problem was the codec. Try to change
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
for
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
and select the codec by hand.
Could be the output resolution is different from the input. Check width and height of cap.
size = (int(cap.get(3)), int(cap.get(4)))
Change either your camera or the output resolution.
I have Windows 10, Python3.7.6, OpenCV 4.2.0. In my case, the problem is video encoder. Both "XVID" and "X264" result in an empty output video. I changed the encoder to "DIVX" and the video is generated successfully.