Skip to content

Video Models

aana.core.models.video

Video

Video(path=None, url=None, content=None, media_id=lambda: str(uuid.uuid4())(), save_on_disk=True, is_saved=False, media_dir=settings.video_dir, title='', description='')

Bases: Media

A class representing a video.

At least one of path, url, or content must be provided. If save_on_disk is True, the video will be saved on disk automatically.

ATTRIBUTE DESCRIPTION
path

the path to the video file

TYPE: Path

url

the URL of the video

TYPE: str

content

the content of the video in bytes

TYPE: bytes

media_id

the ID of the video. If not provided, it will be generated automatically.

TYPE: MediaId

title

the title of the video

TYPE: str

description

the description of the video

TYPE: str

media_dir

the directory to save the video in

TYPE: Path

save

save()

Save the media on disk.

If the media is already available on disk, do nothing. If the media represented as a byte string, save it on disk If the media is represented as a URL, download it and save it on disk.

RAISES DESCRIPTION
ValueError

if at least one of 'path', 'url', or 'content' is not provided

Source code in aana/core/models/media.py
def save(self):
    """Save the media on disk.

    If the media is already available on disk, do nothing.
    If the media represented as a byte string, save it on disk
    If the media is represented as a URL, download it and save it on disk.

    Raises:
        ValueError: if at least one of 'path', 'url', or 'content' is not provided
    """
    if self.path:
        return

    if self.media_dir is None:
        raise ValueError(  # noqa: TRY003
            "The 'media_dir' isn't defined for this media type."
        )
    self.media_dir.mkdir(parents=True, exist_ok=True)
    file_path = self.media_dir / (self.media_id + ".mp4")

    if self.content:
        self._save_from_content(file_path)
    elif self.url:
        self._save_from_url(file_path)
    else:
        raise ValueError(  # noqa: TRY003
            "At least one of 'path', 'url', or 'content' must be provided."
        )
    self.is_saved = True

get_content

get_content()

Get the content of the media as bytes.

RETURNS DESCRIPTION
bytes

the content of the media

TYPE: bytes

RAISES DESCRIPTION
ValueError

if at least one of 'path', 'url', or 'content' is not provided

Source code in aana/core/models/media.py
def get_content(self) -> bytes:
    """Get the content of the media as bytes.

    Returns:
        bytes: the content of the media

    Raises:
        ValueError: if at least one of 'path', 'url', or 'content' is not provided
    """
    if self.content:
        return self.content
    elif self.path:
        self._load_content_from_path()
    elif self.url:
        self._load_content_from_url()
    else:
        raise ValueError(  # noqa: TRY003
            "At least one of 'path', 'url', or 'content' must be provided."
        )
    assert self.content is not None  # noqa: S101
    return self.content

cleanup

cleanup()

Cleanup the media.

If the media is saved on disk by the class, delete it.

Source code in aana/core/models/media.py
def cleanup(self):
    """Cleanup the media.

    If the media is saved on disk by the class, delete it.
    """
    if self.is_saved and self.path:
        self.path.unlink(missing_ok=True)

is_video

is_video()

Checks if it's a valid video.

Source code in aana/core/models/video.py
def is_video(self) -> bool:
    """Checks if it's a valid video."""
    if not self.path:
        return False

    try:
        decord.VideoReader(str(self.path))
    except DECORDError:
        try:
            decord.AudioReader(str(self.path))
        except DECORDError:
            return False
    return True

VideoMetadata

Bases: BaseModel

Metadata of a video.

ATTRIBUTE DESCRIPTION
title

the title of the video

TYPE: str

description

the description of the video

TYPE: str

duration

the duration of the video in seconds

TYPE: float

VideoParams

Bases: BaseModel

A pydantic model for video parameters.

ATTRIBUTE DESCRIPTION
extract_fps

the number of frames to extract per second

TYPE: float

fast_mode_enabled

whether to use fast mode (keyframes only)

TYPE: bool

VideoInput

Bases: BaseModel

A video input.

Exactly one of 'path', 'url', or 'content' must be provided.

If 'content' is set to 'file', the video will be loaded from the files uploaded to the endpoint.

ATTRIBUTE DESCRIPTION
media_id

the ID of the video. If not provided, it will be generated automatically.

TYPE: MediaId

path

the file path of the video

TYPE: str

url

the URL of the video (supports YouTube videos)

TYPE: AnyUrl

content

the content of the video in bytes

TYPE: bytes

check_only_one_field

check_only_one_field()

Check that exactly one of 'path', 'url', or 'content' is provided.

RAISES DESCRIPTION
ValueError

if not exactly one of 'path', 'url', or 'content' is provided

RETURNS DESCRIPTION
Self

the instance

TYPE: Self

Source code in aana/core/models/video.py
@model_validator(mode="after")
def check_only_one_field(self) -> Self:
    """Check that exactly one of 'path', 'url', or 'content' is provided.

    Raises:
        ValueError: if not exactly one of 'path', 'url', or 'content' is provided

    Returns:
        Self: the instance
    """
    count = sum(value is not None for value in [self.path, self.url, self.content])
    if count != 1:
        raise ValueError(  # noqa: TRY003
            "Exactly one of 'path', 'url', or 'content' must be provided."
        )
    return self

set_files

set_files(files)

Set the files for the video.

PARAMETER DESCRIPTION
files

the files uploaded to the endpoint

TYPE: Dict[str, bytes]

RAISES DESCRIPTION
UploadedFileNotFound

if the file is not found

Source code in aana/core/models/video.py
def set_files(self, files: dict[str, bytes]):
    """Set the files for the video.

    Args:
        files (Dict[str, bytes]): the files uploaded to the endpoint

    Raises:
        UploadedFileNotFound: if the file is not found
    """
    if self.content:
        file_name = self.content
        if file_name not in files:
            raise UploadedFileNotFound(filename=file_name)
        self._file = files[file_name]

convert_input_to_object

convert_input_to_object()

Convert the video input to a video object.

RETURNS DESCRIPTION
Video

the video object corresponding to the video input

TYPE: Video

RAISES DESCRIPTION
UploadedFileNotFound

if the file is not found

Source code in aana/core/models/video.py
def convert_input_to_object(self) -> Video:
    """Convert the video input to a video object.

    Returns:
        Video: the video object corresponding to the video input

    Raises:
        UploadedFileNotFound: if the file is not found
    """
    if self.content and not self._file:
        raise UploadedFileNotFound(filename=self.content)
    content = self._file if self.content else None

    return Video(
        path=Path(self.path) if self.path is not None else None,
        url=self.url,
        content=content,
        media_id=self.media_id,
    )

VideoInputList

Bases: BaseListModel

A pydantic model for a list of video inputs.

Only used for the requests, DO NOT use it for anything else.

Convert it to a list of video objects with convert_input_to_object().

check_non_empty

check_non_empty()

Check that the list of videos isn't empty.

RAISES DESCRIPTION
ValueError

if the list of videos is empty

RETURNS DESCRIPTION
Self

the instance

TYPE: Self

Source code in aana/core/models/video.py
@model_validator(mode="after")
def check_non_empty(self) -> Self:
    """Check that the list of videos isn't empty.

    Raises:
        ValueError: if the list of videos is empty

    Returns:
        Self: the instance
    """
    if len(self.root) == 0:
        raise ValueError("The list of videos must not be empty.")  # noqa: TRY003
    return self

set_files

set_files(files)

Set the files for the videos.

PARAMETER DESCRIPTION
files

the files uploaded to the endpoint

TYPE: dict[str, bytes]

RAISES DESCRIPTION
UploadedFileNotFound

if the file is not found

Source code in aana/core/models/video.py
def set_files(self, files: dict[str, bytes]):
    """Set the files for the videos.

    Args:
        files (dict[str, bytes]): the files uploaded to the endpoint

    Raises:
        UploadedFileNotFound: if the file is not found
    """
    for video in self.root:
        video.set_files(files)

convert_input_to_object

convert_input_to_object()

Convert the VideoInputList to a list of video inputs.

RETURNS DESCRIPTION
list[VideoInput]

List[VideoInput]: the list of video inputs

Source code in aana/core/models/video.py
def convert_input_to_object(self) -> list[VideoInput]:
    """Convert the VideoInputList to a list of video inputs.

    Returns:
        List[VideoInput]: the list of video inputs
    """
    return self.root