# coding: utf-8
import cv2 as cv
import os
print('===============================================================')
print('本工具有4个功能,键入不同数字,执行不同功能:1,2,3,4')
print('如果输入有误,程序报错或执行结果(无结果)')
print('\n')
print('输入数字1:截取视频全部帧')
print('输入数字2:自定义区间取帧')
print('输入数字3:自定义间隔数取帧')
print('输入数字4:自定义取某一帧')
print('\n')
print('===============================================================')
all = eval(input('请键入1-4中的对应数字数字( 选择相应的功能 ):'))
if (all ==1):
filepath = './input'
pathDir = os.listdir(filepath)
for allDir in pathDir:
Frame = 0
c = 1
videopath = r'./input/'+allDir
vc = cv.VideoCapture(videopath)
path = './Video_Result(All)Frame/'
if not os.path.exists(path):
os.mkdir(path)
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
timeF = 1
while rval:
rval,frame = vc.read()
if rval==False:
break
if(c%timeF==0):
#frame = frame[0:1070,200:1840]
cv.imencode('.jpg',frame)[1].tofile(path +'/' + str(Frame) +'-'+ allDir[:-4] + '.jpg')
print('正在截取第' + str(Frame) + "帧,请稍后……")
Frame += 1
print('\n','处理完成,请移步------→ :Video_Result(All)Frame查看结果')
if (all ==2):
# coding: utf-8
filepath = './input'
pathDir = os.listdir(filepath)
print('\n')
a = eval(input('请输入待截取的开始帧:'))
b = eval(input('请输入待截取的结束帧:'))
for allDir in pathDir:
Frame = 0
c = 1
videopath = r'./input/'+allDir
vc = cv.VideoCapture(videopath)
path = './Video_Result(自定义区间)Frame/'
if not os.path.exists(path):
os.mkdir(path)
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
timeF = 1
while rval:
rval,frame = vc.read()
if rval==False:
break
if(c%timeF==0):
#frame = frame[0:1070,200:1840]
if (Frame >= a and Frame <=b ): # 50--80帧的数据
cv.imencode('.jpg',frame)[1].tofile(path +'/' + str(Frame) +'-'+ allDir[:-4] + '.jpg')
print('正在截取第' + str(Frame) + "帧,请稍后……")
Frame += 1
print('\n','处理完成,请移步------→ :Video_Result(自定义区间)Frame查看结果')
# coding: utf-8
if (all ==3):
filepath = './input'
pathDir = os.listdir(filepath)
print('\n')
interval = eval(input('请输入需要间隔帧数:'))
for allDir in pathDir:
a = 1
c = 1
videopath = r'./input/'+allDir
vc = cv.VideoCapture(videopath)
path = './Video_Result(Interval)Frame/'
if not os.path.exists(path):
os.mkdir(path)
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
timeF = interval
while rval:
rval,frame = vc.read()
if rval==False:
break
if(c%timeF==0):
cv.imencode('.jpg',frame)[1].tofile(path +'/' + str(a) +'-'+ allDir[:-4] + '.jpg')
print('正在截取第' + str(a) + "帧,请稍后……")
a += 1
c += 1
cv.waitKey(1)
vc.release()
print('\n','处理完成,请移步------→ :Video_Result(Interval)Frame查看结果')
if (all ==4):
# coding: utf-8
filepath = './input'
pathDir = os.listdir(filepath)
print('\n')
only_one = eval(input('请输入需要截取的特定一帧:'))
for allDir in pathDir:
a = only_one
videopath = r'./input/'+allDir
vc = cv.VideoCapture(videopath)
path = './Video_Result(Only-1)Frame/'
if not os.path.exists(path):
os.mkdir(path)
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
while rval:
vc.set(cv.CAP_PROP_POS_FRAMES, a)
rval,frame = vc.read()
cv.imencode('.jpg',frame)[1].tofile(path +'/' + str(a) +'-'+ allDir[:-4] + '.jpg')
print(str(a) +'-'+ allDir[:-4] + '---视频截取完成')
break
print('\n','处理完成,请移步------→ :Video_Result(Only-1)Frame查看结果')