添加边转换边输出frame_urls

This commit is contained in:
nnwang
2026-02-07 02:21:57 +08:00
parent b318144230
commit 736d7dd8f9
5 changed files with 540 additions and 70 deletions

60
main.py
View File

@@ -234,15 +234,15 @@ def video_frame_async_api():
@app.route('/api/task/<task_id>', methods=['GET'])
def get_task_status(task_id):
"""查询任务状态和进度(自动返回新增日志)"""
"""查询任务状态和进度(自动返回新增日志和实时帧信息"""
with task_lock:
if task_id not in task_registry:
return jsonify({'error': '任务不存在'}), 404
task_info = task_registry[task_id].copy()
current_log_count = len(task_info['logs'])
last_returned_index = task_info['last_returned_index']
# 计算新增日志
if last_returned_index < current_log_count:
new_logs = task_info['logs'][last_returned_index:]
@@ -250,31 +250,59 @@ def get_task_status(task_id):
task_registry[task_id]['last_returned_index'] = current_log_count
else:
new_logs = []
# 构建响应
response = {
'task_id': task_id,
'status': task_info['status'],
'type': task_info['type'],
'create_time': task_info['create_time'],
'start_time': task_info.get('start_time'),
'end_time': task_info.get('end_time'),
'progress': task_info.get('progress', 0),
'total_logs': current_log_count,
'new_logs': new_logs,
'last_index': current_log_count # 返回当前日志总数,方便客户端跟踪
'last_index': current_log_count,
'new_logs': new_logs
}
if 'start_time' in task_info:
response['start_time'] = task_info['start_time']
if 'end_time' in task_info:
response['end_time'] = task_info['end_time']
# 根据状态返回不同信息
if task_info['status'] == 'completed':
# 视频帧任务特殊处理
if task_info['type'] == 'video_frame':
result_data = {}
# 音频URL
if 'audio_urls' in task_info:
result_data['audio_urls'] = task_info['audio_urls']
# 当前帧信息
if 'current_frames' in task_info:
result_data['current_frames'] = task_info['current_frames']
# 总帧数
if 'estimated_total_frames' in task_info:
result_data['total_frames'] = task_info['estimated_total_frames']
# 输出分辨率
if 'output_resolution' in task_info:
result_data['output_resolution'] = task_info['output_resolution']
# 帧URL列表
if 'frame_job_dir' in task_info:
import os
import glob
job_dir = task_info['frame_job_dir']
if os.path.exists(job_dir):
frame_files = sorted([f for f in os.listdir(job_dir) if f.endswith('.png')])
frame_urls = [f"/frames/{task_id}/{f}" for f in frame_files]
result_data['frame_urls'] = frame_urls
response['result'] = result_data
# 其他类型任务的result处理
elif task_info['status'] == 'completed':
response['result'] = task_info['result']
elif task_info['status'] == 'error':
response['error'] = task_info['error']
return jsonify(response), 200
# 原有的同步接口保持不变