1+ import http .client
2+ import json
3+ import time
4+
5+ class KlingVideoExtend :
6+ def __init__ (self , api_token , api_url ):
7+ """初始化 Kling 视频延长生成器
8+
9+ 参数:
10+ api_token: API 密钥
11+ api_url: API 节点地址
12+ """
13+ self .api_url = api_url
14+ self .api_token = api_token
15+ # 初始化 HTTP 连接
16+ self .conn = http .client .HTTPSConnection (self .api_url )
17+ self .endpoint = "/kling/v1/videos/video-extend"
18+ # 设置请求头
19+ self .headers = {
20+ 'Authorization' : f'Bearer { self .api_token } ' ,
21+ 'Content-Type' : 'application/json'
22+ }
23+
24+ def _kling_extend_video (self , task_id , video_id , prompt , negative_prompt = "" , cfg_scale = 0.5 , callback_url = "" ):
25+ """使用 kling 提交视频延长任务
26+
27+ 参数:
28+ task_id: str, 任务ID
29+ video_id: str, 要延长的视频ID
30+ prompt: str, 文本提示词,指导延长方向
31+ negative_prompt: str, 负向文本提示词
32+ cfg_scale: float, 提示词参考强度
33+ callback_url: str, 回调地址,可以用于 webhook 等通知场景
34+ 返回:
35+ task_id: 生成任务的 id
36+ """
37+ # 构建请求体,请求的核心参数
38+ payload = {
39+ "task_id" : task_id ,
40+ "video_id" : video_id ,
41+ "prompt" : prompt ,
42+ "negative_prompt" : negative_prompt ,
43+ "cfg_scale" : cfg_scale ,
44+ "callback_url" : callback_url
45+ }
46+
47+ # 发送 POST 请求,提交视频延长任务
48+ self .conn .request ("POST" , self .endpoint , json .dumps (payload ), self .headers )
49+ # 获取响应
50+ res = self .conn .getresponse ()
51+ # 读取响应内容并解析为 JSON
52+ json_data = json .loads (res .read ().decode ("utf-8" ))
53+
54+ if 'code' in json_data and json_data ['code' ] == 0 :
55+ # 成功则返回提交的任务 id
56+ return json_data ['data' ]['task_id' ]
57+ else :
58+ # 失败则返回错误信息
59+ raise Exception (f"API调用失败:{ json_data ['message' ]} " )
60+
61+ def _query_video_extend_result (self , task_id ):
62+ """使用查询接口获取视频延长结果
63+
64+ 参数:
65+ task_id: 生成任务的 id
66+ 返回:
67+ video_url: 延长后的视频 url,任务未完成时返回 None
68+ """
69+ # 构建查询路径
70+ query_path = f"{ self .endpoint } /{ task_id } "
71+
72+ # 发送 GET 请求,查询视频延长任务状态
73+ self .conn .request ("GET" , query_path , None , self .headers )
74+ # 获取响应
75+ res = self .conn .getresponse ()
76+ # 读取响应内容并解析为 JSON
77+ json_data = json .loads (res .read ().decode ("utf-8" ))
78+
79+ # 如果任务状态为成功,则返回视频 url
80+ if json_data ['data' ]['task_status' ] == "succeed" :
81+ video_url = json_data ['data' ]['task_result' ]['videos' ][0 ]['url' ]
82+ video_id = json_data ['data' ]['task_result' ]['videos' ][0 ]['id' ]
83+ return video_url , video_id
84+ else :
85+ return None
86+
87+ def extend_video (self , task_id , video_id , prompt , negative_prompt = "" , cfg_scale = 0.5 , callback_url = "" , timeout = 300 ):
88+ """实现功能,根据预设的参数延长视频并返回延长后的视频 url
89+
90+ 参数:
91+ task_id: str, 任务ID
92+ video_id: str, 要延长的视频ID
93+ prompt: str, 文本提示词,指导延长方向
94+ negative_prompt: str, 负向文本提示词
95+ cfg_scale: float, 提示词参考强度
96+ callback_url: str, 回调地址,可以用于 webhook 等通知场景
97+ timeout: int, 超时时间(秒)
98+ 返回:
99+ video_url: 延长后的视频 url
100+ """
101+ # 调用视频延长 API 提交任务,返回获取 task_id
102+ task_id = self ._kling_extend_video (task_id , video_id , prompt , negative_prompt , cfg_scale , callback_url )
103+
104+ start_time = time .time ()
105+
106+ # 轮询等待生成完成
107+ while True :
108+ # 根据 task_id 调用查询 API 查看任务是否完成
109+ video_url , video_id = self ._query_video_extend_result (task_id )
110+ # 如果任务完成,则返回视频 url
111+ if video_url is not None :
112+ return video_url , video_id
113+ # 如果轮询超时,则返回 None
114+ if time .time () - start_time > timeout :
115+ print (f"请求达到 { timeout } 秒超时" )
116+ return None
117+ # 轮询间隔 1 秒
118+ time .sleep (1 )
119+ print (f"等待视频延长结果生成,{ int (time .time () - start_time )} 秒" , flush = True )
120+
121+
122+ # 使用示例
123+ if __name__ == "__main__" :
124+ API_URL = "www.dmxapi.cn" # API 节点地址
125+ DMX_API_TOKEN = "sk-XXXXXXXXXXXXXX" # API 密钥
126+
127+ # 创建视频延长生成器实例
128+ kling_video_extend = KlingVideoExtend (api_token = DMX_API_TOKEN , api_url = API_URL )
129+
130+ # 延长视频
131+ video_url , video_id = kling_video_extend .extend_video (
132+ task_id = "CjhDaWgU7GAAAAAAAb1QfA" , # [必选] 任务ID
133+ video_id = "bc17f1e8-6c7b-440c-bd30-e47fb1c82932" , # [必选] 要延长的视频ID
134+ prompt = "继续展示动物的走动,保持相同的风格和氛围" , # [必选] 文本提示词
135+ # negative_prompt="突然的场景转换, 光线变化, 画面抖动", # 负向提示词
136+ # cfg_scale=0.5, # 提示词参考强度,设置较高以保持连贯性
137+ # callback_url="", # 回调地址
138+ # timeout=300 # 等待超时时间,视频处理通常需要更长时间
139+ )
140+
141+ print (video_url )
142+ print (video_id )
0 commit comments