关键帧有 I 帧、P 帧、B 帧 等等

一共有这么多类型的帧

cdef enum AVPictureType:
    AV_PICTURE_TYPE_NONE
    AV_PICTURE_TYPE_I
    AV_PICTURE_TYPE_P
    AV_PICTURE_TYPE_B
    AV_PICTURE_TYPE_S
    AV_PICTURE_TYPE_SI
    AV_PICTURE_TYPE_SP
    AV_PICTURE_TYPE_BI

我们关注的是 I 帧,因为 I 帧少,P 帧和 B 帧太太多了

使用 pyav 获取 I 帧,可以用下面的代码:

样例代码:

import av
import av.datasets


content = av.datasets.curated("pexels/time-lapse-video-of-night-sky-857195.mp4")
with av.open(content) as container:
    # Signal that we only want to look at keyframes.
    stream = container.streams.video[0]
    stream.codec_context.skip_frame = "NONKEY"

    for frame in container.decode(stream):

        print(frame)

        # We use `frame.pts` as `frame.index` won't make must sense with the `skip_frame`.
        frame.to_image().save(
            "night-sky.{:04d}.jpg".format(frame.pts),
            quality=80,
        )

视频数据处理方法!关于开源软件FFmpeg视频抽帧的学习


universe_king
3.4k 声望680 粉丝