Python:从视频中截取屏幕截图

标签 python video screenshot video-processing

这个想法是,用户应该能够从本地计算机加载视频并告诉程序每 5 秒或 30 秒从视频中截取一次屏幕截图。有没有图书馆可以帮助我完成这项任务?任何有关如何进行的想法都会有所帮助。

最佳答案

通过发出以下命令安装 opencv-python (这是一个非官方的 Python 预构建 OpenCV 包):

pip install opencv-python
# Importing all necessary libraries
import cv2
import os
import time

# Read the video from specified path
cam = cv2.VideoCapture("C:/Users/User/Desktop/videoplayback.mp4")

try:

    # creating a folder named data
    if not os.path.exists('data'):
        os.makedirs('data')

    # if not created then raise error
except OSError:
    print('Error: Creating directory of data')

# frame
currentframe = 0

while (True):
    time.sleep(5) # take schreenshot every 5 seconds
    # reading from frame
    ret, frame = cam.read()

    if ret:
        # if video is still left continue creating images
        name = './data/frame' + str(currentframe) + '.jpg'
        print('Creating...' + name)

        # writing the extracted images
        cv2.imwrite(name, frame)

        # increasing counter so that it will
        # show how many frames are created
        currentframe += 1
    else:
        break

# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()

关于Python:从视频中截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57791203/

相关文章:

python - 如何制作随机唯一元组列表?

css - 视频背景创建水平滚动

c - 仅使用 X11 lib 在 C 中截取窗口的屏幕截图

android - 当相机在 Android 中的表面 View 上实时显示时以编程方式截屏

delphi - 截取特定区域的屏幕截图

python - 检查列表中是否存在子列表的最有效方法,忽略最后一个元素

python - 有没有办法在断点处获取 python 调试 session 的 'snapshot'?

python - 我在构建剪刀石头布游戏时遇到问题(3 个版本中的最佳版本)

amazon-web-services - AWS CloudFront 与 Youtube - 在网页上加载视频的更快方法是什么?

mongodb - 如何使用 MongoDB 或其他文档数据库保存视频文件,可选择添加到现有二进制文件和并行读/写