Skip to content

Commit

Permalink
Merge pull request #2373 from Rusteam/feature/exact-frame-count
Browse files Browse the repository at this point in the history
count exact number of frames with ffprbe -count_frames
  • Loading branch information
brimoor authored Nov 30, 2022
2 parents eddfb59 + 4a38d7e commit 2dcf237
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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)

0 comments on commit 2dcf237

Please sign in to comment.