提取关键帧| Python | Opencv的 [英] Extracting keyframes | Python | Opencv
本文介绍了提取关键帧| Python | Opencv的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在从视频中提取关键帧.
I am currently working on keyframe extraction from videos.
代码:
while success:
success, currentFrame = vidcap.read()
isDuplicate = False
limit = count if count <= 10 else (count - 10)
for img in xrange(limit, count):
previusFrame = cv2.imread("%sframe-%d.png" % (outputDir, img))
try:
difference = cv2.subtract(currentFrame, previusFrame)
except:
pass
这给了我很多相框. 预期输出:计算帧之间的像素差异,然后将其与阈值进行比较并存储唯一的关键帧.
This gives me huge amounts of frames. Expected ouput: Calculate pixel difference between frames and then compare it with a threshold value and store unique keyframes.
第一次处理视频.请指导如何实现预期的产量
Working on videos for the first time. please guide on how to proceed to achieve the expected output
推荐答案
以下是使用ffprobe和OpenCV提取I帧的脚本:
Here is a script to extract I-frames with ffprobe and OpenCV:
import os
import cv2
import subprocess
filename = '/home/andriy/Downloads/video.mp4'
def get_frame_types(video_fn):
command = 'ffprobe -v error -show_entries frame=pict_type -of default=noprint_wrappers=1'.split()
out = subprocess.check_output(command + [video_fn]).decode()
frame_types = out.replace('pict_type=','').split()
return zip(range(len(frame_types)), frame_types)
def save_i_keyframes(video_fn):
frame_types = get_frame_types(video_fn)
i_frames = [x[0] for x in frame_types if x[1]=='I']
if i_frames:
basename = os.path.splitext(os.path.basename(video_fn))[0]
cap = cv2.VideoCapture(video_fn)
for frame_no in i_frames:
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
ret, frame = cap.read()
outname = basename+'_i_frame_'+str(frame_no)+'.jpg'
cv2.imwrite(outname, frame)
print ('Saved: '+outname)
cap.release()
else:
print ('No I-frames in '+video_fn)
if __name__ == '__main__':
save_i_keyframes(filename)
如果需要提取P帧,可以将'I'
更改为'P'
.
You can change 'I'
to 'P'
if you need to extract P-frames.
这篇关于提取关键帧| Python | Opencv的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文