Creating an mp4 file from images using video.write in opencv

0

I'm trying to create and save a video file in python via opencv

video_name = os.path.join(DIR, "test.mp4")
images = [img for img in os.listdir(DIR) if img.startswith(f'{test_')]
images = sorted(images, key=graph_utils_py.numericalSort)
frame = cv2.imread(os.path.join(DIR, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name, 0, 1, (width, height))
for image in images:
    fpath = os.path.join(DIR, image)
    video.write(cv2.imread(fpath))
    os.remove(fpath)  # remove the image file
cv2.destroyAllWindows()
video.release()

The above generates the video file without any issues but I get a warning

OpenCV: FFMPEG: tag 0x00000000/'????' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
[mp4 @ 000001d0378cdec0] Could not find tag for codec rawvideo in stream #0, codec not currently supported in container

This creates an issue when I try to embed this video file in a pdf generated using latex. From what I understand [The video data inside the file must be encoded with the H.264 codec.][1] to successfully view the embedded video in pdf.

Any suggestion on how to generate the mp4 file with H.264 codec in python will be greatly appreciated.

EDIT:

I tried what has been suggested below and the following error occurs

OpenCV: FFMPEG: tag 0x31435641/'AVC1' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1'

Failed to load OpenH264 library: openh264-1.8.0-win64.dll
    Please check environment and/or download library: https://github.com/cisco/openh264/releases

[libopenh264 @ 000002833670e500] Incorrect library version loaded
Could not open codec 'libopenh264': Unspecified error
python-3.x
image
opencv
video
pdflatex
asked on Stack Overflow Nov 10, 2020 by Natasha • edited Nov 10, 2020 by Natasha

1 Answer

0

You need to pass correct codec:

fourcc = cv2.VideoWriter_fourcc(*'AVC1')
fps = 1
video = cv2.VideoWriter(video_name, fourcc, fps, (width, height))
answered on Stack Overflow Nov 10, 2020 by Gralex

User contributions licensed under CC BY-SA 3.0