【问题标题】:Pygame - get specific frame from videoPygame - 从视频中获取特定帧
【发布时间】:2021-10-12 03:38:22
【问题描述】:

我正在尝试从使用 pygame 加载的视频中提取特定帧

在这个question中给出了这个例子

import pygame
import cv2

video = cv2.VideoCapture("video.mp4")
success, video_image = video.read()
fps = video.get(cv2.CAP_PROP_FPS)

window = pygame.display.set_mode(video_image.shape[1::-1])
clock = pygame.time.Clock()

run = success
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    success, video_image = video.read()
    if success:
        video_surf = pygame.image.frombuffer(
            video_image.tobytes(), video_image.shape[1::-1], "BGR")
    else:
        run = False
    window.blit(video_surf, (0, 0))
    pygame.display.flip()

pygame.quit()
exit()

但是,这只会遍历视频中的所有帧。 有没有办法在鼠标点击时获取当前帧?

当我尝试获取例如第 15 帧 video_image[15].tobytes() 我得到 TypeError: argument 2 must be sequence of length 2, not 1

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    有没有办法在鼠标点击时获取当前帧?

    MOUSEBUTTONDOWN 事件发生时设置save_frame 变量。使用pygame.image.save 将框架保存到文件中:

    farme_count = 0
    run = success
    while run:
        clock.tick(fps)
       
        save_frame = False
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                save_frame = True
        
        success, video_image = video.read()
        if success:
            video_surf = pygame.image.frombuffer(
                video_image.tobytes(), video_image.shape[1::-1], "BGR")
    
            if save_frame:
                pygame.image.save(video_surf, f"frame_{farme_count}.png")
    
        else:
            run = False
            
        window.blit(video_surf, (0, 0))
        pygame.display.flip()
        farme_count += 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2013-12-22
      • 1970-01-01
      相关资源
      最近更新 更多