我想在视频中从一个特定的帧读到一个特定的帧。例如,我的视频由150帧组成,但我想在视频中从第5帧读到第134帧。有可能吗?
发布于 2022-03-30 13:06:11
首先,您必须读取您的视频,并将其存储到一个多维数字数组中。之后,您可以根据需要对numpy数组进行切片。
有关如何将视频读入numpy数组,请参见https://stackoverflow.com/a/42166299/4141279。
然后通过以下方式进行切片:
buf[4:134] # assuming buf is your numpy array from the previous step
如果您有内存限制,也可以在初始创建numpy数组时删除所有不必要的框架。
idx = 0
while (fc < frameCount and ret):
ret, tmp = cap.read()
if fc in range(4, 135):
buf[idx] = tmp
idx += 1
fc += 1
发布于 2022-03-30 13:23:41
我会这样做:
这可能使用的opencv比您想要的少,但我个人认为ffmpeg更适合这种操作。
import cv2
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
def get_framerate(video)->float:
""" this function was made to extract the framerate from a video"""
# Find OpenCV version
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
if int(major_ver) < 3 :
return video.get(cv2.cv.CV_CAP_PROP_FPS)
else :
return = video.get(cv2.CAP_PROP_FPS)
if __name__ == "__main__":
# get start frame
videoName = input("Path of the video file ?")
video = cv2.VideoCapture(videoName);
fps = get_framerate(video)
frame_start = int(input("Starting frame ?\n"))
ts_start = frame_start / fps
frame_end = int(input("Ending frame ?\n"))
ts_end = frame_end / fps
ffmpeg_extract_subclip(frameName, ts_start, ts_end, targetname="output.mp4")
来源1:openCV指南
来源2:关于堆栈溢出的另一个问题
https://stackoverflow.com/questions/71677658
复制相似问题