You need to enable JavaScript to run this app.
Lake AI Service

Lake AI Service

Copy page
Download PDF
Video processing
Video aspect ratio adjustment
Copy page
Download PDF
Video aspect ratio adjustment

Operator ID: daft.las.functions.video.video_aspect_ratio_adjust.VideoAspectRatioAdjust

Operator introduction

Description

Video aspect ratio adjustment adapts the video frame to the specified aspect ratio.

Key features:

  • Supports adjusting videos to a canvas with the specified aspect ratio.
  • Supports two strategies: padding (add padding) or cropping.
  • Supports local/TOS/HTTP path input and output.
  • Supports custom encoding parameters.

Format support:

  • MP4 (.mp4)
  • MOV (.mov)
  • MKV (.mkv)
  • AVI (.avi)
  • Other common video formats

Caution and prerequisites

Details

Caution and prerequisites

Costs

Before calling an operator, you need to understand the model invocation costs associated with using the operator. For details, see Large model invocation billing.

Authentication (API Key)

Before calling an operator, you need to generate an API Key for operator invocation. It is recommended to configure the API Key as an environment variable to ensure safer operator calls. For details, see Obtain and configure API Key.

BaseURL

Before calling an operator, you need to determine the BaseURL for operator invocation based on the region where your current LAS service is deployed. This is used to configure the path parameter values for operator calls.
For details, see Obtain the Base URL. The Examples below are for reference only; when making actual calls, replace the path values with those corresponding to your region.

Daft call

Operator parameters

Input

Input column name

Note

input_paths

Column for input video file paths

output_paths

Column for output video file paths

Output

Adjusted video path column

Parameters

If a parameter does not have a default value, it is required.

Parameter name

Type

Default value

Description

target_width

int

Output video width (pixels)

target_height

int

Output video height (pixels)

mode

str

pad

Processing mode, options:

  • "pad": add padding
  • "crop": cropping

video_codec

str

libx264

Video encoder

audio_codec

str

aac

Audio encoder

extra_params

list[str] or None

Additional ffmpeg parameter list

timeout

int or None

ffmpeg execution timeout (seconds)

Examples

The following code demonstrates how to use Daft (for distributed scenarios) to run the operator for video aspect ratio adjustment. Both input and output use TOS paths. Therefore, you need to set environment variables to ensure permission to read and write TOS, including: ACCESS_KEY, SECRET_KEY, TOS_ENDPOINT, TOS_REGION, TOS_TEST_DIR.

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.udf import las_udf
from daft.las.functions.video import VideoAspectRatioAdjust

if __name__ == "__main__":
    # Both input and output use TOS paths. Therefore, you need to set environment variables to ensure permission to read and write TOS, including: ACCESS_KEY, SECRET_KEY, TOS_ENDPOINT, TOS_REGION, TOS_TEST_DIR.
    TOS_TEST_DIR_URL = os.getenv("TOS_TEST_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com")
    TOS_TEST_DIR = os.getenv("TOS_TEST_DIR", "tos_bucket")

    if os.getenv("DAFT_RUNNER", "native") == "ray":
        import logging

        import ray

        def configure_logging():
            logging.basicConfig(
                level=logging.INFO,
                format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                datefmt="%Y-%m-%d %H:%M:%S",
            )
            logging.getLogger("tracing.span").setLevel(logging.WARNING)
            logging.getLogger("daft_io.stats").setLevel(logging.WARNING)
            logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING)

        ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging})
        daft.set_runner_ray()

    daft.set_execution_config(actor_udf_ready_timeout=600)
    daft.set_execution_config(min_cpu_per_task=0)

    samples = {
        "input_path": [f"https://{TOS_TEST_DIR_URL}/public/shared_video_dataset/sample.mp4"],
        "output_path": [f"tos://{TOS_TEST_DIR}/video/video_aspect_ratio_adjust/sample_4x3.mp4"],
    }
    ds = daft.from_pydict(samples)

    constructor_kwargs = {
        "target_width": 1280,
        "target_height": 768,
        "mode": "pad",
    }

    ds = ds.with_column(
        "adjusted_path",
        las_udf(VideoAspectRatioAdjust, construct_args=constructor_kwargs)(
            col("input_path"),
            col("output_path"),
        ),
    )

    ds.show()
    # ╭────────────────────────────────┬────────────────────────────────┬────────────────────────────────╮
    # │ input_path                     ┆ output_path                    ┆ adjusted_path                  │
    # │ ---                            ┆ ---                            ┆ ---                            │
    # │ String                         ┆ String                         ┆ String                         │
    # ╞════════════════════════════════╪════════════════════════════════╪════════════════════════════════╡
    # │ https://las-cn-beijing-publi-… ┆ tos://tos_bucket/video/video_… ┆ tos://tos_bucket/video/video_… │
    # ╰────────────────────────────────┴────────────────────────────────┴────────────────────────────────╯
Last updated: 2026.05.22 11:12:40