视频自适应压缩
更新时间:2026-06-15
简介
视频自适应压缩
功能描述
- 根据目标文件大小自适应压缩视频
- 多级压缩策略:帧率调整 -> 分辨率调整 -> 码率控制
- 保持视频质量的前提下尽可能压缩文件大小
- 支持CPU和GPU编码
- 支持路径输入、二进制输入和BOS输出
- MP4 (.mp4)
- AVI (.avi)
- MOV (.mov)
- MKV (.mkv)
- 其他常见视频格式
- CPU编码 (libx264)**:质量优先,压缩效率高,速度较慢,成本低
- GPU编码 (h264_nvenc)**:速度快,成本相对高
算子参数
输入
| 输入 | 含义 |
|---|---|
| video_paths | 视频文件路径列(本地、BOS、HTTP等),与video_binaries二选一 |
| video_binaries | 视频二进制数据列,与video_paths二选一 |
| video_formats | 视频格式字符串列,配合video_binaries使用 |
| output_basenames | 输出文件基础名称列(不含扩展名) |
输出
| 输出 | 含义 |
|---|---|
| result | 压缩后的视频路径列 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| output_tos_dir | str | '' | 将压缩后的视频保存到该BOS目录中,如果为空则不保存。 格式:"bos://bucket/path/" 默认值:"" |
| max_output_size_mb | float | 50.0 | 最大输出文件大小(MB)。 默认值:50.0 |
| target_fps | float | 5.0 | 帧率调整的目标值。 默认值:5.0 |
| min_resolution_height | int | 360 | 最小分辨率高度(像素),保持宽高比。 默认值:360 |
| allowed_formats | list[str] | ['mp4', 'avi', 'mov'] | 当视频大小满足时,允许直接输出的视频格式列表。 默认值:["mp4", "avi", "mov"] |
| rank | int | None | 指定使用的GPU设备编号(多卡环境有效)。 默认值:None |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.video.video_adaptive_compress import VideoAdaptiveCompress
10
11if __name__ == "__main__":
12 if os.getenv("DAFT_RUNNER", "native") == "ray":
13 import ray
14 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
15 daft.set_runner_ray()
16 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
17
18 # TODO: 根据实际场景准备样本数据
19 samples = {"video_paths": [...], "video_binaries": [...]}
20 ds = daft.from_pydict(samples)
21 constructor_kwargs = {
22 "output_tos_dir": '',
23 "max_output_size_mb": 50.0,
24 "target_fps": 5.0,
25 "min_resolution_height": 360,
26 "allowed_formats": ['mp4', 'avi', 'mov'],
27 }
28 ds = ds.with_column(
29 "result",
30 aihc_udf(
31 VideoAdaptiveCompress,
32 construct_args=constructor_kwargs,
33 num_cpus=1,
34 concurrency=4,
35 batch_size=8,
36 )(col("video_paths"), col("video_binaries"), col("video_formats")),
37 )
38 ds.show()
评价此篇文章
