Writing an mp4 video using python opencv

前端 未结 10 708
Happy的楠姐
Happy的楠姐 2021-01-31 07:44

I want to capture video from a webcam and save it to an mp4 file using opencv. I found example code on stackoverflow (below) that works great. The only hitch is that I\'m trying

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 08:28

    This worked for me, I added images.sort() to keep the sequence order:

    import cv2
    import numpy as np
    import os
    
    image_folder = 'data-set-race-01'
    video_file = 'race-01.mp4'
    image_size = (160, 120)
    fps = 24
    
    images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
    images.sort()
    
    out = cv2.VideoWriter(video_file, cv2.VideoWriter_fourcc(*'MP4V'), fps, image_size)
    
    img_array = []
    for filename in images:
        img = cv2.imread(os.path.join(image_folder, filename))
        img_array.append(img)
        out.write(img)
    
    out.release()
    

提交回复
热议问题