opencv: 合成视频

Template

# 用list记录图片地址
src_paths = [os.path.join(SRC_FOLDER, path) for path in os.listdir(SRC_FOLDER) if path.endswith(".png")]

# 一定要记得 sort() !
src_paths.sort()

# 配置cv2.VideoWriter参数
sample_img = cv2.imread(src_paths[0])
h, w, _ = sample_img.shape
out = cv2.VideoWriter(DST_FOLDER+video_name, -1, fps, (w, h))

# 循环write
pbar = tqdm(src_paths)
for i, src_path in enumerate(pbar):
    frame = cv2.imread(src_path)
    out.write(frame)
    # 实时显示处理过的帧图片
    cv2.imshow('frame', output)
    cv2.waitKey(1)
    pbar.set_description("  COMPLETE ")

# 释放cv2.VideoWriter
out.release()
# 关闭帧图片展示窗口
cv2.destroyAllWindows()

Test Code

import shutil
import cv2
from tqdm import tqdm


SRC_FOLDER = "./o_file/after_op_1"
DST_FOLDER = "./o_file/after_op_3"
ENDWITH = ".MOV"


def make_video(fps, video_name):

    src_paths = [os.path.join(SRC_FOLDER, path) for path in os.listdir(SRC_FOLDER) if path.endswith(".png")]
    src_paths.sort()

    sample_img = cv2.imread(src_paths[0])
    h, w, _ = sample_img.shape
    out = cv2.VideoWriter(DST_FOLDER+video_name, -1, fps, (w, h))

    pbar = tqdm(src_paths)
    for i, src_path in enumerate(pbar):
        frame = cv2.imread(src_path)
        # 实时显示处理过的帧图片
        cv2.imshow('frame', output)
        cv2.waitKey(1)
        pbar.set_description("  COMPLETE ")
    out.release()
    # 关闭帧图片展示窗口
    cv2.destroyAllWindows()



if __name__ == "__main__":

    try:
        shutil.rmtree(DST_FOLDER)
    except OSError:
        pass
    import os
    os.makedirs(DST_FOLDER)

    fps_lst = [30]
    fourcc_types = [-1]

    for fps in fps_lst:
        video_name = "/output_" + "_" + str(fps) + ENDWITH
        print("\n Make video {} in fps:{}".format(video_name, fps))
        make_video(fps, video_name)

[1] OpenCV: Getting Started with Videos

阅读更多
版权声明:转载请注明出处 https://blog.csdn.net/JNingWei/article/details/79953775
个人分类: OpenCV
所属专栏: OpenCV-Python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭