下载模板时trace归组

This commit is contained in:
2025-04-27 14:24:12 +08:00
parent 2fb0f93886
commit f139fbccd7

View File

@ -2,6 +2,7 @@ import json
import os import os
import logging import logging
from telemetry import get_tracer
from util import api, oss from util import api, oss
TEMPLATES = {} TEMPLATES = {}
@ -75,48 +76,50 @@ def get_template_def(template_id):
return TEMPLATES.get(template_id) return TEMPLATES.get(template_id)
def download_template(template_id): def download_template(template_id):
template_info = api.get_template_info(template_id) tracer = get_tracer(__name__)
if not os.path.isdir(template_info['local_path']): with tracer.start_as_current_span("download_template"):
os.makedirs(template_info['local_path']) template_info = api.get_template_info(template_id)
# download template assets if not os.path.isdir(template_info['local_path']):
overall_template = template_info['overall_template'] os.makedirs(template_info['local_path'])
video_parts = template_info['video_parts'] # download template assets
def _download_assets(_template): overall_template = template_info['overall_template']
if 'source' in _template: video_parts = template_info['video_parts']
if str(_template['source']).startswith("http"): def _download_assets(_template):
_, _fn = os.path.split(_template['source']) if 'source' in _template:
new_fp = os.path.join(template_info['local_path'], _fn) if str(_template['source']).startswith("http"):
oss.download_from_oss(_template['source'], new_fp) _, _fn = os.path.split(_template['source'])
if _fn.endswith(".mp4"): new_fp = os.path.join(template_info['local_path'], _fn)
from util.ffmpeg import re_encode_and_annexb oss.download_from_oss(_template['source'], new_fp)
new_fp = re_encode_and_annexb(new_fp) if _fn.endswith(".mp4"):
_template['source'] = os.path.relpath(new_fp, template_info['local_path']) from util.ffmpeg import re_encode_and_annexb
if 'overlays' in _template: new_fp = re_encode_and_annexb(new_fp)
for i in range(len(_template['overlays'])): _template['source'] = os.path.relpath(new_fp, template_info['local_path'])
overlay = _template['overlays'][i] if 'overlays' in _template:
if str(overlay).startswith("http"): for i in range(len(_template['overlays'])):
_, _fn = os.path.split(overlay) overlay = _template['overlays'][i]
oss.download_from_oss(overlay, os.path.join(template_info['local_path'], _fn)) if str(overlay).startswith("http"):
_template['overlays'][i] = _fn _, _fn = os.path.split(overlay)
if 'luts' in _template: oss.download_from_oss(overlay, os.path.join(template_info['local_path'], _fn))
for i in range(len(_template['luts'])): _template['overlays'][i] = _fn
lut = _template['luts'][i] if 'luts' in _template:
if str(lut).startswith("http"): for i in range(len(_template['luts'])):
_, _fn = os.path.split(lut) lut = _template['luts'][i]
oss.download_from_oss(lut, os.path.join(template_info['local_path'], _fn)) if str(lut).startswith("http"):
_template['luts'][i] = _fn _, _fn = os.path.split(lut)
if 'audios' in _template: oss.download_from_oss(lut, os.path.join(template_info['local_path'], _fn))
for i in range(len(_template['audios'])): _template['luts'][i] = _fn
if str(_template['audios'][i]).startswith("http"): if 'audios' in _template:
_, _fn = os.path.split(_template['audios'][i]) for i in range(len(_template['audios'])):
oss.download_from_oss(_template['audios'][i], os.path.join(template_info['local_path'], _fn)) if str(_template['audios'][i]).startswith("http"):
_template['audios'][i] = _fn _, _fn = os.path.split(_template['audios'][i])
_download_assets(overall_template) oss.download_from_oss(_template['audios'][i], os.path.join(template_info['local_path'], _fn))
for video_part in video_parts: _template['audios'][i] = _fn
_download_assets(video_part) _download_assets(overall_template)
with open(os.path.join(template_info['local_path'], 'template.json'), 'w', encoding='utf-8') as f: for video_part in video_parts:
json.dump(template_info, f) _download_assets(video_part)
load_template(template_id, template_info['local_path']) with open(os.path.join(template_info['local_path'], 'template.json'), 'w', encoding='utf-8') as f:
json.dump(template_info, f)
load_template(template_id, template_info['local_path'])
def analyze_template(template_id): def analyze_template(template_id):