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

count exact number of frames with ffprbe -count_frames #2373

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions fiftyone/utils/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
|
"""
import itertools
import json
import logging
import os

Expand Down Expand Up @@ -658,6 +659,33 @@ def concat_videos(input_paths, output_path, verbose=False):
ffmpeg.run(input_list_path, output_path, verbose=verbose)


def exact_frame_count(input_path):
"""Returns the exact number of frames in the video.

This method uses -count_frames argument of ffprobe
which decodes the entire video to count the frames
and can be very slow.

Args:
input_path: the path to the video

Returns:
the number of frames in the video
"""
opts = [
"-count_frames",
"-select_streams",
"v:0",
"-print_format",
"json",
"-show_streams",
]
ffprobe = etav.FFprobe(opts=opts)
output = ffprobe.run(input_path)
output = json.loads(output.decode())
return int(output["streams"][0]["nb_read_frames"])


def _transform_videos(
sample_collection,
frames=None,
Expand Down
10 changes: 10 additions & 0 deletions tests/intensive/video_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import fiftyone as fo
import fiftyone.zoo as foz
from fiftyone import ViewField as F
import fiftyone.utils.video as fouv


def test_to_clips():
Expand Down Expand Up @@ -458,6 +459,15 @@ def test_to_frame_eval_patches():
print(patches.count_values("type"))


def test_exact_frame_count():
dataset = foz.load_zoo_dataset("quickstart-video").clone()
dataset.limit(2).save()

for smp in dataset:
frame_count = fouv.exact_frame_count(smp.filepath)
assert frame_count == len(smp.frames)


if __name__ == "__main__":
fo.config.show_progress_bars = True
unittest.main(verbosity=2)