前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opencv: 合成视频

opencv: 合成视频

作者头像
JNingWei
发布2018-09-27 14:46:40
1.1K0
发布2018-09-27 14:46:40
举报
文章被收录于专栏:JNing的专栏JNing的专栏

Template

代码语言:javascript
复制
# 用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

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年04月15日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Template
  • Test Code
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档