imageio-ffmpeg icon indicating copy to clipboard operation
imageio-ffmpeg copied to clipboard

count_frames/count_frames_and_secs regression — very slow because ffmpeg is decoding frames (uses -vf null) instead of stream copy

Open Akascape opened this issue 1 month ago • 1 comments

Calling imageio-ffmpeg's frame counting is extremely slow (it decodes every frame) for large files. This looks like a regression vs older behavior where the ffmpeg call used stream copy and returned quickly.

Reproducible by calling imageio.get_reader(..., "ffmpeg").count_frames() (or count_frames_and_secs(path) directly).

import time, imageio
t0 = time.time()
vid = imageio.get_reader("video.mp4", "ffmpeg")
n = vid.count_frames()
print("frames:", n, "time:", time.time()-t0)

Observe this may take minutes for large files.

Expected behavior Count frames should be implemented via stream-copy / container parsing and be fast. In practice a command using "-c copy -f null -" is sufficient and much faster: ffmpeg will parse container and print progress without decoding the pixel data.

Fix: _io.py

  cmd = [
      get_ffmpeg_exe(),
      "-i",
      path,
      "-map",
      "0:v:0",
      "-c",
      "copy",
      "-f",
      "null",
      "-",
  ]

Akascape avatar Nov 06 '25 10:11 Akascape

get_reader is deprecated for abour 2 years now, and we have moved to pyav as the go-to video plugin. Does the same happen if you let ImageIO decide which plugin to use?

import imageio.v3 as iio

n_frames = iio.improps("video.mp4").n_images

FirefoxMetzger avatar Nov 06 '25 10:11 FirefoxMetzger