python - 逐帧存储视频以进行视频编辑的最佳方法是什么?
问题描述
我正在尝试创建一个视频编辑器,显然,您将能够删除和添加帧。我的想法是将视频文件本身转换为一组帧,然后可以对其进行操作。
使用这个答案代码,我做到了。这适用于小视频文件,但对于大视频文件,内存错误可能很快发生 - 因为,当然,内存正在存储数百个未压缩的图像。
这是我正在使用的确切代码:
import numpy
import cv2
def video_to_frames(file):
"""Splits a video file into a numpy array of frames"""
video = cv2.VideoCapture(file)
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
buffer = numpy.empty((frame_count, frame_height, frame_width, 3), numpy.dtype("uint8"))
index_count = 0
running = True
while(index_count < frame_count and running): #Reads each frame to the array
running, buffer[index_count] = video.read()
index_count += 1
video.release()
return buffer #Returns the numpy array of frames
print(video_to_frames("Video.mp4"))
最后,这是我得到的确切内存错误:MemoryError: Unable to allocate 249. GiB for an array with shape (46491, 1000, 1920, 3) and data type uint8
所以我真的有两个问题:
- 这是处理视频的最有效方法吗?
- 如果是,我怎样才能在不发生内存错误的情况下存储所有这些帧?
谢谢你。
解决方案
推荐阅读
- python - TypeError:“模块”对象不可调用:日历模块
- php - Symfony 4,在匿名用户和会话因不活动而被终止的用户之间进行识别
- protocols - MODBUS协议线圈订购
- java - 在 JavaFX 控制器之间传递值
- java - 如何中途停止异步任务
- java - Deeplearning4j - 在学习 WordVectors 时不能使用 UIServer?
- sql - 为所有组合选择最大日期
- apache-kafka - 如果您使用 Apache Kafka 作为严格的消息代理,您是否可以将所有保留条目设置为最低限度?
- c# - 在 C# 中使用 Unicode 值
- html - CSS 侧边菜单内容分层在下面