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

Lake AI Service

Copy page
Download PDF
Video processing
Video black border detection and cropping
Copy page
Download PDF
Video black border detection and cropping

Operator ID: daft.las.functions.video.video_blackborder_crop.VideoBlackBorderCrop

Operator introduction

Description

Video black border detection and cropping

Key features

  • Multi-algorithm support: Supports three black border detection algorithms, suitable for different scenarios:
  • Row and column ratio method (threshold_ratio): Suitable for scenarios where there are watermarks or scattered noise within the black border.
  • Edge detection method (edge_detection): Suitable for scenarios with gradient black borders or slight noise.
  • Histogram analysis method (histogram): Suitable for scenarios with uneven pixel distribution in black borders.
  • Automatic boundary calculation: Automatically calculates the boundaries of the valid frame area.
  • Lossless cropping: Video cropping is implemented based on FFmpeg, preserving the original encoding characteristics.
  • Audio-video synchronization: Retains the original audio and ensures precise alignment between audio and video.

Applicable scenarios

  • Video preprocessing
  • Remove video black borders
  • Video standardization

Format support

  • MP4 (.mp4)
  • AVI (.avi)
  • MOV (.mov)
  • MKV (.mkv)
  • 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 invocation

Operator parameters

Input

Input column name

Note

input_col

An array containing input video paths (supports local paths, HTTP/HTTPS URLs, TOS/S3 URLs).

output_col

An array containing the paths of the cropped output files.

Output

An array containing the paths of the cropped results (string type). Returns the output path on success; returns None on failure.

Parameters

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

Parameter name

Type

Default value

Description

detection_method

str

threshold_ratio

Black border detection algorithm.
Optional values: ["threshold_ratio", "edge_detection", "histogram"]

  • threshold_ratio: Suitable for watermark plus black border scenarios
  • edge_detection: Suitable for gradient black border or slight noise scenarios
  • histogram: Suitable for uneven pixel distribution scenarios

Default value: "threshold_ratio"

black_threshold

int

10

Black border determination threshold; pixel values less than this value are considered black borders (0-255).
Default value: 10

valid_pixel_ratio

float

0.1

Threshold for the proportion of non-black pixels in valid rows and columns (0-1).
Default value: 0.1

sample_frames

int

20

Number of sampled frames for black border recognition; more samples lead to more accurate recognition.
Default value: 20

is_keep_audio

bool

true

Whether to retain audio.
Default value: True

timeout

int

None

Timeout for processing a single video (seconds); no limit if set to None.
Default value: None

core_region_ratio

float

0.5

Core region ratio, used to assist judgment.
Default value: 0.5

continuous_black_rows

int

3

Threshold for the number of consecutive black border rows.
Default value: 3

continuous_black_cols

int

3

Threshold for the number of consecutive black border columns.
Default value: 3

dark_region_brightness

int

50

Dark region brightness threshold.
Default value: 50

edge_sensitivity

float

1.0

Edge detection sensitivity.
Default value: 1.0

Examples

The following code demonstrates how to use Daft (for distributed environments) to run the operator for video black border detection and cropping.

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 VideoBlackBorderCrop

if __name__ == "__main__":
    # The content will be saved to the specified TOS path. Therefore, you need to set environment variables to ensure you have permission to write to TOS, including: ACCESS_KEY, SECRET_KEY, TOS_ENDPOINT, TOS_REGION, TOS_TEST_DIR
    TOS_DIR = os.getenv("TOS_TEST_DIR", "tos_bucket")
    output_tos_dir = f"tos://{TOS_DIR}/video/video_blackborder_crop"
    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.%s".format(),
            )
            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)

    tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com")
    samples = {
        "input_path": [f"https://{tos_dir_url}/public/shared_video_dataset/sample.mp4"],
        "output_path": [f"{output_tos_dir}/test_blackborder_video_cropped_histogram.mp4"],
    }
    ds = daft.from_pydict(samples)

    constructor_kwargs = {
        "detection_method": "histogram",
        "black_threshold": 50,
        "sample_frames": 10,
        "is_keep_audio": True,
        "core_region_ratio": 0.5,
        "continuous_black_rows": 3,
        "continuous_black_cols": 3,
        "dark_region_brightness": 50,
    }

    ds = ds.with_column(
        "crop_result",
        las_udf(VideoBlackBorderCrop, construct_args=constructor_kwargs)(col("input_path"), col("output_path")),
    )

    ds.show()

# ╭────────────────────────────────┬────────────────────────────────┬────────────────────────────────╮
# │ input_path                     ┆ output_path                    ┆ crop_result                    │
# │ ---                            ┆ ---                            ┆ ---                            │
# │ String                         ┆ String                         ┆ String                         │
# ╞════════════════════════════════╪════════════════════════════════╪════════════════════════════════╡
# │ https://las-ai-qa-online.tos-… ┆ https://las-ai-qa-online.tos-… ┆ https://las-ai-qa-online.tos-… │
# ╰────────────────────────────────┴────────────────────────────────┴────────────────────────────────╯
Last updated: 2026.05.24 15:43:14