Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macOS] RuntimeError: Could not get number of frames #54

Open
cuteguy opened this issue Feb 4, 2024 · 2 comments
Open

[macOS] RuntimeError: Could not get number of frames #54

cuteguy opened this issue Feb 4, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@cuteguy
Copy link

cuteguy commented Feb 4, 2024

Deface is quite handy for processing individual images, but it consistently encounters errors when I attempt to process MP4 files, and I'm uncertain of the underlying cause.

OS: macOS sonoma 14.3
Chip: Apple M1 Pro
Python Version: Python 3.11.7

Traceback (most recent call last):
  File "/opt/homebrew/bin/deface", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/deface/deface.py", line 398, in main
    video_detect(
  File "/opt/homebrew/lib/python3.11/site-packages/deface/deface.py", line 142, in video_detect
    nframes = reader.count_frames()
              ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/imageio/plugins/ffmpeg.py", line 385, in count_frames
    return cf(self._filename)[0]
           ^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/imageio_ffmpeg/_io.py", line 187, in count_frames_and_secs
    raise RuntimeError("Could not get number of frames")  # pragma: no cover
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Could not get number of frames
@mdraw
Copy link
Member

mdraw commented Feb 7, 2024

Thanks for reporting this issue. Seems to be related to imageio/imageio-ffmpeg#99. As a workaround you can just replace this line

nframes = reader.count_frames()

with

nframes = None

And everything should work except for the progress bar.
Alternatively try downgrading ffmpeg to an older version.

@mdraw mdraw added the bug Something isn't working label Feb 7, 2024
@Cogitarian
Copy link

Change in lib/python3.9/site-packages/imageio_ffmpeg/_io.py function for counting frames from ffmpeg to ffprobe, I've tested it on macos and it works:

def count_frames_and_secs(path):
"""
Get the number of frames and number of seconds for the given video
file. Note that this operation can be quite slow for large files.

Disclaimer: I've seen this produce different results from actually reading
the frames with older versions of ffmpeg (2.x). Therefore I cannot say
with 100% certainty that the returned values are always exact.
"""
# https://stackoverflow.com/questions/2017843/fetch-frame-count-with-ffmpeg

if isinstance(path, pathlib.PurePath):
    path = str(path)
if not isinstance(path, str):
    raise TypeError("Video path must be a string or pathlib.Path.")

cmd = ['ffprobe','-v','error','-select_streams','v:0','-count_packets','-show_entries','stream=nb_read_packets','-of','csv=p=0', path]
try:
    out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, **_popen_kwargs())
except subprocess.CalledProcessError as err:
    out = err.output.decode(errors="ignore")
    raise RuntimeError(
        "FFMPEG call failed with {}:\n{}".format(err.returncode, out)
    )

# Note that other than with the subprocess calls below, ffmpeg wont hang here.
# Worst case Python will stop/crash and ffmpeg will continue running until done.

nframes = out
return nframes

raise RuntimeError("Could not get number of frames")  # pragma: no cover

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants