video

Classes

VideoFrameReader(video_path[, frame_indexes])

Read video frames efficiently using Decord when available, with an OpenCV fallback.

Functions

count_video_streams(all_streams)

Count number of video streams in video.

create_from_frames(frames, output_path[, fps])

gen_video_stream_name(file_name, stream_index)

Create name to video stream from given filename and index of stream.

get_image_size_and_frames_count(path)

Gets image size and number of frames from Video file.

get_info(video_path[, cpu_count])

Get information about video from given path.

get_labeling_tool_link(url[, name])

Returns HTML link to labeling tool for given url which will be opened in new tab.

get_labeling_tool_url(dataset_id, video_id)

Returns url to labeling tool for given video and frame.

get_video_streams(all_streams)

Get list of video streams from given list of all streams.

has_valid_ext(path)

Checks if Video file from given path has supported extension.

is_valid_ext(ext)

Checks if given extension is supported.

is_valid_format(path)

Checks if Video file from given path could be read and has supported extension.

validate_ext(ext)

Raises error if given extension is not supported.

validate_format(path)

Raise error if Video file from given path couldn't be read or file extension is not supported.

warn_video_requires_processing(file_name[, ...])

Create logger if it was not there and displays message about the need for transcoding.

Description

Functions for processing videos

exception UnsupportedVideoFormat[source]

Bases: Exception

Raised when a video container/extension is not supported by the SDK.

exception VideoExtensionError[source]

Bases: Exception

Raised when a video file extension is not supported.

exception VideoReadException[source]

Bases: Exception

Raised when a video file cannot be read or decoded.

class VideoFrameReader(video_path, frame_indexes=None)[source]

Bases: object

Read video frames efficiently using Decord when available, with an OpenCV fallback.

Parameters:
video_path : str

Path to video file.

frame_indexes : List[int]

Optional frame indices to read.

count_video_streams(all_streams)[source]

Count number of video streams in video.

Parameters:
all_streams : List[dict]

List of Video file audio and video streams.

Returns:

Number of video streams in Video file

Return type:

int

gen_video_stream_name(file_name, stream_index)[source]

Create name to video stream from given filename and index of stream.

Parameters:
file_name : str

Video file name.

stream_index : int

Stream index.

Returns:

str

Return type:

str

Usage Example:
stream_name = gen_video_stream_name('my_video.mp4', 2)
print(stream_name)
# Output: my_video_stream_2_CULxO.mp4
get_image_size_and_frames_count(path)[source]

Gets image size and number of frames from Video file.

Parameters:
path : str

Path to Video file.

Returns:

Image size and number of Video frames.

Return type:

Tuple[Tuple[int, int], int]

Usage Example:
import supervisely as sly

video_path = "/home/admin/work/videos/Cars/ds0/video/6x.mp4"
video_info = sly.video.get_image_size_and_frames_count(video_path)
print(video_info)
# Output: ((720, 1280), 152)
get_info(video_path, cpu_count=None)[source]

Get information about video from given path.

Parameters:
video_path : str

Video file path.

cpu_count : int

CPU count.

Raises:

ValueError – if no video streams found.

Returns:

Information about video

Return type:

Dict

Usage Example:
from supervisely.video.video import get_info

video_info = get_info('/home/video/1.mp4')
print(json.dumps(video_info, indent=4))
# Output: {
#     "streams": [
#         {
#             "index": 0,
#             "width": 1920,
#             "height": 1080,
#             "duration": 16.666667,
#             "rotation": 0,
#             "codecName": "mpeg4",
#             "codecType": "video",
#             "startTime": 0,
#             "framesCount": 500,
#             "framesToTimecodes": [
#                 0.0,
#                 0.033333,
#                 0.066667,
#                 0.1,
#                   ...
#                 16.566667,
#                 16.6,
#                 16.633333
#             ]
#         }
#     ],
#     "formatName": "mov,mp4,m4a,3gp,3g2,mj2",
#     "duration": 16.667,
#     "size": "61572600"
# }

Returns HTML link to labeling tool for given url which will be opened in new tab.

Parameters:
url : str

URL for the HTML link.

name : Optional[str]

text of the link, defaults to “open in labeling tool”.

Returns:

HTML link to labeling tool.

Return type:

str

get_labeling_tool_url(dataset_id, video_id, frame=0, link=False, link_text='open in labeling tool')[source]

Returns url to labeling tool for given video and frame. If link is True, returns html link to labeling tool, which will be opened in new tab. See usage example below.

Parameters:
dataset_id : int

ID of the dataset, where video is stored.

video_id : int

ID of the video to get labeling tool url for.

frame : Optional[int]

Frame number to get labeling tool url for, defaults to 0.

If True, returns html link to labeling tool, defaults to False.

Text of the link, defaults to “open in labeling tool”.

Returns:

Labeling tool url or html link to labeling tool.

Return type:

str

Usage Example:
import os
from dotenv import load_dotenv

import supervisely as sly

# Load secrets and create API object from .env file (recommended)
# Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication
if sly.is_development():
    load_dotenv(os.path.expanduser("~/supervisely.env"))

api = sly.Api.from_env()

dataset_id = 123
video_id = 456

# Get url to labeling tool for the 20 frame of the video
url = sly.video.get_labeling_tool_url(dataset_id, video_id, frame=20)
print(url)
# Output: http://your-supervisely-server.com/app/videos_v2/?datasetId=123&videoId=456&videoFrame=20

# Get html link to labeling tool for the 20 frame of the video
link = sly.video.get_labeling_tool_url(dataset_id, video_id, frame=20, link=True)
print(link)
# Output: <a href="http://your-supervisely-server.com/app/videos_v2/?datasetId=123&videoId=456&videoFrame=20"
# rel="noopener noreferrer" target="_blank">open in labeling tool<i class="zmdi zmdi-open-in-new" style="margin-left: 5px"></i></a>
get_video_streams(all_streams)[source]

Get list of video streams from given list of all streams.

Parameters:
all_streams : List[dict]

List of Video file audio and video streams.

Returns:

List of video streams in Video file.

Return type:

list

has_valid_ext(path)[source]

Checks if Video file from given path has supported extension.

Parameters:
path : str

Path to Video file.

Returns:

bool

Return type:

bool

Usage Example:
import supervisely as sly

video_path = "/home/admin/work/videos/Cars/ds0/video/6x.mp4"
sly.video.has_valid_ext(video_path) # True
is_valid_ext(ext)[source]

Checks if given extension is supported.

Parameters:
ext : str

Video file extension.

Returns:

bool

Return type:

bool

Usage Example:
import supervisely as sly

sly.video.is_valid_ext(".mp4")  # True
sly.video.is_valid_ext(".jpeg") # False
is_valid_format(path)[source]

Checks if Video file from given path could be read and has supported extension.

Parameters:
path : str

Path to Video file.

Returns:

True if file format in list of supported video formats, False - in otherwise

Return type:

bool

Usage Example:
import supervisely as sly

video_path = "/video/video.jpg"
sly.video.is_valid_format(video_path) # False
validate_ext(ext)[source]

Raises error if given extension is not supported.

Parameters:
ext : str

Video extension. Available extensions: avi, mp4, 3gp, flv, webm, wmv, mov, mkv.

:raises UnsupportedVideoFormat: if given video with extension that is not supported. :returns: None :rtype: None

Usage Example:
import supervisely as sly

sly.video.validate_ext(".jpeg")
# Unsupported video extension: .jpeg.
# Only the following extensions are supported: ['.avi', '.mp4', '.3gp', '.flv', '.webm', '.wmv', '.mov', '.mkv'].
validate_format(path)[source]

Raise error if Video file from given path couldn’t be read or file extension is not supported.

Parameters:
path : str

Path to Video file.

:raises VideoReadException: if Video file from given path couldn’t be read or file extension is not supported :returns: None :rtype: None

Usage Example:
import supervisely as sly

video_path = "/home/paul/work/sphinx-docs/supervisely_py/docs/source/debug/video/Prius_360/ds0/video/video.jpg"
sly.video.validate_format(video_path)
# Unsupported video extension: .jpg. Only the following extensions are supported: ['.avi', '.mp4', '.3gp', '.flv', '.webm', '.wmv', '.mov', '.mkv'].
warn_video_requires_processing(file_name, logger=None)[source]

Create logger if it was not there and displays message about the need for transcoding.

Parameters:
file_name : str

Video file name.

logger : logger

Logger object.

Returns:

None

Return type:

None