Python读取视频,写视频

Fourcc:
https

1 读取视频:

# python read video
# python read video
import cv2
import numpy as np

cap = cv2.VideoCapture(0)  # 视频编解码器
id = 0
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
frame_h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_h)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_w)
print(f"fcrame_h: {frame_h}, frame_w: {frame_h}, frame_count: {frame_count}")
cv2.namedWindow("frame", cv2.WINDOW_NORMAL)
show_h, show_w = 320, 640
cv2.resizeWindow("frame", show_w, show_h)
while True:
    id += 1
    ret, frame = cap.read()
    if ret == False:
        print(f"video has breaken.")
    mask = frame[:, :, 0]
    # Get the timestamp of the current frame
    timestamp = cap.get(cv2.CAP_PROP_POS_MSEC)
    cv2.imshow("frame", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    if id == frame_count:
        print(f"finish.")
        break

2 保存视频

import cv2
import numpy as np


width, height = 1920,1080
# Define the codec and create a VideoWriter object
# *'MP4V', mp4
# *'XVID', avi
write_fps = 20.0
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('res_wan.mp4', fourcc, write_fps , (width, height))

# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# out = cv2.VideoWriter('res_wan.avi', fourcc, write_fps , (width, height))
test_num = 500
while test_num:
    test_num -= 1
    test_img = np.zeros((frame_h, frame_w, 3))
    out.write(test_img)
    ```
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值