Skip to content

extractors

This module contains extractor objects that perform extractions.

Extractor

Bases: Protocol

Protocol defining the interface for performing any extraction operation.

extract

extract()

Perform extraction and return the path to the extracted file or directory.

Returns:
`Path`: The path to the extracted file or directory.
Source code in videoxt/extractors.py
def extract(self) -> Path | None:
    """
    Perform extraction and return the path to the extracted file or directory.

    Returns:
    -----
        `Path`: The path to the extracted file or directory.
    """
    ...

AudioExtractor dataclass

Instantiate me with a PreparedAudioRequest and I'll provide you with an extract() method that will extract audio from the video file in the request, apply requested edits to the audio, and save it to disk.

Fields:

`request` (PreparedAudioRequest):
    Request parameters the extractor will use to perform the extraction.

Public Methods:

`extract()` -> `Path`:
    Execute audio extraction and return the path to the extracted audio
    file.

request instance-attribute

request

extract

extract()

Extract audio from a video within a given time range and return the path to the saved audio file. Optional edits to audio are applied before saving to disk.

Returns:
`Path | None`:
    The path to the extracted audio file if the write was successful.
Source code in videoxt/extractors.py
def extract(self) -> Path:
    """
    Extract audio from a video within a given time range and return the path to the
    saved audio file. Optional edits to audio are applied before saving to disk.

    Returns:
    -----
        `Path | None`:
            The path to the extracted audio file if the write was successful.
    """
    with VideoFileClip(str(self.request.video.filepath)) as clip:
        subclip = self._edit_clip_audio(clip)
        return self._write_audio_file(subclip)

ClipExtractor dataclass

Instantiate me with a PreparedClipRequest and I'll provide you with an extract() method that will extract a subclip from the video file in the request, apply requested edits to the subclip, and save it to disk.

Fields:

`request` (PreparedClipRequest):
    Request parameters the extractor will use to perform the extraction.

Public Methods:

`extract()` -> `Path`:
    Execute subclip extraction and return the path to the extracted clip.

request instance-attribute

request

extract

extract()

Extract a subclip from a video within a given time range and return the file path to clip. Optional edits to the clip are applied before saving to disk.

Returns:
`Path`:
    The path to the extracted clip if the write was successful.
Source code in videoxt/extractors.py
def extract(self) -> Path:
    """
    Extract a subclip from a video within a given time range and return the file
    path to clip. Optional edits to the clip are applied before saving to disk.

    Returns:
    -----
        `Path`:
            The path to the extracted clip if the write was successful.
    """
    with VideoFileClip(str(self.request.video.filepath)) as clip:
        subclip = self._edit_clip(clip)
        return self._write_subclip(subclip)

FramesExtractor dataclass

Instantiate me with a PreparedFramesRequest and I'll provide you with an extract() method that will extract frames from the video file in the request, apply requested edits to the frames, and save them to disk as images in a directory.

Fields:

`request` (PreparedFramesRequest):
    The request to prepare and perform an extraction operation with.

Public Methods:

`extract()` -> `Path`:
    Execute frames extraction and return the path to the directory where the
    extracted images were saved.

request instance-attribute

request

extract

extract()

Extract frames from a video, save them to disk as images, and return the path to the directory where the images were saved.

Returns:
`Path`: The directory where the extracted images were saved.
Source code in videoxt/extractors.py
def extract(self) -> Path:
    """
    Extract frames from a video, save them to disk as images, and return the path
    to the directory where the images were saved.

    Returns:
    -----
        `Path`: The directory where the extracted images were saved.
    """

    # Create the destination directory if it doesn't exist.
    self.request.destpath.mkdir(parents=True, exist_ok=True)

    from rich.progress import track

    # Open the video capture and iterate over the frames to write to disk.
    with open_video_capture(self.request.video.filepath) as opencap:
        for edited_frame, image_path in track(
            self._preprocess_frames(opencap),
            total=self.request.images_expected,
            transient=True,
            description=(
                "[yellow]Extracting frames...[/yellow]\n"
                "Press [red][bold]Ctrl+C[/red][/bold] to cancel."
            ),
        ):
            self._write_image(edited_frame, image_path)

    return self.request.destpath

GifExtractor dataclass

Instantiate me with a PreparedGifRequest and I'll provide you with an extract() method that will extract a gif from the video file in the request, apply requested edits to the gif, and save it to disk.

Attributes:

`request` (PreparedGifRequest):
    Request parameters the extractor will use to perform the extraction.

Public Methods:

`extract()` -> `Path`:
    Execute gif extraction and return the path to the extracted gif.

request instance-attribute

request

extract

extract()

Extract a gif from a video within a given time range and return the file path the gif. Optional edits to the gif are applied before saving to disk.

Returns:
`Path`:
    The path to the extracted gif if the write was successful.
Source code in videoxt/extractors.py
def extract(self) -> Path:
    """
    Extract a gif from a video within a given time range and return the file path
    the gif. Optional edits to the gif are applied before saving to disk.

    Returns:
    -----
        `Path`:
            The path to the extracted gif if the write was successful.
    """
    with VideoFileClip(str(self.request.video.filepath)) as clip:
        subclip = self._edit_clip(clip)
        return self._write_gif(subclip)