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

Lake AI Service

Copy page
Download PDF
Video processing
Adaptive video compression
Copy page
Download PDF
Adaptive video compression

Operator introduction

Description

Adaptive video compression

Key features

  • Compress video adaptively based on the target file size
  • Multi-level compression strategy: frame rate adjustment -> resolution adjustment -> bitrate control
  • Compress the file size as much as possible while maintaining video quality
  • Supports CPU and GPU encoding
  • Supports path input, binary input, and TOS output

Compression strategy

  1. Check the original file size; if it is smaller than the target size, return directly
  2. Adjust the frame rate to target_fps (default: 5 fps), check if the size meets the requirement (skip if the original fps is less than or within 2 fps of the target)
  3. Adjust the resolution proportionally to the minimum resolution (default: 360p), check if the size meets the requirement (skip if the original resolution is less than or within 1.2 times the target resolution)
  4. Use two-pass bitrate control to force compression to the target size

Format support

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

Encoding modes

  • CPU encoding (libx264): Quality prioritized, high compression efficiency, slower speed, lower cost
  • GPU encoding (h264_nvenc): Fast speed, relatively higher cost

Daft invocation

Operator parameters

Input

Input column names

Note

video_paths

Column of video file paths (local, TOS, HTTP, and so on); choose either video_paths or video_binaries

video_binaries

Column of video binary data; choose either video_binaries or video_paths

video_formats

Column of video format strings, used with video_binaries

output_basenames

Column of output file base names (without extension)

Output

Column of compressed video paths

Parameters

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

Parameter name

Type

Default value

Description

output_tos_dir

str

Save the compressed video to this TOS directory; if empty, do not save. Format: "tos://bucket/path/" Default value: ""

max_output_size_mb

float

50.0

Maximum output file size (MB). Default value: 50.0

target_fps

float

5.0

Target value for frame rate adjustment. Default value: 5.0

min_resolution_height

int

360

Minimum resolution height (pixels), maintain aspect ratio. Default value: 360

allowed_formats

list

[mp4, avi, mov]

List of video formats allowed for direct output when the video size meets the requirement. Default value: ["mp4", "avi", "mov"]

rank

int or None

Specify the GPU device ID to use (effective in multi-card environments). Default value: None

Examples

The following code demonstrates how to use Daft (for distributed scenarios) to run the operator for adaptive video compression and intelligently control the file size.

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 VideoAdaptiveCompress

if __name__ == "__main__":
    TOS_TEST_DIR = os.getenv("TOS_TEST_DIR", "tos_bucket")
    samples = {
        "video_path": [f"tos://{TOS_TEST_DIR}/video_adaptive_compress/sample.mp4"],
    }
    ds = daft.from_pydict(samples)

    output_tos_dir = f"tos://{TOS_TEST_DIR}/video_adaptive_compress"
    constructor_kwargs = {
        "output_tos_dir": output_tos_dir,
        "max_output_size_mb": 50.0,
        "target_fps": 5.0,
        "min_resolution_height": 360,
        "rank": None,
    }

    ds = ds.with_column(
        "compressed_video_path",
        las_udf(VideoAdaptiveCompress, construct_args=constructor_kwargs, num_gpus=1, batch_size=1, concurrency=1)(
            col("video_path")
        ),
    )

    ds.show()
    # ╭────────────────────────────────┬──────────────────────────────────╮
    # │ video_path                     ┆ compressed_video_path            │
    # │ ---                            ┆ ---                              │
    # │ Utf8                           ┆ Utf8                             │
    # ╞════════════════════════════════╪══════════════════════════════════╡
    # │ tos://tos_bucket/video_adapt…  ┆ tos://tos_bucket/video_adapt…    │
    # ╰────────────────────────────────┴──────────────────────────────────╯
Last updated: 2026.05.12 19:06:36