视频关键帧抽取
更新时间:2026-05-18
视频关键帧抽取
简介
视频关键帧抽取算子,基于 OpenCV 和 FFmpeg 实现,支持多种关键帧检测方法。
功能描述
从视频中提取关键帧,支持 I 帧检测、帧差异、光流直方图等多种方法。可选择输出关键帧数据、Base64 编码或上传到 BOS 存储。
算子参数
输入
| 输入 | 含义 |
|---|---|
| video_paths | 视频文件路径列表 |
| video_binaries | 视频二进制数据列表 |
| video_formats | 视频格式列表 |
输出
| 输出 | 含义 |
|---|---|
| keyframes | 关键帧数据(四维数组) |
| base64 | Base64 编码的关键帧图片 |
| timestamps | 关键帧对应的时间戳(秒) |
| bos_paths | BOS 存储路径 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| method | str | "I_frame" | 关键帧检测方法 |
| img_type | str | ".jpg" | 输出图片格式 |
| threshold | float | 0 | 检测阈值 |
| keyframes_cnt | int | 10 | 最大关键帧数量 |
| seconds_per_frame | int | -1 | 每帧秒数 |
| output_bos_dir | str | "" | BOS 输出目录 |
| by_count_uniform | bool | False | 是否均匀采样 |
| return_keyframes | bool | True | 是否返回关键帧数据 |
| return_base64 | bool | True | 是否返回 Base64 编码 |
关键帧检测方法
| 方法 | 说明 |
|---|---|
| I_frame | 基于 I 帧检测 |
| difference | 基于帧差异 |
| optical_flow | 基于光流 |
| histogram | 基于直方图比较 |
调用示例
Plain Text
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_keyframes import VideoKeyframes
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 samples = {
19 "video_path": ["/tmp/test_video.mp4", "/tmp/test_video2.mp4"]
20 }
21 ds = daft.from_pydict(samples)
22
23 constructor_kwargs = {
24 "method": "I_frame",
25 "keyframes_cnt": 10,
26 }
27 ds = ds.with_column(
28 "keyframes",
29 aihc_udf(
30 VideoKeyframes,
31 construct_args=constructor_kwargs,
32 num_cpus=1,
33 concurrency=4,
34 )(col("video_path"), None, None),
35 )
36 ds.show()
评价此篇文章
