这个想法是,用户应该能够从本地计算机加载视频并告诉程序每 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/