如何使用 pyav 抽取 I-frame (關鍵幀)

ponponon發表於2023-02-13

關鍵幀有 I 幀、P 幀、B 幀

我們關注的是 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影片抽幀的學習

相關文章