【python】opencv教程CV2模块——视频捕获,延时摄影视频、鬼畜表情包密集制作

opencv教程CV2模块还可以调用摄像头录制视频呢,没想到吧!

制作延时摄影视频或者鬼畜表情包gif也是轻而易举的事情,等你发挥啦!

代码传送门:

import cv2
import time

interval = 0.5         # 捕获图像的间隔,单位:秒
num_frames = 30        # 捕获图像的总帧数
out_fps = 24            # 输出文件的帧率

# VideoCapture(0)表示打开默认的相机
cap = cv2.VideoCapture(0)

# 获取捕获的分辨率
size =(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
       int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
       
# 设置要保存视频的编码,分辨率和帧率
video = cv2.VideoWriter(
    "./outvideo/time_lapse.mp4", 
    cv2.VideoWriter_fourcc('M','P','4','2'), 
    out_fps, 
    size
)

# 对于一些低画质的摄像头,前面的帧可能不稳定,略过
for i in range(42):
    cap.read()

# 开始捕获,通过read()函数获取捕获的帧
try:
    for i in range(num_frames):
        _, frame = cap.read()
        video.write(frame)

        # 如果希望把每一帧也存成文件,比如制作GIF,则取消下面的注释
        # filename = '{:0>6d}.png'.format(i)
        # cv2.imwrite(filename, frame)

        print('Frame {} is captured.'.format(i))
        time.sleep(interval)
except KeyboardInterrupt:
    # 提前停止捕获
    print('Stopped! {}/{} frames captured!'.format(i, num_frames))

# 释放资源并写入视频文件
video.release()
cap.release()

效果视频就不放啦,需要的模块不多,动手试一试才能体会到,真是妙啊!

原文地址:https://www.cnblogs.com/helenlee01/p/12707296.html