如何以特定 FPS 从视频中提取帧? [英] How can I extract frames from a video at a certain FPS?

查看:96
本文介绍了如何以特定 FPS 从视频中提取帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用以下代码提取某个 test.mp4 文件的帧:

I am able to extract the frames of a certain test.mp4 file using the following code:

import cv2
def get_frames():
    cap = cv2.VideoCapture('test.mp4')
    i = 0
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        cv2.imwrite('test_'+str(i)+'.jpg', frame)
        i += 1

    cap.release()
    cv2.destroyAllWindows()

提取的很多帧都是无用的(它们几乎相同).我需要能够设置可以完成帧提取的特定速率.

A lot of the frames that are extracted are useless (they're nearly identical). I need to be able to set a certain rate at which the frame extraction can be done.

推荐答案

我认为您只需要根据固定周期跳过帧即可.

I think you need to just skip frames based on a fixed cycle.

import cv2


def get_frames():
    cap = cv2.VideoCapture('test.mp4')
    i = 0
    # a variable to set how many frames you want to skip
    frame_skip = 10
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        if i > frame_skip - 1:
            cv2.imwrite('test_'+str(i)+'.jpg', frame)
            i = 0
            continue
        i += 1

    cap.release()
    cv2.destroyAllWindows()

这篇关于如何以特定 FPS 从视频中提取帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆