python - 使用 Python 获取视频中的 I 帧列表

标签 python python-3.x video ffmpeg ffprobe

我正在尝试使用 Python 获取视频中所有 I 帧的索引列表(稍后将其中一部分保存为 JPEG)。现在,我可以在终端中使用 FFProbe 遍历所有帧,并查看哪一个是 I 帧:

ffprobe -select_streams v -show_frames -show_entries frame=pict_type -of csv 18s.mp4

这给了我这样的东西:

frame,I
frame,B
frame,P
frame,B
frame,P
frame,B

但是我如何在 Python 中做到这一点(我在 Windows 中)并获取所有索引的列表?

最佳答案

here 获取见解,我能够使用 ffprobe 做到这一点:

def iframes():
    if not os.path.exists(iframe_path):
        os.mkdir(iframe_path)
    command = 'ffprobe -v error -show_entries frame=pict_type -of default=noprint_wrappers=1'.split()
    out = subprocess.check_output(command + [filename]).decode()
    f_types = out.replace('pict_type=','').split()
    frame_types = zip(range(len(f_types)), f_types)
    i_frames = [x[0] for x in frame_types if x[1]=='I']
    if i_frames:
        cap = cv2.VideoCapture(filename)
        for frame_no in i_frames:
            cap.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
            ret, frame = cap.read()
            outname = iframe_path+'i_frame_'+str(frame_no)+'.jpg'
            cv2.imwrite(outname, frame)
        cap.release()
        print("I-Frame selection Done!!")


if __name__ == '__main__':
    iframes()

关于python - 使用 Python 获取视频中的 I 帧列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51661864/

相关文章:

python - 检查非字符串的文件扩展名

video - 并排放置 FFMPEG 视频,按帧编号对齐

python - 在 Pandas DataFrame 中分隔值高于特定阈值的连续区域

python - 如何在 python 瓶中提供具有希伯来文名称的静态文件?

python-3.x - 名称 'conn' 未定义 : NameError

javascript - 为视频添加静音按钮

asp.net - 从ffmpeg保存后打开jpg图像不起作用

javascript - 字符转义: from Python string literal to JSON and then to HTML

python - 使用 selenium 和 python 检查是否存在任何警报

python - Pandas 计算字符出现次数