社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何使用opencv和python录制和保存视频?

Anubhav Dinkar • 7 年前 • 2467 次点击  

我从 this website .

import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'avc1')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        out.write(frame)
        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

我面临的问题是视频正在被存储,但我无法打开它。视频大小约为6KB,但持续时间为0秒。我该怎么解决?

我检查了其他类似的问题,但没有一个能解决我面临的问题。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44104
文章 [ 1 ]  |  最新文章 7 年前
furas
Reply   •   1 楼
furas    7 年前

如果保存的帧大小不正确,则打开文件时遇到问题。

如果相机提供的帧大小为IE。 (800, 600) 那你就得用同样大小的字 (800, 600) 或者您必须使用cv来调整框架的大小 (640, 480) 在你保存它之前。

    frame = cv2.resize(frame, (640, 480))

全码

import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'avc1') #(*'MP42')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

while cap.isOpened():
    ret, frame = cap.read()
    if ret:

        frame = cv2.resize(frame, (640, 480))

        out.write(frame)
        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

GitHub上的示例: furas/python-examples/cv2/record-file