I'm new to opencv and I try to generate a video file from a set a jpg pictures.
This works fine if I use ffmpeg : ffmpeg -y -r 60 -i capture.%d.jpg -c:v libx264 ffmpeg_test.mkv
With my 120 pictures, this generates a 2s video.
I'd like to do the same with opencv.
Here is the code I use:
import cv2
def record(writer):
for i in range(121):
img = cv2.imread(f"capture.{i}.jpg")
writer.write(img)
fourcc = cv2.VideoWriter.fourcc(*'x264')
writer = cv2.VideoWriter("py_test.mkv", fourcc, 60.0, [120, 160])
record(writer)
writer.release()
I've got no error on execution, the file seems a regular video file, checked with mediainfo, but vlc raise an error (in verbose mode) "mkv error: cannot find any cluster or chapter". The file is 553 bytes.
Ubuntu 22.04 python3-opencv (package from apt) python 3.10.12 (cv2.version = 4.5.4)
I tried changing format (MPEG, mpgv, XVID, mp4v, ...) and container (avi, mp4), the result is always the same (file ~500 bytes). I concluded that my images are never integrated to the video, but I don't know why.
It was in "with shape (120, 160, 3)". while images are 120 height x 160 width, cv2.VideoWriter wants (width, height).
Clue was in Can't play video created with OpenCV VideoWriter
After correction, I tried with codec MPEG, mpgv, XVID, mp4v, H264, x264, DIVX, AVI1, RGBA, FLV1, with container mkv. Also with codec XVID, it works with containers mp4 and avi.
I did not try all combinations, but if there is a problem, it is not related to mine.
import cv2
def record(writer):
for i in range(121):
img = cv2.imread(f"capture.{i}.jpg")
writer.write(img)
writer.release()
fourcc = cv2.VideoWriter.fourcc(*'x264')
writer = cv2.VideoWriter("py_test.mkv", fourcc, 60.0, [160, 120, 3])
record(writer)
Thanks anyone for your time.