如何使用OpenCV从视频中读取特定的框架?

发布于 2025-01-17 23:27:00 字数 73 浏览 3 评论 0原文

我想从视频中的特定帧读取到特定帧。例如,我的视频由 150 帧组成,但我想读取该视频中从第 5 帧到第 134 帧的视频。是否可以?

I want to read from a specific frame to a specific frame in a video. For example , my video consists of 150 frames, but I want to read the video from frame 5th to frame 134th in that video. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2025-01-24 23:27:01

首先,您必须读取视频并将其存储到多维 numpy 数组中。之后,您可以根据您的要求对 numpy 数组进行切片,

请参阅 https://stackoverflow.com/a/42166299/4141279如何将视频读入 numpy 数组。

然后,切片是通过以下方式完成的:

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

First you have to read your video and store it into a multi-dimensional numpy array. Afterwards you can slice the numpy array according to your requirements

See https://stackoverflow.com/a/42166299/4141279 on how to read a video into a numpy array.

Slicing is then done by:

buf[4:134] # assuming buf is your numpy array from the previous step

You could also drop all the unnecessary frames during initial creation of the numpy array if you have memory limitations.

idx = 0
while (fc < frameCount  and ret):
    ret, tmp = cap.read()
    if fc in range(4, 135):
        buf[idx] = tmp
        idx += 1
    fc += 1
蓝天白云 2025-01-24 23:27:01

我会做这样的事情:

这可能使用的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 : 关于堆栈溢出的另一个问题

I would do something like this :

This probably uses less opencv than you want, but I personally believe that ffmpeg is more adapted to this kind of operation

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")

source 1 : an openCV guide

source 2 : another question on stack overflow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文