Skip to content

video_utils

sleap.sleap_io_adaptors.video_utils

Video utilities for sleap-io integration.

Functions:

Name Description
available_video_exts

Return tuple of supported video extensions.

can_use_ffmpeg

Check if ffmpeg is available for writing videos.

get_last_frame_idx

Get the last frame index for a specific video.

video_get_channels

Get video channels for backward compatibility.

video_get_frame

Get frame by index for backward compatibility.

video_get_frames

Get frame count for backward compatibility.

video_get_height

Get video height for backward compatibility.

video_get_width

Get video width for backward compatibility.

video_util_reset

Reloads the video.

available_video_exts()

Return tuple of supported video extensions.

Returns:

Type Description
Tuple[str]

Tuple of supported video extensions.

Source code in sleap/sleap_io_adaptors/video_utils.py
29
30
31
32
33
34
35
def available_video_exts() -> Tuple[str]:
    """Return tuple of supported video extensions.

    Returns:
        Tuple of supported video extensions.
    """
    return MediaVideo.EXTS + HDF5Video.EXTS + ImageVideo.EXTS + TiffVideo.EXTS

can_use_ffmpeg()

Check if ffmpeg is available for writing videos.

Source code in sleap/sleap_io_adaptors/video_utils.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def can_use_ffmpeg():
    """Check if ffmpeg is available for writing videos."""
    try:
        import imageio_ffmpeg as ffmpeg
    except ImportError:
        return False

    try:
        # Try to get the version of the ffmpeg plugin
        ffmpeg_version = ffmpeg.get_ffmpeg_version()
        if ffmpeg_version:
            return True
    except Exception:
        return False

    return False

get_last_frame_idx(video=None)

Get the last frame index for a specific video. This function recreates the functionality of labels.get_last_frame_idx(video) from the original SLEAP codebase.

Source code in sleap/sleap_io_adaptors/video_utils.py
62
63
64
65
66
67
68
69
70
71
72
73
def get_last_frame_idx(video=None):
    """Get the last frame index for a specific video.
    This function recreates the functionality of labels.get_last_frame_idx(video)
    from the original SLEAP codebase.
    """
    if video is None:
        return None
    if hasattr(video.backend, "source_inds"):
        source_inds = video.backend.source_inds
        return max(source_inds)
    else:
        return len(video) - 1

video_get_channels(video)

Get video channels for backward compatibility.

Parameters:

Name Type Description Default
video Video

Video object to get channels from.

required

Returns:

Type Description
int

Number of channels in video frames.

Source code in sleap/sleap_io_adaptors/video_utils.py
112
113
114
115
116
117
118
119
120
121
def video_get_channels(video: Video) -> int:
    """Get video channels for backward compatibility.

    Args:
        video: Video object to get channels from.

    Returns:
        Number of channels in video frames.
    """
    return video.shape[3] if len(video.shape) > 3 else 3

video_get_frame(video, idx)

Get frame by index for backward compatibility.

Parameters:

Name Type Description Default
video Video

Video object to get frame from.

required
idx int

Frame index to retrieve.

required

Returns:

Type Description

Frame data as numpy array.

Source code in sleap/sleap_io_adaptors/video_utils.py
124
125
126
127
128
129
130
131
132
133
134
def video_get_frame(video: Video, idx: int):
    """Get frame by index for backward compatibility.

    Args:
        video: Video object to get frame from.
        idx: Frame index to retrieve.

    Returns:
        Frame data as numpy array.
    """
    return video[idx]

video_get_frames(video)

Get frame count for backward compatibility.

Parameters:

Name Type Description Default
video Video

Video object to get frame count from.

required

Returns:

Type Description
int

Number of frames in the video.

Source code in sleap/sleap_io_adaptors/video_utils.py
76
77
78
79
80
81
82
83
84
85
def video_get_frames(video: Video) -> int:
    """Get frame count for backward compatibility.

    Args:
        video: Video object to get frame count from.

    Returns:
        Number of frames in the video.
    """
    return len(video)

video_get_height(video)

Get video height for backward compatibility.

Parameters:

Name Type Description Default
video Video

Video object to get height from.

required

Returns:

Type Description
int

Height of video frames in pixels.

Source code in sleap/sleap_io_adaptors/video_utils.py
88
89
90
91
92
93
94
95
96
97
def video_get_height(video: Video) -> int:
    """Get video height for backward compatibility.

    Args:
        video: Video object to get height from.

    Returns:
        Height of video frames in pixels.
    """
    return video.shape[1] if len(video.shape) > 1 else None

video_get_width(video)

Get video width for backward compatibility.

Parameters:

Name Type Description Default
video Video

Video object to get width from.

required

Returns:

Type Description
int

Width of video frames in pixels.

Source code in sleap/sleap_io_adaptors/video_utils.py
100
101
102
103
104
105
106
107
108
109
def video_get_width(video: Video) -> int:
    """Get video width for backward compatibility.

    Args:
        video: Video object to get width from.

    Returns:
        Width of video frames in pixels.
    """
    return video.shape[2] if len(video.shape) > 2 else None

video_util_reset(video, filename=None, grayscale=None)

Reloads the video.

Returns:

Type Description

None

Source code in sleap/sleap_io_adaptors/video_utils.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def video_util_reset(video: Video, filename: str = None, grayscale: bool = None):
    """Reloads the video.

    Returns:
        None
    """
    if filename is not None:
        video.replace_filename(filename, open=False)
        # No apparent 'test frame'.

    # If none, auto-detects based on first frame load.
    video.grayscale = grayscale

    # no apparent 'bgr' attribute (removed in sleap-io?)

    # potential breaking change
    if (filename is not None) or (grayscale is not None):
        # In sleap-io, keep_open is a backend attribute, not a video attribute
        if hasattr(video, "backend") and hasattr(video.backend, "keep_open"):
            video.backend.keep_open = (
                False  # Reader depends on both filename and grayscale
            )