可以按帧提取视频帧,或者可以跳过并读取帧。
cv2是OpenCV,安装命令如下。
conda install opencv
# or
pip install opencv
必须在读取期间指定文件名。 用以下命令输入:
. zfill(num ) ) num是指充填会的位数
导入cv2
import os
#要提取视频的文件名,请隐藏后缀
source filename=' fog 2020031300026 '
#在此处添加后缀
video _ path=OS.path.join (source filename '.MP4 ' ) ) )。
times=0
#每375帧提取一个视频频率
frameFrequency=375
#将图像输出到当前目录的vedio文件夹下
output dirname=' vedio/' source filename '/'
ifno tos.path.exists (output dirname ) :
#如果文件目录不存在,请创建目录
OS.makedirs(outputdirname )。
camera=cv2.video capture (video _ path ) )。
counter=0
while True:
#读取指定数量的帧
if counter=1920:
times =1
res,image=camera.read () ) ) ) ) )。
if not res:
打印(' notres,not image ' ) )。
布莱克
if times % frameFrequency==0:
counter =1
cv2.im write (output dirname str (counter ).zfill(4)4) '.jpg ',image ) ) ) ) ) )。
打印(output dirname str (counter ) ).jpg ) )。
else:
布莱克
打印(“图像提取结束”)
camera.release (