I am using cv2.VideoWriter() to record from a single camera and writing the video to a .mp4 file with a HEVC codec. I had this script previously working on a (Windows) laptop with the following code:
video = cv2.VideoCapture(0)
frame_width = int(video.get(3))
frame_height = int(video.get(4))
# create output file
out1 = cv2.VideoWriter(videoFileName, cv2.VideoWriter_fourcc(str('H'), str('E'), str('V'), str('C')), 30.0,
(frame_width, frame_height))
I am setting up the script on a new laptop and the above code no longer records and throws the following error:
OpenCV: FFMPEG: tag 0x43564548/'HEVC' is not supported with codec id 173 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31766568/'hev1'
[hevc_mf # 0000028eed319d00] COM must not be in STA mode
[ERROR:0] global /build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp (2794) open Could not open codec hevc_mf, error: Unspecified error
[ERROR:0] global /build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp (2811) open VIDEOIO/FFMPEG: Failed to initialize VideoWriter
I have read through many forums who suggest that this is not possible with cv2 because it does not support the HEVC codec. I have also read the .mp4 and HEVC are possibly not compatible. However, it was previously functioning on the other laptop. Therefore, I know it works in some manner. Note: I did not set it up initially on the other laptop.
I have also tried
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# fourcc = cv2.VideoWriter_fourcc(*'h264')
# out1 = cv2.VideoWriter(videoFileName, fourcc, 30.0, (frame_width, frame_height))
with mp4v working but not the codec I initially had and h264 throwing an error similar to above.
Has anyone encountered a solution to this problem?
Additional info:
Windows 10, Python 3.8.10 through Pycharm, cv2 installed via opencv-python and imported with ´from cv2 import cv2`
Related
This is my code for recording videos on Raspberry Pi 4 running Raspbian Buster:
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
writer = cv2.VideoWriter(tempVideo.path, fourcc, framerate, resolution, True)
writer.write(frame)
However, no matter what codec I try, I keep getting errors like:
OpenCV: FFMPEG: tag 0x47504a4d/'MJPG' is not supported with codec id 7 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'
Setting fourcc = 1 also did not help. Here is what I see:
OpenCV: FFMPEG: tag 0xffffffff/'????' is not found (format 'mp4 / MP4 (MPEG-4 Part 14)'
Is there a way to list all the supported codecs and their tags?
Here is the link with all fourcc codecs.
http://www.fourcc.org/codecs.php
Some codecs don't exist there, but in your case, it is not an error it is just a fallback, so your code is running anyway and in the output you will get your mp4 file.
If you don't like how it looks just use this
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
or
fourcc = 0x00000021
It's not an exact answer to the question, but what eventually helped me is cv2.getBuildInformation() is an answer. It shows what video formats are supported by particular build of opencv.
I'm trying to create a video using opencv-python. I have to use H.264 codec for my video so I have the following lines:
import cv2
width = ...
height = ...
name = ...
fourcc = cv2.VideoWriter_fourcc(*'H264')
self.video = cv2.VideoWriter(name, fourcc, 30, (width, height))
When I run the code I get the following error:
[h264_nvenc # 0x562490d62d80] Cannot load libcuda.so.1
Could not open codec 'h264_nvenc': Unspecified error
As I see in the error, I need libcuda.so.1 library which comes with nvidia driver afaik, but my laptop has no dedicated graphic card.
Is it possible to write a video using H.264 using my laptop? If yes, please advice how. Thank you!
I'm trying to create a video from a sequence of images and display it in a browser but from some weird reason no matter what codec or file format I use I get the following error:
No video with supported format and mime type found
Here is my code:
ready_images = []
import cv2
for img in videos['Images']:
image = cv2.imread(img.fileName)
ready_images.append(image)
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
video_name = videos['Images'][0].gifLocationPath + "//" + videos['Name']
frame = cv2.imread(videos['Images'][0].fileName)
height, width, layers = frame.shape
video_name = video_name[:-4]+".mp4"
video = cv2.VideoWriter(video_name, fourcc, 20.0, (width, height))
for image in ready_images:
video.write(image)
cv2.destroyAllWindows()
video.release()
The funny thing is that in Firefox or Chrome the videos are not working but in Edge... they actually work.
I don't want to use FFMPEG and would prefer to make it work with OpenCV.
If any of you guys know what format of video (I know the web formats are webm, ogg, mp4) or codec I should use for this, please, just let me know.
Thanks.
MP4V or MPEG-4 part 2 is not supported by most browsers, you may want to try H.264 (MPEG-4 part 10) instead.
To do that, change:
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
to
fourcc = cv2.VideoWriter_fourcc(*'H264')
If you are using Python 3, use the following hexadecimal code instead (there seems to be a bug when using the four bytes notation):
fourcc = 0x00000021
Run the script and you will likely get the following error message:
Failed to load OpenH264 library: openh264-1.6.0-win32msvc.dll
Please check environment and/or download library: https://github.com/cisco/openh264/releases
You need to do as the message says and download the required library from github and place it somewhere accessible by your PATH.
Using H.264 compression you will also get a smaller file which is better for Web.
I know the question is old but for everyone that is looking for a compatible codec + container for web browser :
VP8 or VP80 is a compatible encoder
cv2.VideoWriter_fourcc('V','P','8','0')
I used it with .webM as a container.
Native WebM support by Mozilla Firefox,[7][8] Opera,[9][10] and Google Chrome[11] was announced at the 2010 Google I/O conference
https://en.wikipedia.org/wiki/WebM
it worked like a charm and with pretty good performance even though
for some reason I got this error when creating videoWriter objects :
OpenCV: FFMPEG: tag 0x30385056/'VP80' is not supported with codec id 139 and format 'webm / WebM'
Get .webm suffix audio file.
fourcc = cv2.VideoWriter_fourcc(*'vp80')
video_writer = cv2.VideoWriter('file.webm', fourcc, 20, (640, 480))
In html:
<body>
<video width="320" height="240" controls>
<source src="file.webm" type="video/webm">
</video>
</body>
It works on centos7 and Windows10.
I print the all available mp4 codecs by fourcc=-1.
After that I check codecs which are useful for me. I see there avc1.
So I write the code like:
fourcc = cv2.VideoWriter_fourcc(*'avc1')
When print the codes, you also see they are lowercase.
On OSX I can record from my webcam and write a video file with the following simple script:
import cv2
camera = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter('output.avi', fourcc, 25.0, (640, 480))
while True:
try:
(grabbed, frame) = camera.read() # grab the current frame
frame = cv2.resize(frame, (640, 480)) # resize the frame
video_writer.write(frame) # Write the video to the file system
except KeyboardInterrupt:
camera.release()
break
The resulting avi file is quite big though. I want a smaller file, preferably an mp4. So I changed the filename to output.mp4 and the fourcc codec to H264. That writes a video file which works, but gives me the following error:
$ python write_video_file.py
OpenCV: FFMPEG: tag 0x34363248/'H264' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000021/'!???'
Since I thought I'm missing the H264 codec in ffmpeg I decided to uninstall ffmpeg and opencv and reinstall them again with H264 support. For this I used the following commands:
# First ffmpeg
brew install ffmpeg --with-fdk-aac --with-libvidstab --with-openh264 \
--with-openjpeg --with-openssl --with-tools --with-webp --with-x265 --with-zeromq
# then opencv3
brew tap homebrew/science
brew install opencv3 --with-contrib --with-ffmpeg --with-tbb
After this I ran the script again, using the following combinations:
output.mp4 with H264
output.mp4 with X264
Unfortunately I still get the OpenCV warnings/errors. The file is readable, but it still annoys me that I get these errors. Does anybody have any idea how I can make OpenCV write mp4 video file with the H264 codec?
All tips are welcome!
I spent ages trying to find a list of video codecs on macOS, and finding which codecs work with which containers, and then if QuickTime can actually read the resulting files.
I can summarise my findings as follows:
.mov container and fourcc('m','p','4','v') work and QuickTime can read it
.mov container and fourcc('a','v','c','1') work and QuickTime can read it
.avi container and fourcc('F','M','P','4') works but QuickTime cannot read it without conversion
I did manage to write h264 video in an mp4 container, as you wanted, just not using OpenCV's VideoWriter module. Instead I changed the code (mine happens to be C++) to just output raw bgr24 format data - which is how OpenCV likes to store pixels anyway:
So the output of a frame of video stored in a Mat called Frame becomes:
cout.write(reinterpret_cast<const char *>(Frame.data),width*height*3);
and then you pipe the output of your program into ffmpeg like this:
./yourProgram | ffmpeg -y -f rawvideo -pixel_format bgr24 -video_size 1024x768 -i - -c:v h264 -pix_fmt yuv420p video.mp4
Yes, I know I have made some assumptions:
that the data are CV8_UC3,
that the sizes match in OpenCV and ffmpeg, and
that there is no padding or mad stride in the OpenCV data,
but the basic technique works and you can adapt it for other sizes and situations.
I am using mac os x 1.7.5 with python 2.7.5_1 and opencv 2.4.4_0 installed via macports. I seem to have all the latest dependent ports.
In my code, the cv2.Videowriter() is successfully created and opened which produces a 6kb .avi file but videoFile.write(img0) doesn't write anything into that file. I really am not able to figure out why the video stream isn't written to the file. Any insights?
My code is as follows:
import cv2
import cv
cv2.namedWindow("Original")
cap0 = cv2.VideoCapture(0)
codec = cv.CV_FOURCC('D','I','V','X')
print codec
videoFile = cv2.VideoWriter();
videoFile.open('video.avi', codec, 25, (640, 480),1)
key = -1
while(key < 0):
success0, img0 = cap0.read()
cv2.imshow("Original", img0)
videoFile.write(img0)
key = cv2.waitKey(1)
cv2.destroyAllWindows()
I've tried these codecs and none of them worked: I420, AVC1, YUV1, PIM1, MJPG, MP42, MP4V, DIV3, DIVX, XVID, IUYV,FFV1, FLV1, U263, H264, ZLIB
I've also gone through all the quick time codecs mentioned here
Using ZLIB codec I get the error:
[zlib # 0x7fb0d130a000] Specified pixel format yuv420p is invalid or not supported
Using H264 codec I get an error:
[libx264 # 0x7fe423869600] broken ffmpeg default settings detected
[libx264 # 0x7fe423869600] use an encoding preset (e.g. -vpre medium)
[libx264 # 0x7fe423869600] preset usage: -vpre <speed> -vpre <profile>
[libx264 # 0x7fe423869600] speed presets are listed in x264 --help
[libx264 # 0x7fe423869600] profile is optional; x264 defaults to high
I didn't understand what the above errors meant. I tried reinstalling ffmpeg to the latest version (1.2.2_0+gpl2) but my script still doesn't work. All the other codecs did not give any error.
I've even tried the file extensions of .mpg and .mkv with the above codecs. Sometimes I would get an error saying that the codec was not suitable for the file extension but when I didn't get error I would simply get the unreadable video file of a minuscule size.
Any help is much appreciated.
ps: I've already gone through the following SO questions which didn't solve my issue:
using opencv2 write streaming video in python
Writing video with OpenCV + Python + Mac
Create an avi video with opencv and python on a mac
Python OpenCV, can't write a video (.avi) to file
Creating AVI files in OpenCV
read/write avi video on MAC using openCV
Python OpenCV 2.4 writes half-complete PNG video frames
The problem seems to have been with the image size in the function videoFile.open('video.avi', codec, 25, (640, 480),1)
So I updated my script to include
size = (int(cap0.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cap0.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
And then accordingly changed videoFile.open to
videoFile.open('video.avi', codec, 25, size,1)
Then my script started working.
I've tried the codecs with fourcc: IYUV, I420, PIM1, MJPG, FFV1 and DIVX with file extension .avi
Each of the above codec worked with frame rates 16, 20, 25 and 30 except for PIM1 which seems to work only on 20fps and above.
Also,
Codec with fourcc THEO worked with file extension .ogv
XVID worked properly with file extension .mkv and though the .mkv container should work with any encoding I got a variety of weird results with other codecs.
FLV1 with file extension .flv didn't work. It gave the error:
[flv # 0x7f8414006000] Tag FLV1/0x31564c46 incompatible with output codec id '22' ([2][0][0][0])
FLV4 with file extension .flv didn't give an error but opencv's videowrite outputted an error "Could not update video file"
Of the codecs that worked with .avi file container, DIVX produced the smallest video file (~4Mb for 4 sec video) and IYUV produced the largest file (~160Mb for 4 sec video)
Note:
fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS) always returns 0.0 when capturing from webcam. It is a bug in OpenCV2.4.3 and OpenCV2.4.4
I also found out that ffmpeg cannot grab images from Mac's iSight and FacetimeHD webcams the way it can from Windows and Ubuntu because designers at Apple have prohibited easy access to the mac's camera... what a shame!
References:
http://en.wikipedia.org/wiki/Comparison_of_container_formats
Initially I was getting a 0kb video file. I changed the Codec from MJPG to iYUV. It worked for me. Python 2.7 and openCV 2.4.5.
cap = cv2.VideoCapture(0)
fourCC = cv2.cv.CV_FOURCC('i','Y','U', 'V'); # Important to notice cv2.cv.CV_FOURCC
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
out = cv2.VideoWriter("Test.avi", fourCC, 15.0, size, True)
To get h264 working, install ffmpeg from the following link. Its quite a cumbersome installation but it ll get the encoder up and running.
https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu